From 483f65a5d58b5bca2836db4bf825b4db6d2f88af Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 21 Jul 2021 14:45:49 -0700 Subject: [PATCH] Removing dead sections and adding specs for avatars --- build | 2 +- openpapi/ucentralsec/ucentralsec.yaml | 27 +++++ src/RESTAPI_system_command.cpp | 146 ++++++++++++++++---------- src/RESTAPI_system_command.h | 28 ++--- 4 files changed, 137 insertions(+), 66 deletions(-) diff --git a/build b/build index 9a03714..9d60796 100644 --- a/build +++ b/build @@ -1 +1 @@ -10 \ No newline at end of file +11 \ No newline at end of file diff --git a/openpapi/ucentralsec/ucentralsec.yaml b/openpapi/ucentralsec/ucentralsec.yaml index bacaf59..263e2e7 100644 --- a/openpapi/ucentralsec/ucentralsec.yaml +++ b/openpapi/ucentralsec/ucentralsec.yaml @@ -804,6 +804,33 @@ paths: $ref: '#/components/responses/Unauthorized' 404: $ref: '#/components/responses/NotFound' + get: + tags: + - System Commands + summary: Retrieve different values from the running service. + operationId: getSystemCommand + parameters: + - in: query + description: Get a value + name: command + schema: + type: string + enum: + - version + - times + required: true + + responses: + 200: + description: Successfull command execution + content: + application/json: + schema: + $ref: '#/components/schemas/TagValuePair' + 403: + $ref: '#/components/responses/Unauthorized' + 404: + $ref: '#/components/responses/NotFound' /securityProfiles: get: diff --git a/src/RESTAPI_system_command.cpp b/src/RESTAPI_system_command.cpp index fdd30ac..a29ca17 100644 --- a/src/RESTAPI_system_command.cpp +++ b/src/RESTAPI_system_command.cpp @@ -57,70 +57,110 @@ namespace uCentral { if (!IsAuthorized(Request, Response)) return; + if (Request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST) + DoPost(Request, Response); + else if(Request.getMethod()==Poco::Net::HTTPRequest::HTTP_GET) + DoGet(Request, Response); + + BadRequest(Request, Response); + } + + void RESTAPI_system_command::DoPost(Poco::Net::HTTPServerRequest &Request, Poco::Net::HTTPServerResponse &Response) { try { - if (Request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST) { - Poco::JSON::Parser parser; - auto Obj = parser.parse(Request.stream()).extract(); + Poco::JSON::Parser parser; + auto Obj = parser.parse(Request.stream()).extract(); - if (Obj->has(uCentral::RESTAPI::Protocol::COMMAND)) { - auto Command = Poco::toLower(Obj->get(uCentral::RESTAPI::Protocol::COMMAND).toString()); - if (Command == uCentral::RESTAPI::Protocol::SETLOGLEVEL) { - if (Obj->has(uCentral::RESTAPI::Protocol::PARAMETERS) && - Obj->isArray(uCentral::RESTAPI::Protocol::PARAMETERS)) { - auto ParametersBlock = Obj->getArray(uCentral::RESTAPI::Protocol::PARAMETERS); - for (const auto &i:*ParametersBlock) { - Poco::JSON::Parser pp; - auto InnerObj = pp.parse(i).extract(); - if (InnerObj->has(uCentral::RESTAPI::Protocol::TAG) && - InnerObj->has(uCentral::RESTAPI::Protocol::VALUE)) { - auto Name = GetS(uCentral::RESTAPI::Protocol::TAG, InnerObj); - auto Value = GetS(uCentral::RESTAPI::Protocol::VALUE, InnerObj); - Daemon()->SetSubsystemLogLevel(Name, Value); - Logger_.information(Poco::format("Setting log level for %s at %s", Name, Value)); - } + if (Obj->has(uCentral::RESTAPI::Protocol::COMMAND)) { + auto Command = Poco::toLower(Obj->get(uCentral::RESTAPI::Protocol::COMMAND).toString()); + if (Command == uCentral::RESTAPI::Protocol::SETLOGLEVEL) { + if (Obj->has(uCentral::RESTAPI::Protocol::PARAMETERS) && + Obj->isArray(uCentral::RESTAPI::Protocol::PARAMETERS)) { + auto ParametersBlock = Obj->getArray(uCentral::RESTAPI::Protocol::PARAMETERS); + for (const auto &i:*ParametersBlock) { + Poco::JSON::Parser pp; + auto InnerObj = pp.parse(i).extract(); + if (InnerObj->has(uCentral::RESTAPI::Protocol::TAG) && + InnerObj->has(uCentral::RESTAPI::Protocol::VALUE)) { + auto Name = GetS(uCentral::RESTAPI::Protocol::TAG, InnerObj); + auto Value = GetS(uCentral::RESTAPI::Protocol::VALUE, InnerObj); + Daemon()->SetSubsystemLogLevel(Name, Value); + Logger_.information(Poco::format("Setting log level for %s at %s", Name, Value)); } - OK(Request, Response); - return; } - } else if (Command == uCentral::RESTAPI::Protocol::GETLOGLEVELS) { - auto CurrentLogLevels = Daemon()->GetLogLevels(); - Poco::JSON::Object Result; - Poco::JSON::Array Array; - for(auto &[Name,Level]:CurrentLogLevels) { - Poco::JSON::Object Pair; - Pair.set( uCentral::RESTAPI::Protocol::TAG,Name); - Pair.set(uCentral::RESTAPI::Protocol::VALUE,Level); - Array.add(Pair); - } - Result.set(uCentral::RESTAPI::Protocol::TAGLIST,Array); - ReturnObject(Request,Result,Response); + OK(Request, Response); return; - } else if (Command == uCentral::RESTAPI::Protocol::GETLOGLEVELNAMES) { - Poco::JSON::Object Result; - Poco::JSON::Array LevelNamesArray; - const Types::StringVec & LevelNames = Daemon()->GetLogLevelNames(); - for(const auto &i:LevelNames) - LevelNamesArray.add(i); - Result.set(uCentral::RESTAPI::Protocol::LIST,LevelNamesArray); - ReturnObject(Request,Result,Response); - return; - } else if (Command == uCentral::RESTAPI::Protocol::GETSUBSYSTEMNAMES) { - Poco::JSON::Object Result; - Poco::JSON::Array LevelNamesArray; - const Types::StringVec & SubSystemNames = Daemon()->GetSubSystems(); - for(const auto &i:SubSystemNames) - LevelNamesArray.add(i); - Result.set(uCentral::RESTAPI::Protocol::LIST,LevelNamesArray); - ReturnObject(Request,Result,Response); - return; - } else if (Command == uCentral::RESTAPI::Protocol::STATS) { - } + } else if (Command == uCentral::RESTAPI::Protocol::GETLOGLEVELS) { + auto CurrentLogLevels = Daemon()->GetLogLevels(); + Poco::JSON::Object Result; + Poco::JSON::Array Array; + for(auto &[Name,Level]:CurrentLogLevels) { + Poco::JSON::Object Pair; + Pair.set( uCentral::RESTAPI::Protocol::TAG,Name); + Pair.set(uCentral::RESTAPI::Protocol::VALUE,Level); + Array.add(Pair); + } + Result.set(uCentral::RESTAPI::Protocol::TAGLIST,Array); + ReturnObject(Request,Result,Response); + return; + } else if (Command == uCentral::RESTAPI::Protocol::GETLOGLEVELNAMES) { + Poco::JSON::Object Result; + Poco::JSON::Array LevelNamesArray; + const Types::StringVec & LevelNames = Daemon()->GetLogLevelNames(); + for(const auto &i:LevelNames) + LevelNamesArray.add(i); + Result.set(uCentral::RESTAPI::Protocol::LIST,LevelNamesArray); + ReturnObject(Request,Result,Response); + return; + } else if (Command == uCentral::RESTAPI::Protocol::GETSUBSYSTEMNAMES) { + Poco::JSON::Object Result; + Poco::JSON::Array LevelNamesArray; + const Types::StringVec & SubSystemNames = Daemon()->GetSubSystems(); + for(const auto &i:SubSystemNames) + LevelNamesArray.add(i); + Result.set(uCentral::RESTAPI::Protocol::LIST,LevelNamesArray); + ReturnObject(Request,Result,Response); + return; + } else if (Command == uCentral::RESTAPI::Protocol::STATS) { + } } + } catch(const Poco::Exception &E) { + Logger_.log(E); + } + BadRequest(Request, Response); + } + + void RESTAPI_system_command::DoGet(Poco::Net::HTTPServerRequest &Request, Poco::Net::HTTPServerResponse &Response) { + try { + ParseParameters(Request); + auto Command = GetParameter("command", ""); + if (!Poco::icompare(Command, "version")) { + Poco::JSON::Object Answer; + Answer.set("tag", "version"); + Answer.set("value", Daemon()->Version()); + ReturnObject(Request, Answer, Response); + return; + } + if (!Poco::icompare(Command, "times")) { + Poco::JSON::Array Array; + Poco::JSON::Object Answer; + Poco::JSON::Object UpTimeObj; + UpTimeObj.set("tag","uptime"); + UpTimeObj.set("value", Daemon()->uptime().totalSeconds()); + Poco::JSON::Object StartObj; + StartObj.set("tag","start"); + StartObj.set("value", Daemon()->startTime().epochTime()); + Array.add(UpTimeObj); + Array.add(StartObj); + Answer.set("times", Array); + ReturnObject(Request, Answer, Response); + return; + } } catch (const Poco::Exception &E) { Logger_.log(E); } BadRequest(Request, Response); } + } \ No newline at end of file diff --git a/src/RESTAPI_system_command.h b/src/RESTAPI_system_command.h index 1748912..8295cbe 100644 --- a/src/RESTAPI_system_command.h +++ b/src/RESTAPI_system_command.h @@ -12,17 +12,21 @@ #include "RESTAPI_handler.h" namespace uCentral { - class RESTAPI_system_command : public RESTAPIHandler { - public: - RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{Poco::Net::HTTPRequest::HTTP_POST, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Internal) {} - void handleRequest(Poco::Net::HTTPServerRequest &request, - Poco::Net::HTTPServerResponse &response) override; - static const std::list PathName() { return std::list{"/api/v1/system"}; }; - }; +class RESTAPI_system_command : public RESTAPIHandler { + public: + RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_POST, + Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Internal) {} + void handleRequest(Poco::Net::HTTPServerRequest &request, + Poco::Net::HTTPServerResponse &response) override; + static const std::list PathName() { return std::list{"/api/v1/system"};} + void DoGet(Poco::Net::HTTPServerRequest &Request, + Poco::Net::HTTPServerResponse &Response); + void DoPost(Poco::Net::HTTPServerRequest &Request, + Poco::Net::HTTPServerResponse &Response); + }; } - #endif // UCENTRALGW_RESTAPI_SYSTEM_COMMAND_H