Do not return empty relationships

If no relationships of a particular type are defined for a resource, the
key for that relationship should not be defined.
This commit is contained in:
Markus Mäkelä
2017-10-23 10:31:14 +03:00
parent d371ecb30f
commit 582a65f77c
5 changed files with 47 additions and 10 deletions

View File

@ -322,7 +322,7 @@ json_t* monitor_list_to_json(const char* host);
* @param server Server to inspect * @param server Server to inspect
* @param host Hostname of this server * @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); json_t* monitor_relations_to_server(const SERVER* server, const char* host);

View File

@ -341,7 +341,7 @@ json_t* service_listener_to_json(const SERVICE* service, const char* name, const
* @param server Server to inspect * @param server Server to inspect
* @param host Hostname of this server * @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); json_t* service_relations_to_server(const SERVER* server, const char* host);

View File

@ -25,6 +25,7 @@
#include <set> #include <set>
#include <zlib.h> #include <zlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <vector>
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <mysqld_error.h> #include <mysqld_error.h>
@ -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* monitor_relations_to_server(const SERVER* server, const char* host)
{ {
json_t* rel = mxs_json_relationship(host, MXS_JSON_API_MONITORS); std::vector<std::string> names;
spinlock_acquire(&monLock); spinlock_acquire(&monLock);
for (MXS_MONITOR* mon = allMonitors; mon; mon = mon->next) 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) if (db->server == server)
{ {
mxs_json_add_relation(rel, mon->name, CN_MONITORS); names.push_back(mon->name);
break; break;
} }
} }
@ -1827,6 +1827,19 @@ json_t* monitor_relations_to_server(const SERVER* server, const char* host)
spinlock_release(&monLock); spinlock_release(&monLock);
json_t* rel = NULL;
if (!names.empty())
{
rel = mxs_json_relationship(host, MXS_JSON_API_MONITORS);
for (std::vector<std::string>::iterator it = names.begin();
it != names.end(); it++)
{
mxs_json_add_relation(rel, it->c_str(), CN_MONITORS);
}
}
return rel; return rel;
} }

View File

@ -1502,8 +1502,19 @@ static json_t* server_to_json_data(const SERVER* server, const char* host)
/** Relationships */ /** Relationships */
json_t* rel = json_object(); json_t* rel = json_object();
json_object_set_new(rel, CN_SERVICES, service_relations_to_server(server, host)); json_t* service_rel = service_relations_to_server(server, host);
json_object_set_new(rel, CN_MONITORS, monitor_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); json_object_set_new(rval, CN_RELATIONSHIPS, rel);
/** Attributes */ /** Attributes */
json_object_set_new(rval, CN_ATTRIBUTES, server_json_attributes(server)); json_object_set_new(rval, CN_ATTRIBUTES, server_json_attributes(server));

View File

@ -28,6 +28,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <string> #include <string>
#include <set> #include <set>
#include <vector>
#include <maxscale/service.h> #include <maxscale/service.h>
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
@ -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* service_relations_to_server(const SERVER* server, const char* host)
{ {
json_t* rel = mxs_json_relationship(host, MXS_JSON_API_SERVICES); std::vector<std::string> names;
spinlock_acquire(&service_spin); spinlock_acquire(&service_spin);
for (SERVICE *service = allServices; service; service = service->next) 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)) 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); spinlock_release(&service_spin);
json_t* rel = NULL;
if (!names.empty())
{
rel = mxs_json_relationship(host, MXS_JSON_API_SERVICES);
for (std::vector<std::string>::iterator it = names.begin();
it != names.end(); it++)
{
mxs_json_add_relation(rel, it->c_str(), CN_SERVICES);
}
}
return rel; return rel;
} }