mirror of
https://github.com/emqx/neuron.git
synced 2026-10-29 07:06:32 +08:00
Merge pull request #1097 from eeff/eeff/dev
feat(restful): put global config node setting
This commit is contained in:
@@ -53,6 +53,7 @@ typedef enum {
|
||||
STATE_ADD_GROUP,
|
||||
STATE_ADD_TAG,
|
||||
STATE_ADD_SUBSCRIPTION,
|
||||
STATE_ADD_SETTING,
|
||||
STATE_END,
|
||||
} context_state_e;
|
||||
|
||||
@@ -98,6 +99,7 @@ static int add_group(context_t * ctx,
|
||||
neu_json_get_driver_group_resp_group_t *data);
|
||||
static int add_tag(context_t *ctx, neu_json_add_tags_req_t *data);
|
||||
static int add_subscription(context_t *ctx, neu_json_subscribe_req_t *data);
|
||||
static int add_setting(context_t *ctx, neu_json_node_setting_req_t *data);
|
||||
|
||||
static inline bool is_static_node(const char *name, const char *plugin)
|
||||
{
|
||||
@@ -409,6 +411,28 @@ static void put_context_next(context_t *ctx, neu_reqresp_type_e type,
|
||||
break;
|
||||
}
|
||||
|
||||
ctx->idx = 0;
|
||||
ctx->state = STATE_ADD_SETTING;
|
||||
}
|
||||
// fall through
|
||||
|
||||
case STATE_ADD_SETTING: {
|
||||
neu_resp_error_t * err = data;
|
||||
neu_json_global_config_req_settings_t *settings = ctx->req->settings;
|
||||
|
||||
if (ctx->idx > 0 && 0 != err->error) {
|
||||
// received error response
|
||||
ctx->error = err->error;
|
||||
ctx->state = STATE_END;
|
||||
break;
|
||||
}
|
||||
|
||||
if (ctx->idx < (size_t) settings->n_setting) {
|
||||
NEXT(ctx, add_setting, &settings->settings[ctx->idx]);
|
||||
++ctx->idx;
|
||||
break;
|
||||
}
|
||||
|
||||
ctx->state = STATE_END;
|
||||
break;
|
||||
}
|
||||
@@ -928,6 +952,27 @@ static int add_subscription(context_t *ctx, neu_json_subscribe_req_t *data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int add_setting(context_t *ctx, neu_json_node_setting_req_t *data)
|
||||
{
|
||||
neu_plugin_t *plugin = neu_rest_get_plugin();
|
||||
|
||||
neu_reqresp_head_t header = {
|
||||
.ctx = ctx->aio,
|
||||
.type = NEU_REQ_NODE_SETTING,
|
||||
};
|
||||
|
||||
neu_req_node_setting_t cmd = { 0 };
|
||||
strcpy(cmd.node, data->node);
|
||||
cmd.setting = data->setting; // ownership moved
|
||||
data->setting = NULL;
|
||||
|
||||
if (0 != neu_plugin_op(plugin, header, &cmd)) {
|
||||
return NEU_ERR_IS_BUSY;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handle_get_global_config(nng_aio *aio)
|
||||
{
|
||||
NEU_VALIDATE_JWT(aio);
|
||||
|
||||
@@ -354,6 +354,83 @@ void neu_json_decode_global_config_req_subscriptions_free(
|
||||
free(req_sub);
|
||||
}
|
||||
|
||||
static inline void fini_node_setting(neu_json_node_setting_req_t *req)
|
||||
{
|
||||
free(req->node);
|
||||
free(req->setting);
|
||||
}
|
||||
|
||||
int neu_json_decode_global_config_req_settings(
|
||||
void *json_obj, neu_json_global_config_req_settings_t **result)
|
||||
{
|
||||
json_t *settings_array = json_object_get(json_obj, "settings");
|
||||
if (!json_is_array(settings_array)) {
|
||||
nlog_error("no `settings` array field");
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_global_config_req_settings_t *req = calloc(1, sizeof(*req));
|
||||
if (NULL == req) {
|
||||
nlog_error("calloc fail");
|
||||
return -1;
|
||||
}
|
||||
|
||||
req->n_setting = json_array_size(settings_array);
|
||||
req->settings = calloc(req->n_setting, sizeof(*req->settings));
|
||||
if (NULL == req->settings) {
|
||||
free(req);
|
||||
nlog_error("calloc fail");
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_node_setting_req_t *p = req->settings;
|
||||
for (int i = 0; i < req->n_setting; ++i) {
|
||||
json_t *setting_obj = json_array_get(settings_array, i);
|
||||
json_t *node_obj = json_object_get(setting_obj, "node");
|
||||
|
||||
if (NULL == node_obj) {
|
||||
nlog_error("no settings `node` field");
|
||||
goto error;
|
||||
}
|
||||
|
||||
p->node = strdup(json_string_value(node_obj));
|
||||
if (NULL == p->node) {
|
||||
nlog_error("strdup fail");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (0 != neu_json_dump_key(setting_obj, "params", &p->setting, true)) {
|
||||
free(p->node);
|
||||
nlog_error("no settings `params` field");
|
||||
goto error;
|
||||
}
|
||||
++p;
|
||||
}
|
||||
|
||||
*result = req;
|
||||
return 0;
|
||||
|
||||
error:
|
||||
while (--p >= req->settings) {
|
||||
fini_node_setting(p);
|
||||
}
|
||||
free(req->settings);
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void neu_json_decode_global_config_req_settings_free(
|
||||
neu_json_global_config_req_settings_t *req_settings)
|
||||
{
|
||||
if (req_settings) {
|
||||
for (int i = 0; i < req_settings->n_setting; ++i) {
|
||||
fini_node_setting(&req_settings->settings[i]);
|
||||
}
|
||||
free(req_settings->settings);
|
||||
free(req_settings);
|
||||
}
|
||||
}
|
||||
|
||||
int neu_json_decode_global_config_req(char * buf,
|
||||
neu_json_global_config_req_t **result)
|
||||
{
|
||||
@@ -376,7 +453,10 @@ int neu_json_decode_global_config_req(char * buf,
|
||||
0 == neu_json_decode_global_config_req_tags(json_obj, &req->tags) &&
|
||||
0 ==
|
||||
neu_json_decode_global_config_req_subscriptions(
|
||||
json_obj, &req->subscriptions)) {
|
||||
json_obj, &req->subscriptions) &&
|
||||
0 ==
|
||||
neu_json_decode_global_config_req_settings(json_obj,
|
||||
&req->settings)) {
|
||||
*result = req;
|
||||
} else {
|
||||
ret = -1;
|
||||
@@ -395,6 +475,7 @@ void neu_json_decode_global_config_req_free(neu_json_global_config_req_t *req)
|
||||
neu_json_decode_global_config_req_tags_free(req->tags);
|
||||
neu_json_decode_global_config_req_subscriptions_free(
|
||||
req->subscriptions);
|
||||
neu_json_decode_global_config_req_settings_free(req->settings);
|
||||
free(req);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,11 +50,22 @@ int neu_json_decode_global_config_req_subscriptions(
|
||||
void neu_json_decode_global_config_req_subscriptions_free(
|
||||
neu_json_global_config_req_subscriptions_t *req_sub);
|
||||
|
||||
typedef struct {
|
||||
int n_setting;
|
||||
neu_json_node_setting_req_t *settings;
|
||||
} neu_json_global_config_req_settings_t;
|
||||
|
||||
int neu_json_decode_global_config_req_settings(
|
||||
void *json_obj, neu_json_global_config_req_settings_t **result);
|
||||
void neu_json_decode_global_config_req_settings_free(
|
||||
neu_json_global_config_req_settings_t *req_settings);
|
||||
|
||||
typedef struct {
|
||||
neu_json_get_nodes_resp_t * nodes;
|
||||
neu_json_get_driver_group_resp_t * groups;
|
||||
neu_json_global_config_req_tags_t * tags;
|
||||
neu_json_global_config_req_subscriptions_t *subscriptions;
|
||||
neu_json_global_config_req_settings_t * settings;
|
||||
} neu_json_global_config_req_t;
|
||||
|
||||
int neu_json_decode_global_config_req(char * buf,
|
||||
|
||||
Reference in New Issue
Block a user