From a863214e9b6c95026eeb6ebd5eac6b1d6b350667 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:46:05 +0800 Subject: [PATCH 1/3] feat: add template del tags req json parser --- src/parser/neu_json_template.c | 122 +++++++++++++++++++++++++++++++++ src/parser/neu_json_template.h | 13 ++++ 2 files changed, 135 insertions(+) diff --git a/src/parser/neu_json_template.c b/src/parser/neu_json_template.c index c8cdce53..a4b947e5 100644 --- a/src/parser/neu_json_template.c +++ b/src/parser/neu_json_template.c @@ -17,6 +17,8 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **/ +#include + #include #include "json/json.h" @@ -498,3 +500,123 @@ void neu_json_decode_template_mod_tags_req_free( free(req->group); free(req); } + +int neu_json_encode_template_del_tags_req(void *json_object, void *param) +{ + int ret = 0; + neu_json_template_del_tags_req_t *req = param; + + void * tag_array = neu_json_array(); + neu_json_del_tags_req_name_t *p_name = req->tags; + for (int i = 0; i < req->n_tags; i++) { + neu_json_elem_t tag_elems[] = { + { + .name = NULL, + .t = NEU_JSON_STR, + .v.val_str = *p_name, + }, + }; + tag_array = neu_json_encode_array_value(tag_array, tag_elems, + NEU_JSON_ELEM_SIZE(tag_elems)); + p_name++; + } + + neu_json_elem_t req_elems[] = { { + .name = "template", + .t = NEU_JSON_STR, + .v.val_str = req->tmpl, + }, + { + .name = "group", + .t = NEU_JSON_STR, + .v.val_str = req->group, + }, + { + .name = "tags", + .t = NEU_JSON_OBJECT, + .v.val_object = tag_array, + } }; + + ret = neu_json_encode_field(json_object, req_elems, + NEU_JSON_ELEM_SIZE(req_elems)); + + return ret; +} + +int neu_json_decode_template_del_tags_req( + char *buf, neu_json_template_del_tags_req_t **result) +{ + int ret = 0; + void * json_obj = NULL; + neu_json_template_del_tags_req_t *req = NULL; + json_t * names_arr = NULL; + + req = calloc(1, sizeof(*req)); + if (req == NULL) { + return -1; + } + + json_obj = neu_json_decode_new(buf); + if (NULL == json_obj) { + goto error; + } + + neu_json_elem_t req_elems[] = { { + .name = "template", + .t = NEU_JSON_STR, + }, + { + .name = "group", + .t = NEU_JSON_STR, + } }; + ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems), + req_elems); + req->tmpl = req_elems[0].v.val_str; + req->group = req_elems[1].v.val_str; + if (ret != 0) { + goto error; + } + + names_arr = json_object_get(json_obj, "tags"); + if (!json_is_array(names_arr)) { + goto error; + } + + size_t n_tags = json_array_size(names_arr); + req->tags = calloc(n_tags, sizeof(req->tags[0])); + for (size_t i = 0; i < n_tags; i++) { + req->n_tags += 1; + json_t *name = json_array_get(names_arr, i); + if (!json_is_string(name) || + NULL == (req->tags[i] = strdup(json_string_value(name)))) { + goto error; + } + } + + *result = req; + neu_json_decode_free(json_obj); + return 0; + +error: + neu_json_decode_template_del_tags_req_free(req); + neu_json_decode_free(json_obj); + return -1; +} + +void neu_json_decode_template_del_tags_req_free( + neu_json_template_del_tags_req_t *req) +{ + if (NULL == req) { + return; + } + + free(req->tmpl); + free(req->group); + + for (int i = 0; i < req->n_tags; i++) { + free(req->tags[i]); + } + free(req->tags); + + free(req); +} diff --git a/src/parser/neu_json_template.h b/src/parser/neu_json_template.h index a27dcc80..e4cb7502 100644 --- a/src/parser/neu_json_template.h +++ b/src/parser/neu_json_template.h @@ -90,4 +90,17 @@ int neu_json_decode_template_mod_tags_req( void neu_json_decode_template_mod_tags_req_free( neu_json_template_mod_tags_req_t *req); +typedef struct { + char * tmpl; + char * group; + int n_tags; + char **tags; +} neu_json_template_del_tags_req_t; + +int neu_json_encode_template_del_tags_req(void *json_object, void *param); +int neu_json_decode_template_del_tags_req( + char *buf, neu_json_template_del_tags_req_t **result); +void neu_json_decode_template_del_tags_req_free( + neu_json_template_del_tags_req_t *req); + #endif From 0e2d11322a993debf784d601e0866ec812fce065 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:48:25 +0800 Subject: [PATCH 2/3] feat(core): manager fulfill del template tags requests --- include/neuron/adapter.h | 9 +++++++++ src/core/manager.c | 17 +++++++++++++++++ src/core/manager_internal.c | 24 ++++++++++++++++++++++++ src/core/manager_internal.h | 2 ++ 4 files changed, 52 insertions(+) diff --git a/include/neuron/adapter.h b/include/neuron/adapter.h index 1c3ef13f..064a9eca 100644 --- a/include/neuron/adapter.h +++ b/include/neuron/adapter.h @@ -529,6 +529,15 @@ typedef struct { char ** tags; } neu_req_del_template_tag_t; +static inline void +neu_req_del_template_tag_fini(neu_req_del_template_tag_t *req) +{ + for (uint16_t i = 0; i < req->n_tag; ++i) { + free(req->tags[i]); + } + free(req->tags); +} + typedef struct neu_req_get_template_tag { char tmpl[NEU_TEMPLATE_NAME_LEN]; char group[NEU_GROUP_NAME_LEN]; diff --git a/src/core/manager.c b/src/core/manager.c index 5a8c3b43..2156b5ae 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -415,6 +415,23 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data) reply(manager, header, &resp); break; } + case NEU_REQ_DEL_TEMPLATE_TAG: { + neu_req_del_template_tag_t *cmd = + (neu_req_del_template_tag_t *) &header[1]; + neu_resp_error_t resp = { 0 }; + + resp.error = neu_manager_del_template_tags(manager, cmd); + if (0 == resp.error) { + // TODO: storage + } + + neu_req_del_template_tag_fini(cmd); + + header->type = NEU_RESP_ERROR; + strcpy(header->receiver, header->sender); + reply(manager, header, &resp); + break; + } case NEU_REQ_ADD_NODE: { neu_req_add_node_t *cmd = (neu_req_add_node_t *) &header[1]; int error = diff --git a/src/core/manager_internal.c b/src/core/manager_internal.c index e356fe9b..ca13a290 100644 --- a/src/core/manager_internal.c +++ b/src/core/manager_internal.c @@ -291,6 +291,30 @@ int neu_manager_update_template_tags(neu_manager_t * manager, return ret; } +int neu_manager_del_template_tags(neu_manager_t * manager, + neu_req_del_template_tag_t *req) +{ + neu_template_t *tmpl = + neu_template_manager_find(manager->template_manager, req->tmpl); + if (NULL == tmpl) { + return NEU_ERR_TEMPLATE_NOT_FOUND; + } + + neu_group_t *grp = neu_template_get_group(tmpl, req->group); + if (NULL == grp) { + return NEU_ERR_GROUP_NOT_EXIST; + } + + int i = 0; + for (; i < req->n_tag; ++i) { + // the only possible error is that the tag does not exist + // in which case we just ignore and continue + neu_group_del_tag(grp, req->tags[i]); + } + + return 0; +} + UT_array *neu_manager_get_driver_group(neu_manager_t *manager) { UT_array *drivers = diff --git a/src/core/manager_internal.h b/src/core/manager_internal.h index 2680ab1d..598983bb 100644 --- a/src/core/manager_internal.h +++ b/src/core/manager_internal.h @@ -71,6 +71,8 @@ int neu_manager_add_template_tags(neu_manager_t * manager, int neu_manager_update_template_tags(neu_manager_t * manager, neu_req_add_template_tag_t *req, uint16_t * index_p); +int neu_manager_del_template_tags(neu_manager_t * manager, + neu_req_del_template_tag_t *req); UT_array *neu_manager_get_driver_group(neu_manager_t *manager); From 6e7985f1739daa5e77b3329be926c5de2303bcf9 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Fri, 14 Apr 2023 17:10:05 +0800 Subject: [PATCH 3/3] feat: template del tags api --- plugins/restful/handle.c | 6 ++++ plugins/restful/template_handle.c | 57 ++++++++++++++++++++++++++++--- plugins/restful/template_handle.h | 1 + 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index eecf66f0..9295b457 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -372,6 +372,12 @@ static struct neu_http_handler rest_handlers[] = { .url = "/api/v2/template/tag", .value.handler = handle_update_template_tags, }, + { + .method = NEU_HTTP_METHOD_DELETE, + .type = NEU_HTTP_HANDLER_FUNCTION, + .url = "/api/v2/template/tag", + .value.handler = handle_del_template_tags, + }, }; void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size) diff --git a/plugins/restful/template_handle.c b/plugins/restful/template_handle.c index 30d77eb5..2b31c302 100644 --- a/plugins/restful/template_handle.c +++ b/plugins/restful/template_handle.c @@ -321,8 +321,8 @@ void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp) return; } -static int send_template_tags_req(nng_aio *aio, neu_reqresp_type_e type, - neu_json_template_mod_tags_req_t *req) +static int send_template_mod_tags_req(nng_aio *aio, neu_reqresp_type_e type, + neu_json_template_mod_tags_req_t *req) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -359,13 +359,47 @@ static int send_template_tags_req(nng_aio *aio, neu_reqresp_type_e type, return ret; } +static int send_template_del_tags_req(nng_aio * aio, + neu_json_template_del_tags_req_t *req) +{ + int ret = 0; + neu_plugin_t *plugin = neu_rest_get_plugin(); + + neu_reqresp_head_t header = { + .ctx = aio, + .type = NEU_REQ_DEL_TEMPLATE_TAG, + }; + + neu_req_del_template_tag_t cmd = { 0 }; + strcpy(cmd.tmpl, req->tmpl); + strcpy(cmd.group, req->group); + + cmd.n_tag = req->n_tags; + cmd.tags = calloc(req->n_tags, sizeof(char *)); + if (NULL == cmd.tags) { + return NEU_ERR_EINTERNAL; + } + + for (int i = 0; i < req->n_tags; i++) { + cmd.tags[i] = req->tags[i]; + req->tags[i] = NULL; // ownership moved + } + + if (ret != neu_plugin_op(plugin, header, &cmd)) { + neu_req_del_template_tag_fini(&cmd); + ret = NEU_ERR_IS_BUSY; + } + + return ret; +} + void handle_add_template_tags(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_template_mod_tags_req_t, neu_json_decode_template_mod_tags_req, { int ret = - send_template_tags_req(aio, NEU_REQ_ADD_TEMPLATE_TAG, req); + send_template_mod_tags_req(aio, NEU_REQ_ADD_TEMPLATE_TAG, req); if (0 != ret) { NEU_JSON_RESPONSE_ERROR( ret, { neu_http_response(aio, ret, result_error); }); @@ -378,8 +412,21 @@ void handle_update_template_tags(nng_aio *aio) NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_template_mod_tags_req_t, neu_json_decode_template_mod_tags_req, { - int ret = - send_template_tags_req(aio, NEU_REQ_UPDATE_TEMPLATE_TAG, req); + int ret = send_template_mod_tags_req( + aio, NEU_REQ_UPDATE_TEMPLATE_TAG, req); + if (0 != ret) { + NEU_JSON_RESPONSE_ERROR( + ret, { neu_http_response(aio, ret, result_error); }); + } + }) +} + +void handle_del_template_tags(nng_aio *aio) +{ + NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( + aio, neu_json_template_del_tags_req_t, + neu_json_decode_template_del_tags_req, { + int ret = send_template_del_tags_req(aio, req); if (0 != ret) { NEU_JSON_RESPONSE_ERROR( ret, { neu_http_response(aio, ret, result_error); }); diff --git a/plugins/restful/template_handle.h b/plugins/restful/template_handle.h index 3a24268a..cfb5e470 100644 --- a/plugins/restful/template_handle.h +++ b/plugins/restful/template_handle.h @@ -32,5 +32,6 @@ void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp); void handle_add_template_tags(nng_aio *aio); void handle_update_template_tags(nng_aio *aio); +void handle_del_template_tags(nng_aio *aio); #endif