From b9a5d91fe6650518389ca7933c8723f507403e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Thu, 4 May 2017 09:41:06 +0300 Subject: [PATCH] MXS-1220: Reorganize monitor resource The monitor resource now conforms to the JSON API schema. Added a test case for the individual monitor resource. --- server/core/monitor.cc | 47 +++++++++++-------- .../test/rest-api/test/schema_validation.js | 1 + 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/server/core/monitor.cc b/server/core/monitor.cc index 13ea53081..d67387a84 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -31,6 +31,7 @@ #include #include #include +#include #include "maxscale/config.h" #include "maxscale/externcmd.h" @@ -1497,16 +1498,22 @@ json_t* monitor_parameters_to_json(const MXS_MONITOR* monitor) return rval; } -json_t* monitor_to_json(const MXS_MONITOR* monitor, const char* host) +json_t* monitor_json_data(const MXS_MONITOR* monitor, const char* host) { json_t* rval = json_object(); - json_object_set_new(rval, CN_NAME, json_string(monitor->name)); - json_object_set_new(rval, CN_MODULE, json_string(monitor->module_name)); - json_object_set_new(rval, CN_STATE, json_string(monitor_state_to_string(monitor->state))); + spinlock_acquire(&monitor->lock); + + json_object_set_new(rval, CN_ID, json_string(monitor->name)); + json_object_set_new(rval, CN_TYPE, json_string(CN_MONITORS)); + + json_t* attr = json_object(); + + json_object_set_new(attr, CN_MODULE, json_string(monitor->module_name)); + json_object_set_new(attr, CN_STATE, json_string(monitor_state_to_string(monitor->state))); /** Monitor parameters */ - json_object_set_new(rval, CN_PARAMETERS, monitor_parameters_to_json(monitor)); + json_object_set_new(attr, CN_PARAMETERS, monitor_parameters_to_json(monitor)); if (monitor->handle && monitor->module->diagnostics) { @@ -1514,38 +1521,38 @@ json_t* monitor_to_json(const MXS_MONITOR* monitor, const char* host) if (diag) { - json_object_set_new(rval, "monitor_diagnostics", diag); + json_object_set_new(attr, "monitor_diagnostics", diag); } } json_t* rel = json_object(); - /** Store relationships to other objects */ - string self = host; - self += "/monitors/"; - self += monitor->name; - json_object_set_new(rel, CN_SELF, json_string(self.c_str())); if (monitor->databases) { - json_t* arr = json_array(); + json_t* mon_rel = mxs_json_relationship(host, MXS_JSON_API_SERVERS); for (MXS_MONITOR_SERVERS *db = monitor->databases; db; db = db->next) { - string s = host; - s += "/servers/"; - s += db->server->unique_name; - json_array_append_new(arr, json_string(s.c_str())); + mxs_json_add_relation(mon_rel, db->server->unique_name, CN_SERVERS); } - json_object_set_new(rel, "servers", arr); + json_object_set_new(rel, CN_SERVERS, mon_rel); } - json_object_set_new(rval, "relationships", rel); + spinlock_release(&monitor->lock); + + json_object_set_new(rval, CN_RELATIONSHIPS, rel); + json_object_set_new(rval, CN_ATTRIBUTES, attr); return rval; } +json_t* monitor_to_json(const MXS_MONITOR* monitor, const char* host) +{ + return mxs_json_resource(host, MXS_JSON_API_MONITORS, monitor_json_data(monitor, host)); +} + json_t* monitor_list_to_json(const char* host) { json_t* rval = json_array(); @@ -1554,7 +1561,7 @@ json_t* monitor_list_to_json(const char* host) for (MXS_MONITOR* mon = allMonitors; mon; mon = mon->next) { - json_t *json = monitor_to_json(mon, host); + json_t *json = monitor_json_data(mon, host); if (json) { @@ -1564,7 +1571,7 @@ json_t* monitor_list_to_json(const char* host) spinlock_release(&monLock); - return rval; + return mxs_json_resource(host, MXS_JSON_API_MONITORS, rval); } json_t* monitor_relations_to_server(const SERVER* server, const char* host) diff --git a/server/core/test/rest-api/test/schema_validation.js b/server/core/test/rest-api/test/schema_validation.js index 6ffcfe51d..e91f35619 100644 --- a/server/core/test/rest-api/test/schema_validation.js +++ b/server/core/test/rest-api/test/schema_validation.js @@ -31,6 +31,7 @@ describe("Individual Resources", function(){ "/servers/server1", "/servers/server2", "/services/RW-Split-Router", + "/monitors/MySQL-Monitor", "/sessions/1", ]