Skip to content

Commit fbf9bc9

Browse files
committed
add /cubes/ creation api
This feature allows to load an entire topology in the daemon by picking the cubes and their set up in a JSON. Signed-off-by: Riccardo Marchi <riccardo.marchi4@gmail.com>
1 parent 37a0800 commit fbf9bc9

4 files changed

Lines changed: 49 additions & 22 deletions

File tree

src/polycubed/src/rest_server.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ void RestServer::setup_routes() {
171171
router_->get(base + std::string("/cubes/:cubeName"),
172172
bind(&RestServer::get_cube, this));
173173

174+
router_->post(base + std::string("/cubes"),
175+
bind(&RestServer::post_cubes, this));
176+
174177
router_->options(base + std::string("/cubes"),
175178
bind(&RestServer::cubes_help, this));
176179

@@ -428,6 +431,31 @@ void RestServer::get_cube(const Pistache::Rest::Request &request,
428431
}
429432
}
430433

434+
void RestServer::post_cubes(const Pistache::Rest::Request &request,
435+
Pistache::Http::ResponseWriter response) {
436+
logRequest(request);
437+
try {
438+
json j = json::parse(request.body());
439+
logJson(j);
440+
std::vector<Response> resp = {{ErrorTag::kNoContent, nullptr}};
441+
bool error = false;
442+
for (auto &it : j) {
443+
resp = core.get_service_controller(it["service-name"]).get_management_interface()->get_service()
444+
->CreateReplaceUpdate(it["name"], it, false, true);
445+
if (!error && resp[0].error_tag != kCreated) {
446+
Rest::Server::ResponseGenerator::Generate(std::move(resp), std::move(response));
447+
error = true;
448+
}
449+
}
450+
if (!error) {
451+
Rest::Server::ResponseGenerator::Generate(std::move(resp), std::move(response));
452+
}
453+
} catch (const std::runtime_error &e) {
454+
logger->error("{0}", e.what());
455+
response.send(Pistache::Http::Code::Bad_Request, e.what());
456+
}
457+
}
458+
431459
void RestServer::cubes_help(const Pistache::Rest::Request &request,
432460
Pistache::Http::ResponseWriter response) {
433461
json j = json::object();

src/polycubed/src/rest_server.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class RestServer {
8585
void get_cube(const Pistache::Rest::Request &request,
8686
Pistache::Http::ResponseWriter response);
8787

88+
void post_cubes(const Pistache::Rest::Request &request,
89+
Pistache::Http::ResponseWriter response);
90+
8891
void cubes_help(const Pistache::Rest::Request &request,
8992
Pistache::Http::ResponseWriter response);
9093

src/polycubed/src/server/Resources/Endpoint/Service.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ void Service::ClearCubes() {
6767
}
6868
}
6969

70-
void Service::CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
71-
ResponseWriter response, bool update,
72-
bool initialization) {
70+
std::vector<Response>
71+
Service::CreateReplaceUpdate(const std::string &name, nlohmann::json &body, bool update, bool initialization) {
7372
if (update || !ServiceController::exists_cube(name)) {
7473
auto op = OperationType(update, initialization);
7574
auto k = ListKeyValues{};
@@ -80,9 +79,7 @@ void Service::CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
8079

8180
auto body_errors = BodyValidate(name, k, jbody, initialization);
8281
if (!body_errors.empty()) {
83-
Server::ResponseGenerator::Generate(std::move(body_errors),
84-
std::move(response));
85-
return;
82+
return std::move(body_errors);
8683
}
8784

8885
auto resp = WriteValue(name, body, k, op);
@@ -91,12 +88,9 @@ void Service::CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
9188
resp.error_tag == ErrorTag::kNoContent)) {
9289
cube_names_.AddValue(name);
9390
}
94-
Server::ResponseGenerator::Generate(std::vector<Response>{resp},
95-
std::move(response));
91+
return std::vector<Response>{resp};
9692
} else {
97-
Server::ResponseGenerator::Generate(
98-
std::vector<Response>{{ErrorTag::kDataExists, nullptr}},
99-
std::move(response));
93+
return std::vector<Response>{{ErrorTag::kDataExists, nullptr}};
10094
}
10195
}
10296

@@ -127,8 +121,8 @@ void Service::post_body(const Request &request, ResponseWriter response) {
127121
return;
128122
}
129123

130-
CreateReplaceUpdate(body["name"].get<std::string>(), body,
131-
std::move(response), false, true);
124+
auto resp = CreateReplaceUpdate(body["name"].get<std::string>(), body, false, true);
125+
Server:: ResponseGenerator::Generate(std::move(resp), std::move(response));
132126
}
133127

134128
void Service::post(const Request &request, ResponseWriter response) {
@@ -149,7 +143,8 @@ void Service::post(const Request &request, ResponseWriter response) {
149143
}
150144
}
151145

152-
CreateReplaceUpdate(name, body, std::move(response), false, true);
146+
auto resp = CreateReplaceUpdate(name, body, false, true);
147+
Server:: ResponseGenerator::Generate(std::move(resp), std::move(response));
153148
}
154149

155150
void Service::put(const Request &request, ResponseWriter response) {
@@ -161,7 +156,8 @@ void Service::put(const Request &request, ResponseWriter response) {
161156
body = nlohmann::json::parse(request.body());
162157
}
163158
body["name"] = name;
164-
CreateReplaceUpdate(name, body, std::move(response), false, true);
159+
auto resp = CreateReplaceUpdate(name, body, false, true);
160+
Server:: ResponseGenerator::Generate(std::move(resp), std::move(response));
165161
}
166162

167163
void Service::patch(const Request &request, ResponseWriter response) {
@@ -173,7 +169,8 @@ void Service::patch(const Request &request, ResponseWriter response) {
173169
body = nlohmann::json::parse(request.body());
174170
}
175171
body["name"] = name;
176-
CreateReplaceUpdate(name, body, std::move(response), true, false);
172+
auto resp = CreateReplaceUpdate(name, body, true, false);
173+
Server:: ResponseGenerator::Generate(std::move(resp), std::move(response));
177174
}
178175

179176
void Service::del(const Pistache::Rest::Request &request,
@@ -201,8 +198,8 @@ void Service::patch_body(const Request &request, ResponseWriter response) {
201198
std::move(response));
202199
return;
203200
}
204-
CreateReplaceUpdate(body["name"].get<std::string>(), body,
205-
std::move(response), true, false);
201+
auto resp = CreateReplaceUpdate(body["name"].get<std::string>(), body, true, false);
202+
Server:: ResponseGenerator::Generate(std::move(resp), std::move(response));
206203
}
207204

208205
void Service::options_body(const Request &request, ResponseWriter response) {

src/polycubed/src/server/Resources/Endpoint/Service.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@ class Service : public ParentResource, public Body::Service {
5656

5757
virtual Response ReadHelp() = 0;
5858

59+
std::vector<Response> CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
60+
bool update, bool initialization);
61+
5962
private:
6063
const std::string body_rest_endpoint_;
6164
Validators::InSetValidator cube_names_;
6265

63-
void CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
64-
ResponseWriter response, bool replace,
65-
bool initialization);
66-
6766
nlohmann::json getServiceKeys() const;
6867

6968
void post(const Request &request, ResponseWriter response) final;

0 commit comments

Comments
 (0)