MXS-1220: Add endpoint for set/clear of server status

The server status can now be manipulated via the REST API. Added tests for
the state manipulation. Fixed minor issues in related code.
This commit is contained in:
Markus Mäkelä
2017-06-30 12:57:16 +03:00
parent c189378389
commit 63d2eee0e3
7 changed files with 172 additions and 25 deletions

View File

@ -1622,14 +1622,7 @@ json_t* runtime_get_json_error()
if (errmsg.length())
{
json_t* err = json_object();
json_object_set_new(err, "detail", json_string(errmsg.c_str()));
json_t* arr = json_array();
json_array_append_new(arr, err);
obj = json_object();
json_object_set_new(obj, "errors", arr);
obj = mxs_json_error(errmsg.c_str());
}
return obj;

View File

@ -155,3 +155,17 @@ json_t* mxs_json_self_link(const char* host, const char* path, const char* id)
return links;
}
json_t* mxs_json_error(const char* message)
{
json_t* err = json_object();
json_object_set_new(err, "detail", json_string(message));
json_t* arr = json_array();
json_array_append_new(arr, err);
json_t* obj = json_object();
json_object_set_new(obj, "errors", arr);
return obj;
}

View File

@ -562,6 +562,34 @@ HttpResponse cb_delete_user(const HttpRequest& request)
return HttpResponse(MHD_HTTP_FORBIDDEN, runtime_get_json_error());
}
HttpResponse cb_set_server(const HttpRequest& request)
{
SERVER* server = server_find_by_unique_name(request.uri_part(1).c_str());
int opt = server_map_status(request.get_option("status").c_str());
if (opt)
{
server_set_status(server, opt);
return HttpResponse(MHD_HTTP_NO_CONTENT);
}
return HttpResponse(MHD_HTTP_FORBIDDEN, mxs_json_error("Invalid or missing value for the `status` parameter"));
}
HttpResponse cb_clear_server(const HttpRequest& request)
{
SERVER* server = server_find_by_unique_name(request.uri_part(1).c_str());
int opt = server_map_status(request.get_option("status").c_str());
if (opt)
{
server_clear_status(server, opt);
return HttpResponse(MHD_HTTP_NO_CONTENT);
}
return HttpResponse(MHD_HTTP_FORBIDDEN, mxs_json_error("Invalid or missing value for the `status` parameter"));
}
HttpResponse cb_modulecmd(const HttpRequest& request)
{
std::string module = request.uri_part(2);
@ -699,6 +727,8 @@ public:
"services", ":service", "listeners")));
m_post.push_back(SResource(new Resource(cb_create_user, 2, "users", "inet")));
m_post.push_back(SResource(new Resource(cb_create_user, 2, "users", "unix")));
m_post.push_back(SResource(new Resource(cb_set_server, 3, "servers", ":server", "set")));
m_post.push_back(SResource(new Resource(cb_clear_server, 3, "servers", ":server", "clear")));
/** For all module commands that modify state/data */
m_post.push_back(SResource(new Resource(cb_modulecmd, 4, "maxscale", "modules", ":module", "?")));

View File

@ -5,7 +5,7 @@ describe("Errors", function()
{
before(startMaxScale)
it("error on invalid PUT request", function()
it("error on invalid PATCH request", function()
{
return request.patch(base_url + "/servers/server1", { json: {this_is: "a test"}})
.should.be.rejected

View File

@ -80,3 +80,51 @@ describe("Server Relationships", function() {
after(stopMaxScale)
});
describe("Server Status", function() {
before(startMaxScale)
it("create new server", function() {
return request.post(base_url + "/servers/", {json: server })
.should.be.fulfilled
});
it("set server into maintenance", function() {
return request.post(base_url + "/servers/" + server.data.id + "/set?status=maintenance")
.then(function(resp) {
return request.get(base_url + "/servers/" + server.data.id)
})
.then(function(resp) {
var srv = JSON.parse(resp)
srv.data.attributes.status.should.match(/Maintenance/)
})
});
it("clear maintenance", function() {
return request.post(base_url + "/servers/" + server.data.id + "/clear?status=maintenance")
.then(function(resp) {
return request.get(base_url + "/servers/" + server.data.id)
})
.then(function(resp) {
var srv = JSON.parse(resp)
srv.data.attributes.status.should.not.match(/Maintenance/)
})
});
it("set invalid status value", function() {
return request.post(base_url + "/servers/" + server.data.id + "/set?status=somethingstrange")
.should.be.rejected
});
it("clear invalid status value", function() {
return request.post(base_url + "/servers/" + server.data.id + "/clear?status=somethingstrange")
.should.be.rejected
});
it("destroy server", function() {
return request.delete(base_url + "/servers/" + server.data.id)
.should.be.fulfilled
});
after(stopMaxScale)
});