Adding configuration block management.

This commit is contained in:
stephb9959
2021-09-09 12:08:39 -07:00
parent 274c6f13eb
commit 926a1de197
4 changed files with 115 additions and 83 deletions

2
build
View File

@@ -1 +1 @@
131 133

View File

@@ -89,6 +89,42 @@ namespace OpenWifi{
BadRequest(Request, Response, "Internal error. Consult documentation and try again."); BadRequest(Request, Response, "Internal error. Consult documentation and try again.");
} }
// interfaces
// metrics
// radios
// services
// globals
// unit
bool RESTAPI_configurations_handler::ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config,
Poco::Net::HTTPServerRequest &Request,
Poco::Net::HTTPServerResponse &Response) {
static const std::vector<std::string> SectionNames{ "globals", "interfaces", "metrics", "radios", "services", "unit" };
try {
for(const auto &i:Config.configuration) {
Poco::JSON::Parser P;
if(i.name.empty()) {
BadRequest(Request, Response, "The configuration block name must be included.");
return false;
}
auto Blocks = P.parse(i.configuration).extract<Poco::JSON::Object::Ptr>();
auto N = Blocks->getNames();
for(const auto &j:N) {
if(std::find(SectionNames.cbegin(),SectionNames.cend(),j)==SectionNames.cend()) {
BadRequest(Request, Response, "Configuration block type invalid.");
return false;
}
}
}
} catch (const Poco::Exception &E) {
BadRequest(Request, Response, "Invalid configuration portion.");
return false;
}
return true;
}
void RESTAPI_configurations_handler::DoPost(Poco::Net::HTTPServerRequest &Request, void RESTAPI_configurations_handler::DoPost(Poco::Net::HTTPServerRequest &Request,
Poco::Net::HTTPServerResponse &Response) { Poco::Net::HTTPServerResponse &Response) {
try { try {
@@ -127,20 +163,8 @@ namespace OpenWifi{
return; return;
} }
try { if(!ValidateConfigBlock(C, Request, Response))
for(const auto &i:C.configuration) {
Poco::JSON::Parser P;
// std::cout << "Config:>>>" << std::endl << i.configuration << std::endl << "<<<" << std::endl;
if(i.name.empty()) {
BadRequest(Request, Response, "The configuration block name must be included.");
return;
}
P.parse(i.configuration).extract<Poco::JSON::Object::Ptr>();
}
} catch (const Poco::Exception &E) {
BadRequest(Request, Response, "Invalid configuration portion.");
return; return;
}
if(Storage()->ConfigurationDB().CreateRecord(C)) { if(Storage()->ConfigurationDB().CreateRecord(C)) {
Storage()->ConfigurationDB().GetRecord("id", C.info.id, C); Storage()->ConfigurationDB().GetRecord("id", C.info.id, C);
@@ -208,23 +232,8 @@ namespace OpenWifi{
NewConfig.info.modified = std::time(nullptr); NewConfig.info.modified = std::time(nullptr);
if(!NewConfig.configuration.empty()) { if(!ValidateConfigBlock(NewConfig, Request, Response))
try { return;
for(const auto &i:NewConfig.configuration) {
if(i.name.empty()) {
BadRequest(Request, Response, "The configuration block name must be included.");
return;
}
Poco::JSON::Parser P;
auto T = P.parse(i.configuration).extract<Poco::JSON::Object>();
}
} catch (const Poco::Exception &E) {
BadRequest(Request, Response, "Invalid configuration portion.");
return;
}
Existing.configuration = NewConfig.configuration;
}
if(!NewConfig.variables.empty()) if(!NewConfig.variables.empty())
Existing.variables = NewConfig.variables; Existing.variables = NewConfig.variables;

View File

@@ -11,6 +11,7 @@
#include "RESTAPI_handler.h" #include "RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h" #include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h" #include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI_ProvObjects.h"
namespace OpenWifi { namespace OpenWifi {
class RESTAPI_configurations_handler : public RESTAPIHandler { class RESTAPI_configurations_handler : public RESTAPIHandler {
@@ -34,6 +35,10 @@ namespace OpenWifi {
Poco::Net::HTTPServerResponse &Response); Poco::Net::HTTPServerResponse &Response);
void DoDelete(Poco::Net::HTTPServerRequest &Request, void DoDelete(Poco::Net::HTTPServerRequest &Request,
Poco::Net::HTTPServerResponse &Response); Poco::Net::HTTPServerResponse &Response);
private:
bool ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config,
Poco::Net::HTTPServerRequest &Request,
Poco::Net::HTTPServerResponse &Response);
}; };
} }

View File

@@ -1,53 +1,71 @@
{ {
"type" : "entity", "type": "entity",
"name" : "world", "name": "world",
"children" : [{ "children": [{
"type" : "entity", "type": "entity",
"name" : "root" , "name": "Canada",
"children" : [ { "children": [{
"type" : "entity" , "type": "entity",
"name" : "sub-root1" , "name": "Provinces",
"children" : [{ "children": [{
"type" : "entity" , "type": "entity",
"name" : "sub-1.1" , "name": "Quebec",
"children" : [] , "children": [],
"venues" : [] "venues": [{
}, "type": "venue",
{ "name": "Bell Center",
"type" : "entity" , "venues": []
"name" : "sub-1.2" , },
"children" : [] , {
"venues" : [] "type": "venue",
} "name": "Olymic Statium",
] , "venues": []
"venues" : [{ }
"type" : "venue" , ]
"name" : "sub-r1-v1" , },
"venues" : [] {
}, "type": "entity",
{ "name": "Ontario",
"type" : "venue" , "children": [],
"name" : "sub-r1-v2" , "venues": [{
"venues" : [] "type": "venue",
}] "name": "CN Tower",
}, "venues": []
{ },
"type" : "entity" , {
"name" : "sub-root2" , "type": "venue",
"children" : [] , "name": "Rogers Center",
"venues" : [] "venues": []
} }
] , ]
"venues" : [ { },
"type" : "venue" , {
"name" : "sub-v1" , "type": "entity",
"venues" : [] "name": "BC",
}, "children": [],
{ "venues": [{
"type" : "venue" , "type": "venue",
"name" : "sub-v2" , "name": "BC Place",
"venues" : [] "venues": []
}] },
}], {
"venues" : [] "type": "venue",
} "name": "Stanley Park",
"venues": []
}
]
}
],
"venues": []
}],
"venues": []
},
{
"type": "entity",
"name": "Mexico",
"children": [],
"venues": []
}
],
"venues": []
}