MXS-1220: Reorganize server resource
The server resource now conforms to the JSON API schema.
This commit is contained in:
parent
dcf9c8dab6
commit
49b45acd13
@ -52,6 +52,7 @@ extern const char CN_ADMIN_USER[];
|
||||
extern const char CN_ADMIN_SSL_KEY[];
|
||||
extern const char CN_ADMIN_SSL_CERT[];
|
||||
extern const char CN_ADMIN_SSL_CA_CERT[];
|
||||
extern const char CN_ATTRIBUTES[];
|
||||
extern const char CN_AUTHENTICATOR[];
|
||||
extern const char CN_AUTHENTICATOR_OPTIONS[];
|
||||
extern const char CN_AUTH_ALL_SERVERS[];
|
||||
@ -60,6 +61,7 @@ extern const char CN_AUTH_READ_TIMEOUT[];
|
||||
extern const char CN_AUTH_WRITE_TIMEOUT[];
|
||||
extern const char CN_AUTO[];
|
||||
extern const char CN_CONNECTION_TIMEOUT[];
|
||||
extern const char CN_DATA[];
|
||||
extern const char CN_DEFAULT[];
|
||||
extern const char CN_ENABLE_ROOT_USER[];
|
||||
extern const char CN_FEEDBACK[];
|
||||
|
@ -60,6 +60,7 @@ const char CN_ADMIN_USER[] = "admin_user";
|
||||
const char CN_ADMIN_SSL_KEY[] = "admin_ssl_key";
|
||||
const char CN_ADMIN_SSL_CERT[] = "admin_ssl_cert";
|
||||
const char CN_ADMIN_SSL_CA_CERT[] = "admin_ssl_ca_cert";
|
||||
const char CN_ATTRIBUTES[] = "attributes";
|
||||
const char CN_AUTHENTICATOR[] = "authenticator";
|
||||
const char CN_AUTHENTICATOR_OPTIONS[] = "authenticator_options";
|
||||
const char CN_AUTH_ALL_SERVERS[] = "auth_all_servers";
|
||||
@ -68,6 +69,7 @@ const char CN_AUTH_READ_TIMEOUT[] = "auth_read_timeout";
|
||||
const char CN_AUTH_WRITE_TIMEOUT[] = "auth_write_timeout";
|
||||
const char CN_AUTO[] = "auto";
|
||||
const char CN_CONNECTION_TIMEOUT[] = "connection_timeout";
|
||||
const char CN_DATA[] = "data";
|
||||
const char CN_DEFAULT[] = "default";
|
||||
const char CN_ENABLE_ROOT_USER[] = "enable_root_user";
|
||||
const char CN_FEEDBACK[] = "feedback";
|
||||
|
@ -1566,9 +1566,31 @@ json_t* monitor_list_to_json(const char* host)
|
||||
return rval;
|
||||
}
|
||||
|
||||
static json_t* monitor_self_link(const char* host)
|
||||
{
|
||||
json_t* rel_links = json_object();
|
||||
|
||||
string links = host;
|
||||
links += "/monitors/";
|
||||
json_object_set_new(rel_links, CN_SELF, json_string(links.c_str()));
|
||||
|
||||
return rel_links;
|
||||
}
|
||||
|
||||
static void add_monitor_relation(json_t* arr, const MXS_MONITOR* monitor)
|
||||
{
|
||||
json_t* obj = json_object();
|
||||
json_object_set_new(obj, CN_ID, json_string(monitor->name));
|
||||
json_object_set_new(obj, CN_TYPE, json_string(CN_MONITORS));
|
||||
json_array_append_new(arr, obj);
|
||||
}
|
||||
|
||||
json_t* monitor_relations_to_server(const SERVER* server, const char* host)
|
||||
{
|
||||
json_t* arr = json_array();
|
||||
json_t* rel = json_object();
|
||||
json_t* rel_data = json_array();
|
||||
|
||||
json_object_set_new(rel, CN_LINKS, monitor_self_link(host));
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
|
||||
@ -1580,10 +1602,8 @@ json_t* monitor_relations_to_server(const SERVER* server, const char* host)
|
||||
{
|
||||
if (db->server == server)
|
||||
{
|
||||
string m = host;
|
||||
m += "/monitors/";
|
||||
m += mon->name;
|
||||
json_array_append_new(arr, json_string(m.c_str()));
|
||||
add_monitor_relation(rel_data, mon);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1592,5 +1612,7 @@ json_t* monitor_relations_to_server(const SERVER* server, const char* host)
|
||||
|
||||
spinlock_release(&monLock);
|
||||
|
||||
return arr;
|
||||
json_object_set_new(rel, CN_DATA, rel_data);
|
||||
|
||||
return rel;
|
||||
}
|
||||
|
@ -1360,44 +1360,22 @@ bool server_is_mxs_service(const SERVER *server)
|
||||
return rval;
|
||||
}
|
||||
|
||||
json_t* server_list_to_json(const char* host)
|
||||
json_t* server_self_link(const char* host)
|
||||
{
|
||||
json_t* rval = json_array();
|
||||
json_t* links = json_object();
|
||||
string self = host;
|
||||
self += "/servers/";
|
||||
json_object_set_new(links, CN_SELF, json_string(self.c_str()));
|
||||
|
||||
if (rval)
|
||||
{
|
||||
spinlock_acquire(&server_spin);
|
||||
|
||||
for (SERVER* server = allServers; server; server = server->next)
|
||||
{
|
||||
if (SERVER_IS_ACTIVE(server))
|
||||
{
|
||||
json_t* srv_json = server_to_json(server, host);
|
||||
|
||||
if (srv_json == NULL)
|
||||
{
|
||||
json_decref(rval);
|
||||
rval = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
json_array_append_new(rval, srv_json);
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&server_spin);
|
||||
}
|
||||
|
||||
return rval;
|
||||
return links;
|
||||
}
|
||||
|
||||
json_t* server_to_json(const SERVER* server, const char* host)
|
||||
static json_t* server_json_attributes(const SERVER* server)
|
||||
{
|
||||
json_t* rval = json_object();
|
||||
/** Resource attributes */
|
||||
json_t* attr = json_object();
|
||||
|
||||
json_object_set_new(rval, CN_NAME, json_string(server->unique_name));
|
||||
|
||||
/** Store server parameters */
|
||||
/** Store server parameters in attributes */
|
||||
json_t* params = json_object();
|
||||
|
||||
json_object_set_new(params, CN_ADDRESS, json_string(server->name));
|
||||
@ -1419,21 +1397,22 @@ json_t* server_to_json(const SERVER* server, const char* host)
|
||||
json_object_set_new(params, p->name, json_string(p->value));
|
||||
}
|
||||
|
||||
json_object_set_new(rval, CN_PARAMETERS, params);
|
||||
json_object_set_new(attr, CN_PARAMETERS, params);
|
||||
|
||||
|
||||
/** Store general information about the server state */
|
||||
char* stat = server_status(server);
|
||||
json_object_set_new(rval, "status", json_string(stat));
|
||||
json_object_set_new(attr, CN_STATUS, json_string(stat));
|
||||
MXS_FREE(stat);
|
||||
|
||||
if (server->server_string)
|
||||
{
|
||||
json_object_set_new(rval, "version", json_string(server->server_string));
|
||||
json_object_set_new(attr, CN_VERSION_STRING, json_string(server->server_string));
|
||||
}
|
||||
|
||||
json_object_set_new(rval, "node_id", json_integer(server->node_id));
|
||||
json_object_set_new(rval, "master_id", json_integer(server->master_id));
|
||||
json_object_set_new(rval, "replication_depth", json_integer(server->depth));
|
||||
json_object_set_new(attr, "node_id", json_integer(server->node_id));
|
||||
json_object_set_new(attr, "master_id", json_integer(server->master_id));
|
||||
json_object_set_new(attr, "replication_depth", json_integer(server->depth));
|
||||
|
||||
if (server->slaves)
|
||||
{
|
||||
@ -1444,12 +1423,12 @@ json_t* server_to_json(const SERVER* server, const char* host)
|
||||
json_array_append_new(slaves, json_integer(server->slaves[i]));
|
||||
}
|
||||
|
||||
json_object_set_new(rval, "slaves", slaves);
|
||||
json_object_set_new(attr, "slaves", slaves);
|
||||
}
|
||||
|
||||
if (server->rlag >= 0)
|
||||
{
|
||||
json_object_set_new(rval, "replication_lag", json_integer(server->rlag));
|
||||
json_object_set_new(attr, "replication_lag", json_integer(server->rlag));
|
||||
}
|
||||
|
||||
if (server->node_ts > 0)
|
||||
@ -1460,7 +1439,7 @@ json_t* server_to_json(const SERVER* server, const char* host)
|
||||
asctime_r(localtime_r(&tim, &result), timebuf);
|
||||
trim(timebuf);
|
||||
|
||||
json_object_set_new(rval, "last_heartbeat", json_string(timebuf));
|
||||
json_object_set_new(attr, "last_heartbeat", json_string(timebuf));
|
||||
}
|
||||
|
||||
/** Store statistics */
|
||||
@ -1470,39 +1449,67 @@ json_t* server_to_json(const SERVER* server, const char* host)
|
||||
json_object_set_new(stats, "total_connections", json_integer(server->stats.n_connections));
|
||||
json_object_set_new(stats, "active_operations", json_integer(server->stats.n_current_ops));
|
||||
|
||||
json_object_set_new(rval, "statictics", stats);
|
||||
json_object_set_new(attr, "statictics", stats);
|
||||
|
||||
/** Store relationships to other objects */
|
||||
return attr;
|
||||
}
|
||||
|
||||
static json_t* server_to_json_data(const SERVER* server, const char* host)
|
||||
{
|
||||
json_t* rval = json_object();
|
||||
|
||||
/** Add resource identifiers */
|
||||
json_object_set_new(rval, CN_ID, json_string(server->unique_name));
|
||||
json_object_set_new(rval, CN_TYPE, json_string(CN_SERVERS));
|
||||
|
||||
/** Relationships */
|
||||
json_t* rel = json_object();
|
||||
|
||||
string self = host;
|
||||
self += "/servers/";
|
||||
self += server->unique_name;
|
||||
json_object_set_new(rel, CN_SELF, json_string(self.c_str()));
|
||||
|
||||
json_t* arr = service_relations_to_server(server, host);
|
||||
|
||||
if (json_array_size(arr) > 0)
|
||||
{
|
||||
json_object_set_new(rel, CN_SERVICES, arr);
|
||||
}
|
||||
else
|
||||
{
|
||||
json_decref(arr);
|
||||
}
|
||||
|
||||
arr = monitor_relations_to_server(server, host);
|
||||
|
||||
if (json_array_size(arr) > 0)
|
||||
{
|
||||
json_object_set_new(rel, CN_MONITORS, arr);
|
||||
}
|
||||
else
|
||||
{
|
||||
json_decref(arr);
|
||||
}
|
||||
|
||||
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_object_set_new(rval, CN_RELATIONSHIPS, rel);
|
||||
|
||||
/** Attributes */
|
||||
json_object_set_new(rval, CN_ATTRIBUTES, server_json_attributes(server));
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
json_t* server_to_json(const SERVER* server, const char* host)
|
||||
{
|
||||
json_t* rval = json_object();
|
||||
|
||||
/** Top level self link */
|
||||
json_object_set_new(rval, CN_LINKS, server_self_link(host));
|
||||
|
||||
/** Add server data */
|
||||
json_object_set_new(rval, CN_DATA, server_to_json_data(server, host));
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
json_t* server_list_to_json(const char* host)
|
||||
{
|
||||
json_t* data = json_array();
|
||||
|
||||
spinlock_acquire(&server_spin);
|
||||
|
||||
for (SERVER* server = allServers; server; server = server->next)
|
||||
{
|
||||
if (SERVER_IS_ACTIVE(server))
|
||||
{
|
||||
json_array_append_new(data, server_to_json_data(server, host));
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&server_spin);
|
||||
|
||||
json_t* rval = json_object();
|
||||
|
||||
/** Top level self link */
|
||||
json_object_set_new(rval, CN_LINKS, server_self_link(host));
|
||||
|
||||
/** Add server data array */
|
||||
json_object_set_new(rval, CN_DATA, data);
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
@ -2240,7 +2240,8 @@ static bool create_service_config(const SERVICE *service, const char *filename)
|
||||
dprintf(file, "%s=%ld\n", CN_CONNECTION_TIMEOUT, service->conn_idle_timeout);
|
||||
dprintf(file, "%s=%s\n", CN_AUTH_ALL_SERVERS, service->users_from_all ? "true" : "false");
|
||||
dprintf(file, "%s=%s\n", CN_STRIP_DB_ESC, service->strip_db_esc ? "true" : "false");
|
||||
dprintf(file, "%s=%s\n", CN_LOCALHOST_MATCH_WILDCARD_HOST, service->localhost_match_wildcard_host ? "true" : "false");
|
||||
dprintf(file, "%s=%s\n", CN_LOCALHOST_MATCH_WILDCARD_HOST,
|
||||
service->localhost_match_wildcard_host ? "true" : "false");
|
||||
dprintf(file, "%s=%s\n", CN_VERSION_STRING, service->version_string);
|
||||
dprintf(file, "%s=%s\n", CN_WEIGHTBY, service->weightby);
|
||||
dprintf(file, "%s=%s\n", CN_LOG_AUTH_WARNINGS, service->log_auth_warnings ? "true" : "false");
|
||||
@ -2571,17 +2572,32 @@ json_t* service_list_to_json(const char* host)
|
||||
return rval;
|
||||
}
|
||||
|
||||
static void add_service_relation(json_t* arr, const char* host, const SERVICE* service)
|
||||
static void add_service_relation(json_t* arr, const SERVICE* service)
|
||||
{
|
||||
string svc = host;
|
||||
svc += "/services/";
|
||||
svc += service->name;
|
||||
json_array_append_new(arr, json_string(svc.c_str()));
|
||||
json_t* obj = json_object();
|
||||
json_object_set_new(obj, CN_ID, json_string(service->name));
|
||||
json_object_set_new(obj, CN_TYPE, json_string(CN_SERVICES));
|
||||
json_array_append_new(arr, obj);
|
||||
}
|
||||
|
||||
static json_t* service_self_link(const char* host)
|
||||
{
|
||||
json_t* rel_links = json_object();
|
||||
|
||||
string links = host;
|
||||
links += "/services/";
|
||||
json_object_set_new(rel_links, CN_SELF, json_string(links.c_str()));
|
||||
|
||||
return rel_links;
|
||||
}
|
||||
|
||||
json_t* service_relations_to_filter(const MXS_FILTER_DEF* filter, const char* host)
|
||||
{
|
||||
json_t* arr = json_array();
|
||||
json_t* rel = json_object();
|
||||
json_t* rel_data = json_array();
|
||||
|
||||
json_object_set_new(rel, CN_LINKS, service_self_link(host));
|
||||
|
||||
spinlock_acquire(&service_spin);
|
||||
|
||||
for (SERVICE *service = allServices; service; service = service->next)
|
||||
@ -2592,7 +2608,7 @@ json_t* service_relations_to_filter(const MXS_FILTER_DEF* filter, const char* ho
|
||||
{
|
||||
if (service->filters[i] == filter)
|
||||
{
|
||||
add_service_relation(arr, host, service);
|
||||
add_service_relation(rel_data, service);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2601,12 +2617,19 @@ json_t* service_relations_to_filter(const MXS_FILTER_DEF* filter, const char* ho
|
||||
|
||||
spinlock_release(&service_spin);
|
||||
|
||||
return arr;
|
||||
json_object_set_new(rel, CN_DATA, rel_data);
|
||||
|
||||
return rel;
|
||||
}
|
||||
|
||||
|
||||
json_t* service_relations_to_server(const SERVER* server, const char* host)
|
||||
{
|
||||
json_t* arr = json_array();
|
||||
json_t* rel = json_object();
|
||||
json_t* rel_data = json_array();
|
||||
|
||||
json_object_set_new(rel, CN_LINKS, service_self_link(host));
|
||||
|
||||
spinlock_acquire(&service_spin);
|
||||
|
||||
for (SERVICE *service = allServices; service; service = service->next)
|
||||
@ -2617,7 +2640,7 @@ json_t* service_relations_to_server(const SERVER* server, const char* host)
|
||||
{
|
||||
if (ref->server == server && SERVER_REF_IS_ACTIVE(ref))
|
||||
{
|
||||
add_service_relation(arr, host, service);
|
||||
add_service_relation(rel_data, service);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2626,5 +2649,7 @@ json_t* service_relations_to_server(const SERVER* server, const char* host)
|
||||
|
||||
spinlock_release(&service_spin);
|
||||
|
||||
return arr;
|
||||
json_object_set_new(rel, CN_DATA, rel_data);
|
||||
|
||||
return rel;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user