MXS-1220: Create corrent relation links

When a resource has a relation to another resource, it should be expressed
as a working link to the resource. By passing the hostname of the server
to the functions, we are able to generate working relation links.
This commit is contained in:
Markus Mäkelä 2017-04-19 10:15:24 +03:00 committed by Markus Mäkelä
parent 978af12a91
commit 5b9c276123
10 changed files with 54 additions and 29 deletions

View File

@ -254,17 +254,20 @@ MXS_FILTER* filter_def_get_instance(const MXS_FILTER_DEF* filter_def);
* @brief Convert a filter to JSON
*
* @param filter Filter to convert
* @param host Hostname of this server
*
* @return Filter converted to JSON format
*/
json_t* filter_to_json(const MXS_FILTER_DEF* filter);
json_t* filter_to_json(const MXS_FILTER_DEF* filter, const char* host);
/**
* @brief Convert all filters into JSON
*
* @param host Hostname of this server
*
* @return A JSON array containing all filters
*/
json_t* filter_list_to_json();
json_t* filter_list_to_json(const char* host);
void dprintAllFilters(DCB *);
void dprintFilter(DCB *, const MXS_FILTER_DEF *);

View File

@ -278,16 +278,19 @@ void mon_hangup_failed_servers(MXS_MONITOR *monitor);
* @brief Convert monitor to JSON
*
* @param monitor Monitor to convert
* @param host Hostname of this server
*
* @return JSON representation of the monitor
*/
json_t* monitor_to_json(const MXS_MONITOR* monitor);
json_t* monitor_to_json(const MXS_MONITOR* monitor, const char* host);
/**
* @brief Convert all monitors to JSON
*
* @param host Hostname of this server
*
* @return JSON array containing all monitors
*/
json_t* monitor_list_to_json();
json_t* monitor_list_to_json(const char* host);
MXS_END_DECLS

View File

@ -270,17 +270,20 @@ bool server_is_mxs_service(const SERVER *server);
* @brief Convert a server to JSON format
*
* @param server Server to convert
* @param host Hostname of this server
*
* @return JSON representation of server or NULL if an error occurred
*/
json_t* server_to_json(const SERVER* server);
json_t* server_to_json(const SERVER* server, const char* host);
/**
* @brief Convert all servers into JSON format
*
* @param host Hostname of this server
*
* @return JSON array of servers or NULL if an error occurred
*/
json_t* server_list_to_json();
json_t* server_list_to_json(const char* host);
extern int server_free(SERVER *server);
extern SERVER *server_find_by_unique_name(const char *name);

View File

@ -283,17 +283,20 @@ void service_print_users(DCB *, const SERVICE *);
* @brief Convert a service to JSON
*
* @param service Service to convert
* @param host Hostname of this server
*
* @return JSON representation of the service
*/
json_t* service_to_json(const SERVICE* service);
json_t* service_to_json(const SERVICE* service, const char* host);
/**
* @brief Convert all services to JSON
*
* @param host Hostname of this server
*
* @return A JSON array with all services
*/
json_t* service_list_to_json();
json_t* service_list_to_json(const char* host);
void dprintAllServices(DCB *dcb);
void dprintService(DCB *dcb, SERVICE *service);

View File

@ -417,9 +417,10 @@ void session_clear_stmt(MXS_SESSION *session);
* @brief Convert a session to JSON
*
* @param session Session to convert
* @param host Hostname of this server
*
* @return New JSON object or NULL on error
*/
json_t* session_to_json(const MXS_SESSION *session);
json_t* session_to_json(const MXS_SESSION *session, const char* host);
MXS_END_DECLS

View File

@ -477,7 +477,7 @@ filter_upstream(MXS_FILTER_DEF *filter, MXS_FILTER_SESSION *fsession, MXS_UPSTRE
return me;
}
json_t* filter_to_json(const MXS_FILTER_DEF* filter)
json_t* filter_to_json(const MXS_FILTER_DEF* filter, const char* host)
{
json_t* rval = json_object();
json_object_set_new(rval, "name", json_string(filter->name));
@ -508,7 +508,7 @@ json_t* filter_to_json(const MXS_FILTER_DEF* filter)
return rval;
}
json_t* filter_list_to_json()
json_t* filter_list_to_json(const char* host)
{
json_t* rval = json_array();
@ -516,7 +516,7 @@ json_t* filter_list_to_json()
for (MXS_FILTER_DEF* f = allFilters; f; f = f->next)
{
json_t* json = filter_to_json(f);
json_t* json = filter_to_json(f, host);
if (json)
{

View File

@ -1539,7 +1539,7 @@ static const char* monitor_state_to_string(int state)
}
}
json_t* monitor_to_json(const MXS_MONITOR* monitor)
json_t* monitor_to_json(const MXS_MONITOR* monitor, const char* host)
{
json_t* rval = json_object();
@ -1559,7 +1559,8 @@ json_t* monitor_to_json(const MXS_MONITOR* monitor)
for (MXS_MONITOR_SERVERS *db = monitor->databases; db; db = db->next)
{
string s = "/servers/";
string s = host;
s += "/servers/";
s += db->server->unique_name;
json_array_append_new(arr, json_string(s.c_str()));
}
@ -1581,7 +1582,7 @@ json_t* monitor_to_json(const MXS_MONITOR* monitor)
return rval;
}
json_t* monitor_list_to_json()
json_t* monitor_list_to_json(const char* host)
{
json_t* rval = json_array();
@ -1589,7 +1590,7 @@ json_t* monitor_list_to_json()
for (MXS_MONITOR* mon = allMonitors; mon; mon = mon->next)
{
json_t *json = monitor_to_json(mon);
json_t *json = monitor_to_json(mon, host);
if (json)
{

View File

@ -442,7 +442,7 @@ dprintAllServers(DCB *dcb)
void
dprintAllServersJson(DCB *dcb)
{
json_t* all_servers = server_list_to_json();
json_t* all_servers = server_list_to_json("");
char* dump = json_dumps(all_servers, JSON_INDENT(4));
dcb_printf(dcb, "%s", dump);
MXS_FREE(dump);
@ -1369,7 +1369,7 @@ bool server_is_mxs_service(const SERVER *server)
return rval;
}
json_t* server_list_to_json()
json_t* server_list_to_json(const char* host)
{
json_t* rval = json_array();
@ -1379,7 +1379,7 @@ json_t* server_list_to_json()
for (SERVER* server = allServers; server; server = server->next)
{
json_t* srv_json = server_to_json(server);
json_t* srv_json = server_to_json(server, host);
if (srv_json == NULL)
{
@ -1397,7 +1397,7 @@ json_t* server_list_to_json()
return rval;
}
json_t* server_to_json(const SERVER* server)
json_t* server_to_json(const SERVER* server, const char* host)
{
// TODO: Add error checks
json_t* rval = json_object();

View File

@ -2380,7 +2380,7 @@ static const char* service_state_to_string(int state)
}
}
json_t* service_to_json(const SERVICE* service)
json_t* service_to_json(const SERVICE* service, const char* host)
{
spinlock_acquire(&service->spin);
// TODO: Handle errors
@ -2415,7 +2415,8 @@ json_t* service_to_json(const SERVICE* service)
for (int i = 0; i < service->n_filters; i++)
{
string filter = "/filters/";
string filter = host;
filter += "/filters/";
filter += service->filters[i]->name;
json_array_append_new(arr, json_string(filter.c_str()));
}
@ -2454,7 +2455,8 @@ json_t* service_to_json(const SERVICE* service)
{
if (SERVER_REF_IS_ACTIVE(ref))
{
string s = "/servers/";
string s = host;
s += "/servers/";
s += ref->server->unique_name;
json_array_append_new(arr, json_string(s.c_str()));
}
@ -2476,7 +2478,7 @@ json_t* service_to_json(const SERVICE* service)
return rval;
}
json_t* service_list_to_json()
json_t* service_list_to_json(const char* host)
{
json_t* rval = json_array();
@ -2484,7 +2486,7 @@ json_t* service_list_to_json()
for (SERVICE *service = allServices; service; service = service->next)
{
json_t* svc = service_to_json(service);
json_t* svc = service_to_json(service, host);
if (svc)
{

View File

@ -35,6 +35,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <maxscale/alloc.h>
#include <maxscale/atomic.h>
@ -50,6 +51,8 @@
#include "maxscale/session.h"
#include "maxscale/filter.h"
using std::string;
/** Global session id counter. Must be updated atomically. Value 0 is reserved for
* dummy/unused sessions.
*/
@ -1000,13 +1003,17 @@ uint32_t session_get_next_id()
return atomic_add_uint32(&next_session_id, 1);
}
json_t* session_to_json(const MXS_SESSION *session)
json_t* session_to_json(const MXS_SESSION *session, const char *host)
{
json_t* rval = json_object();
json_object_set_new(rval, "id", json_integer(session->ses_id));
json_object_set_new(rval, "state", json_string(session_state(session->state)));
json_object_set_new(rval, "service", json_string(session->service->name));
string svc = host;
svc += "/services/";
svc += session->service->name;
json_object_set_new(rval, "service", json_string(svc.c_str()));
if (session->client_dcb->user)
{
@ -1039,8 +1046,10 @@ json_t* session_to_json(const MXS_SESSION *session)
for (int i = 0; i < session->n_filters; i++)
{
json_array_append_new(filters, json_string(session->filters[i].filter->name));
// TODO: Add diagnostic output from filters
string fil = host;
fil += "/filters/";
fil += session->filters[i].filter->name;
json_array_append_new(filters, json_string(fil.c_str()));
}
json_object_set_new(rval, "filters", filters);