MXS-1220: Reorganize filter resource

The filter resource now conforms to the JSON API schema. Added a test case
for the individual filter resource.
This commit is contained in:
Markus Mäkelä
2017-05-04 09:48:43 +03:00
parent b9a5d91fe6
commit 432a6d6f28
2 changed files with 22 additions and 25 deletions

View File

@ -30,6 +30,7 @@
#include <maxscale/spinlock.h> #include <maxscale/spinlock.h>
#include <maxscale/service.h> #include <maxscale/service.h>
#include <maxscale/filter.hh> #include <maxscale/filter.hh>
#include <maxscale/json_api.h>
#include "maxscale/config.h" #include "maxscale/config.h"
#include "maxscale/modules.h" #include "maxscale/modules.h"
@ -497,48 +498,43 @@ json_t* filter_parameters_to_json(const MXS_FILTER_DEF* filter)
return rval; return rval;
} }
json_t* filter_to_json(const MXS_FILTER_DEF* filter, const char* host) json_t* filter_json_data(const MXS_FILTER_DEF* filter, const char* host)
{ {
json_t* rval = json_object(); json_t* rval = json_object();
json_object_set_new(rval, CN_NAME, json_string(filter->name)); json_object_set_new(rval, CN_ID, json_string(filter->name));
json_object_set_new(rval, CN_MODULE, json_string(filter->module)); json_object_set_new(rval, CN_TYPE, json_string(CN_FILTERS));
json_object_set_new(rval, CN_PARAMETERS, filter_parameters_to_json(filter));
if (filter->obj && filter->filter) json_t* attr = json_object();
json_object_set_new(attr, CN_MODULE, json_string(filter->module));
json_object_set_new(attr, CN_PARAMETERS, filter_parameters_to_json(filter));
if (filter->obj && filter->filter && filter->obj->diagnostics_json)
{ {
json_t* diag = filter->obj->diagnostics_json(filter->filter, NULL); json_t* diag = filter->obj->diagnostics_json(filter->filter, NULL);
if (diag) if (diag)
{ {
json_object_set_new(rval, "filter_diagnostics", diag); json_object_set_new(attr, "filter_diagnostics", diag);
} }
} }
/** Store relationships to other objects */ /** Store relationships to other objects */
json_t* rel = json_object(); json_t* rel = json_object();
json_object_set_new(rel, CN_SERVICES, service_relations_to_filter(filter, host));
string self = host; json_object_set_new(rval, CN_RELATIONSHIPS, rel);
self += "/filters/"; json_object_set_new(rval, CN_ATTRIBUTES, attr);
self += filter->name;
json_object_set_new(rel, CN_SELF, json_string(self.c_str()));
json_t* arr = service_relations_to_filter(filter, host);
if (json_array_size(arr) > 0)
{
json_object_set_new(rel, "services", arr);
}
else
{
json_decref(arr);
}
json_object_set_new(rval, "relationships", rel);
return rval; return rval;
} }
json_t* filter_to_json(const MXS_FILTER_DEF* filter, const char* host)
{
return mxs_json_resource(host, MXS_JSON_API_FILTERS, filter_json_data(filter, host));
}
json_t* filter_list_to_json(const char* host) json_t* filter_list_to_json(const char* host)
{ {
json_t* rval = json_array(); json_t* rval = json_array();
@ -547,7 +543,7 @@ json_t* filter_list_to_json(const char* host)
for (MXS_FILTER_DEF* f = allFilters; f; f = f->next) for (MXS_FILTER_DEF* f = allFilters; f; f = f->next)
{ {
json_t* json = filter_to_json(f, host); json_t* json = filter_json_data(f, host);
if (json) if (json)
{ {
@ -557,7 +553,7 @@ json_t* filter_list_to_json(const char* host)
spinlock_release(&filter_spin); spinlock_release(&filter_spin);
return rval; return mxs_json_resource(host, MXS_JSON_API_FILTERS, rval);
} }
namespace maxscale namespace maxscale

View File

@ -32,6 +32,7 @@ describe("Individual Resources", function(){
"/servers/server2", "/servers/server2",
"/services/RW-Split-Router", "/services/RW-Split-Router",
"/monitors/MySQL-Monitor", "/monitors/MySQL-Monitor",
"/filters/Hint",
"/sessions/1", "/sessions/1",
] ]