diff --git a/include/maxscale/monitor.h b/include/maxscale/monitor.h index 25e657400..4c55775b1 100644 --- a/include/maxscale/monitor.h +++ b/include/maxscale/monitor.h @@ -322,7 +322,7 @@ json_t* monitor_list_to_json(const char* host); * @param server Server to inspect * @param host Hostname of this server * - * @return Array of monitor links + * @return Array of monitor links or NULL if no relations exist */ json_t* monitor_relations_to_server(const SERVER* server, const char* host); diff --git a/include/maxscale/service.h b/include/maxscale/service.h index 9217a3652..ac1574647 100644 --- a/include/maxscale/service.h +++ b/include/maxscale/service.h @@ -341,7 +341,7 @@ json_t* service_listener_to_json(const SERVICE* service, const char* name, const * @param server Server to inspect * @param host Hostname of this server * - * @return Array of service links + * @return Array of service links or NULL if no relations exist */ json_t* service_relations_to_server(const SERVER* server, const char* host); diff --git a/server/core/monitor.cc b/server/core/monitor.cc index cc22d093f..b704c8f18 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -1802,8 +1803,7 @@ json_t* monitor_list_to_json(const char* host) json_t* monitor_relations_to_server(const SERVER* server, const char* host) { - json_t* rel = mxs_json_relationship(host, MXS_JSON_API_MONITORS); - + std::vector names; spinlock_acquire(&monLock); for (MXS_MONITOR* mon = allMonitors; mon; mon = mon->next) @@ -1816,7 +1816,7 @@ json_t* monitor_relations_to_server(const SERVER* server, const char* host) { if (db->server == server) { - mxs_json_add_relation(rel, mon->name, CN_MONITORS); + names.push_back(mon->name); break; } } @@ -1827,6 +1827,19 @@ json_t* monitor_relations_to_server(const SERVER* server, const char* host) spinlock_release(&monLock); + json_t* rel = NULL; + + if (!names.empty()) + { + rel = mxs_json_relationship(host, MXS_JSON_API_MONITORS); + + for (std::vector::iterator it = names.begin(); + it != names.end(); it++) + { + mxs_json_add_relation(rel, it->c_str(), CN_MONITORS); + } + } + return rel; } diff --git a/server/core/server.cc b/server/core/server.cc index 0aac85b8c..0ad8bf10e 100644 --- a/server/core/server.cc +++ b/server/core/server.cc @@ -1502,8 +1502,19 @@ static json_t* server_to_json_data(const SERVER* server, const char* host) /** Relationships */ json_t* rel = json_object(); - json_object_set_new(rel, CN_SERVICES, service_relations_to_server(server, host)); - json_object_set_new(rel, CN_MONITORS, monitor_relations_to_server(server, host)); + json_t* service_rel = service_relations_to_server(server, host); + json_t* monitor_rel = monitor_relations_to_server(server, host); + + if (service_rel) + { + json_object_set_new(rel, CN_SERVICES, service_rel); + } + + if (monitor_rel) + { + json_object_set_new(rel, CN_MONITORS, monitor_rel); + } + json_object_set_new(rval, CN_RELATIONSHIPS, rel); /** Attributes */ json_object_set_new(rval, CN_ATTRIBUTES, server_json_attributes(server)); diff --git a/server/core/service.cc b/server/core/service.cc index 544c111d4..7906a8bc4 100644 --- a/server/core/service.cc +++ b/server/core/service.cc @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -2700,8 +2701,7 @@ json_t* service_relations_to_filter(const MXS_FILTER_DEF* filter, const char* ho json_t* service_relations_to_server(const SERVER* server, const char* host) { - json_t* rel = mxs_json_relationship(host, MXS_JSON_API_SERVICES); - + std::vector names; spinlock_acquire(&service_spin); for (SERVICE *service = allServices; service; service = service->next) @@ -2712,7 +2712,7 @@ json_t* service_relations_to_server(const SERVER* server, const char* host) { if (ref->server == server && SERVER_REF_IS_ACTIVE(ref)) { - mxs_json_add_relation(rel, service->name, CN_SERVICES); + names.push_back(service->name); } } @@ -2721,6 +2721,19 @@ json_t* service_relations_to_server(const SERVER* server, const char* host) spinlock_release(&service_spin); + json_t* rel = NULL; + + if (!names.empty()) + { + rel = mxs_json_relationship(host, MXS_JSON_API_SERVICES); + + for (std::vector::iterator it = names.begin(); + it != names.end(); it++) + { + mxs_json_add_relation(rel, it->c_str(), CN_SERVICES); + } + } + return rel; }