mirror of
https://github.com/emqx/neuron.git
synced 2026-10-28 22:59:34 +08:00
fix: node setting api
This commit is contained in:
@@ -363,6 +363,7 @@ typedef struct neu_req_get_node_setting {
|
||||
} neu_req_get_node_setting_t;
|
||||
|
||||
typedef struct neu_resp_get_node_setting {
|
||||
char node[NEU_NODE_NAME_LEN];
|
||||
char *setting;
|
||||
} neu_resp_get_node_setting_t;
|
||||
|
||||
|
||||
@@ -90,6 +90,8 @@ int neu_json_encode(void *json_object, char **str);
|
||||
|
||||
int neu_json_dump_key(void *object, const char *key, char **const result,
|
||||
bool must_exist);
|
||||
int neu_json_load_key(void *object, const char *key, const char *input,
|
||||
bool must_exist);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -157,17 +157,16 @@ void handle_set_node_setting(nng_aio *aio)
|
||||
|
||||
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
|
||||
aio, neu_json_node_setting_req_t, neu_json_decode_node_setting_req, {
|
||||
char *config_buf = calloc(req_data_size + 1, sizeof(char));
|
||||
int ret = 0;
|
||||
int ret = 0;
|
||||
neu_reqresp_head_t header = { 0 };
|
||||
neu_req_node_setting_t cmd = { 0 };
|
||||
|
||||
memcpy(config_buf, req_data, req_data_size);
|
||||
header.ctx = aio;
|
||||
header.type = NEU_REQ_NODE_SETTING;
|
||||
strcpy(cmd.node, req->node);
|
||||
cmd.setting = config_buf;
|
||||
ret = neu_plugin_op(plugin, header, &cmd);
|
||||
cmd.setting = req->setting;
|
||||
req->setting = NULL; // ownership moved
|
||||
ret = neu_plugin_op(plugin, header, &cmd);
|
||||
if (ret != 0) {
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
|
||||
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
|
||||
@@ -209,8 +208,24 @@ void handle_get_node_setting(nng_aio *aio)
|
||||
void handle_get_node_setting_resp(nng_aio * aio,
|
||||
neu_resp_get_node_setting_t *setting)
|
||||
{
|
||||
neu_http_ok(aio, setting->setting);
|
||||
char * json_str = NULL;
|
||||
neu_json_get_node_setting_resp_t resp = {
|
||||
.node = setting->node,
|
||||
.setting = setting->setting,
|
||||
};
|
||||
|
||||
if (0 !=
|
||||
neu_json_encode_by_fn(&resp, neu_json_encode_get_node_setting_resp,
|
||||
&json_str)) {
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, {
|
||||
neu_http_response(aio, error_code.error, result_error);
|
||||
})
|
||||
} else {
|
||||
neu_http_ok(aio, json_str);
|
||||
}
|
||||
|
||||
free(setting->setting);
|
||||
free(json_str);
|
||||
}
|
||||
|
||||
void handle_node_ctl(nng_aio *aio)
|
||||
|
||||
@@ -468,6 +468,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
reply(adapter, header, &error);
|
||||
} else {
|
||||
header->type = NEU_RESP_GET_NODE_SETTING;
|
||||
strcpy(resp.node, adapter->name);
|
||||
reply(adapter, header, &resp);
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -527,8 +527,7 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
|
||||
case NEU_REQ_ADD_NODE_EVENT:
|
||||
case NEU_REQ_DEL_NODE_EVENT:
|
||||
case NEU_REQ_NODE_CTL_EVENT:
|
||||
case NEU_REQ_NODE_SETTING_EVENT: {
|
||||
case NEU_REQ_NODE_CTL_EVENT: {
|
||||
if (neu_node_manager_find(manager->node_manager, header->receiver)) {
|
||||
forward_msg(manager, msg, header->receiver);
|
||||
}
|
||||
@@ -593,7 +592,8 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_NODE_SETTING: {
|
||||
case NEU_REQ_NODE_SETTING:
|
||||
case NEU_REQ_NODE_SETTING_EVENT: {
|
||||
neu_req_node_setting_t *cmd = (neu_req_node_setting_t *) &header[1];
|
||||
|
||||
if (neu_node_manager_find(manager->node_manager, header->receiver) ==
|
||||
|
||||
@@ -356,12 +356,19 @@ int neu_json_decode_node_setting_req(char * buf,
|
||||
goto decode_fail;
|
||||
}
|
||||
|
||||
ret = neu_json_dump_key(json_obj, "params", &req->setting, true);
|
||||
if (0 != ret) {
|
||||
goto decode_fail;
|
||||
}
|
||||
|
||||
req->node = req_elems[0].v.val_str;
|
||||
|
||||
*result = req;
|
||||
goto decode_exit;
|
||||
|
||||
decode_fail:
|
||||
free(req->node);
|
||||
free(req->setting);
|
||||
free(req);
|
||||
ret = -1;
|
||||
|
||||
@@ -375,5 +382,27 @@ decode_exit:
|
||||
void neu_json_decode_node_setting_req_free(neu_json_node_setting_req_t *req)
|
||||
{
|
||||
free(req->node);
|
||||
free(req->setting);
|
||||
free(req);
|
||||
}
|
||||
|
||||
int neu_json_encode_get_node_setting_resp(void *json_object, void *param)
|
||||
{
|
||||
int ret = 0;
|
||||
neu_json_get_node_setting_resp_t *resp = param;
|
||||
|
||||
neu_json_elem_t resp_elems[] = { {
|
||||
.name = "node",
|
||||
.t = NEU_JSON_STR,
|
||||
.v.val_str = resp->node,
|
||||
} };
|
||||
|
||||
ret = neu_json_encode_field(json_object, resp_elems,
|
||||
NEU_JSON_ELEM_SIZE(resp_elems));
|
||||
|
||||
if (0 == ret) {
|
||||
ret = neu_json_load_key(json_object, "params", resp->setting, true);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -99,12 +99,20 @@ void neu_json_decode_node_ctl_req_free(neu_json_node_ctl_req_t *req);
|
||||
|
||||
typedef struct {
|
||||
char *node;
|
||||
char *setting;
|
||||
} neu_json_node_setting_req_t;
|
||||
|
||||
int neu_json_decode_node_setting_req(char * buf,
|
||||
neu_json_node_setting_req_t **result);
|
||||
void neu_json_decode_node_setting_req_free(neu_json_node_setting_req_t *req);
|
||||
|
||||
typedef struct {
|
||||
char *node;
|
||||
char *setting;
|
||||
} neu_json_get_node_setting_resp_t;
|
||||
|
||||
int neu_json_encode_get_node_setting_resp(void *json_object, void *param);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -417,6 +417,7 @@ int neu_json_dump_key(void *object, const char *key, char **const result,
|
||||
value = json_object_get(object, key);
|
||||
if (NULL == value) {
|
||||
if (must_exist) {
|
||||
zlog_error(neuron, "json has no key `%s`", key);
|
||||
return -1;
|
||||
} else {
|
||||
*result = NULL;
|
||||
@@ -432,3 +433,34 @@ int neu_json_dump_key(void *object, const char *key, char **const result,
|
||||
json_decref(new);
|
||||
return rv;
|
||||
}
|
||||
|
||||
int neu_json_load_key(void *object, const char *key, const char *input,
|
||||
bool must_exist)
|
||||
{
|
||||
int rv = 0;
|
||||
json_t * input_object = NULL;
|
||||
json_t * value = NULL;
|
||||
json_error_t error = {};
|
||||
|
||||
input_object = json_loads(input, 0, &error);
|
||||
if (NULL == input_object) {
|
||||
zlog_error(neuron,
|
||||
"json load error, line: %d, column: %d, position: %d, info: "
|
||||
"%s",
|
||||
error.line, error.column, error.position, error.text);
|
||||
return -1;
|
||||
}
|
||||
|
||||
value = json_object_get(input_object, key);
|
||||
if (NULL == value) {
|
||||
if (must_exist) {
|
||||
zlog_error(neuron, "json has no key `%s`", key);
|
||||
rv = -1;
|
||||
}
|
||||
} else if (json_object_set(object, key, value) < 0) {
|
||||
rv = -1;
|
||||
}
|
||||
|
||||
json_decref(input_object);
|
||||
return rv;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user