MXS-1220: Migrate create/update monitor to JSON API format
The creation and modification of moitor now supports the JSON API conforming format generated by the GET endpoints. Also added tests for creating and altering monitors via the REST API.
This commit is contained in:
parent
9495438f2b
commit
c1968aac2f
@ -55,6 +55,8 @@ static const char PTR_SRV_PROTOCOL[] = PTR_PARAMETERS "/protocol";
|
||||
static const char PTR_SRV_AUTHENTICATOR[] = PTR_PARAMETERS "/authenticator";
|
||||
static const char PTR_SRV_AUTHENTICATOR_OPTIONS[] = PTR_PARAMETERS "/authenticator_options";
|
||||
|
||||
static const char PTR_MON_MODULE[] = "/data/attributes/module";
|
||||
|
||||
static SPINLOCK crt_lock = SPINLOCK_INIT;
|
||||
|
||||
bool runtime_link_server(SERVER *server, const char *target)
|
||||
@ -991,7 +993,7 @@ bool runtime_alter_server_from_json(SERVER* server, json_t* new_json)
|
||||
|
||||
const char* object_relation_types[] =
|
||||
{
|
||||
CN_SERVERS,
|
||||
PTR_RELATIONSHIPS_SERVERS,
|
||||
NULL
|
||||
};
|
||||
|
||||
@ -1012,8 +1014,8 @@ static bool validate_monitor_json(json_t* json)
|
||||
bool rval = false;
|
||||
json_t* value;
|
||||
|
||||
if ((value = json_object_get(json, CN_NAME)) && json_is_string(value) &&
|
||||
(value = json_object_get(json, CN_MODULE)) && json_is_string(value))
|
||||
if ((value = mxs_json_pointer(json, PTR_ID)) && json_is_string(value) &&
|
||||
(value = json_object_get(json, PTR_MON_MODULE)) && json_is_string(value))
|
||||
{
|
||||
set<string> relations;
|
||||
if (extract_relations(json, relations, object_relation_types, object_relation_is_valid))
|
||||
@ -1068,8 +1070,8 @@ MXS_MONITOR* runtime_create_monitor_from_json(json_t* json)
|
||||
|
||||
if (validate_monitor_json(json))
|
||||
{
|
||||
const char* name = json_string_value(json_object_get(json, CN_NAME));
|
||||
const char* module = json_string_value(json_object_get(json, CN_MODULE));
|
||||
const char* name = json_string_value(mxs_json_pointer(json, PTR_ID));
|
||||
const char* module = json_string_value(mxs_json_pointer(json, PTR_MON_MODULE));
|
||||
|
||||
if (runtime_create_monitor(name, module))
|
||||
{
|
||||
@ -1125,15 +1127,15 @@ bool runtime_alter_monitor_from_json(MXS_MONITOR* monitor, json_t* new_json)
|
||||
|
||||
if (object_to_server_relations(monitor->name, old_json.get(), new_json))
|
||||
{
|
||||
rval = true;
|
||||
bool changed = false;
|
||||
json_t* parameters = json_object_get(new_json, CN_PARAMETERS);
|
||||
json_t* old_parameters = json_object_get(old_json.get(), CN_PARAMETERS);
|
||||
json_t* parameters = mxs_json_pointer(new_json, PTR_PARAMETERS);
|
||||
json_t* old_parameters = mxs_json_pointer(old_json.get(), PTR_PARAMETERS);
|
||||
|
||||
ss_dassert(old_parameters);
|
||||
|
||||
if (parameters)
|
||||
{
|
||||
rval = true;
|
||||
const char* key;
|
||||
json_t* value;
|
||||
|
||||
@ -1191,8 +1193,8 @@ bool runtime_alter_service_from_json(SERVICE* service, json_t* new_json)
|
||||
if (object_to_server_relations(service->name, old_json.get(), new_json))
|
||||
{
|
||||
bool changed = false;
|
||||
json_t* parameters = json_object_get(new_json, CN_PARAMETERS);
|
||||
json_t* old_parameters = json_object_get(old_json.get(), CN_PARAMETERS);
|
||||
json_t* parameters = mxs_json_pointer(new_json, PTR_PARAMETERS);
|
||||
json_t* old_parameters = mxs_json_pointer(old_json.get(), PTR_PARAMETERS);
|
||||
|
||||
ss_dassert(old_parameters);
|
||||
|
||||
@ -1223,6 +1225,7 @@ bool runtime_alter_service_from_json(SERVICE* service, json_t* new_json)
|
||||
}
|
||||
else if (paramset.find(key) != paramset.end())
|
||||
{
|
||||
/** Parameter can be altered */
|
||||
if (!runtime_alter_service(service, key, mxs::json_to_string(value).c_str()))
|
||||
{
|
||||
rval = false;
|
||||
|
76
server/core/test/rest-api/test/monitor.js
Normal file
76
server/core/test/rest-api/test/monitor.js
Normal file
@ -0,0 +1,76 @@
|
||||
require("../utils.js")()
|
||||
|
||||
var monitor = {
|
||||
data: {
|
||||
id: "test-monitor",
|
||||
type: "monitors",
|
||||
attributes: {
|
||||
module: "mysqlmon"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe("Creating a Monitor", function() {
|
||||
before(startMaxScale)
|
||||
|
||||
it("create new monitor", function() {
|
||||
return request.post(base_url + "/monitors/", {json: monitor})
|
||||
.should.be.fulfilled
|
||||
})
|
||||
|
||||
it("request monitor", function() {
|
||||
return request.get(base_url + "/monitors/" + monitor.data.id)
|
||||
.should.be.fulfilled
|
||||
});
|
||||
|
||||
it("alter monitor", function() {
|
||||
monitor.data.attributes.parameters.monitor_interval = 1000
|
||||
return request.put(base_url + "/monitors/" + monitor.data.id, {json:monitor})
|
||||
.should.be.fulfilled
|
||||
});
|
||||
|
||||
it("destroy created monitor", function() {
|
||||
return request.delete(base_url + "/monitors/" + monitor.data.id)
|
||||
.should.be.fulfilled
|
||||
});
|
||||
|
||||
after(stopMaxScale)
|
||||
}
|
||||
|
||||
describe("Modifying Existing Monitor", function() {
|
||||
before(startMaxScale)
|
||||
|
||||
it("create new monitor", function() {
|
||||
return request.post(base_url + "/monitors/", {json: monitor})
|
||||
.should.be.fulfilled
|
||||
})
|
||||
|
||||
it("remove relationships from old monitor", function() {
|
||||
|
||||
return request.get(base_url + "/monitors/MySQL-Monitor")
|
||||
.then(function(resp) {
|
||||
var mon = JSON.parse(resp)
|
||||
delete mon.data.relationships
|
||||
return request.put(base_url + "/monitors/MySQL-Monitor", {json: mon})
|
||||
})
|
||||
.should.be.fulfilled
|
||||
});
|
||||
|
||||
it("add relationships to new monitor", function() {
|
||||
|
||||
return request.get(base_url + "/monitors/" + monitor.data.id)
|
||||
.then(function(resp) {
|
||||
var mon = JSON.parse(resp)
|
||||
mon.data.relationships.servers = [
|
||||
{id: "server1", type: "servers"},
|
||||
{id: "server2", type: "servers"},
|
||||
{id: "server3", type: "servers"},
|
||||
{id: "server4", type: "servers"},
|
||||
]
|
||||
return request.put(base_url + "/monitors/" + monitor.data.id, {json: mon})
|
||||
})
|
||||
.should.be.fulfilled
|
||||
});
|
||||
|
||||
after(stopMaxScale)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user