From fec281e89cb9c365479d1ca3d8bd772d609c8324 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Fri, 14 Apr 2023 12:13:12 +0800 Subject: [PATCH] feat(restful): template add tags api --- plugins/restful/handle.c | 9 ++++++ plugins/restful/rest.c | 1 + plugins/restful/template_handle.c | 53 +++++++++++++++++++++++++++++++ plugins/restful/template_handle.h | 2 ++ 4 files changed, 65 insertions(+) diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index b8c87328..003e701a 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -112,6 +112,9 @@ static struct neu_http_handler cors_handler[] = { { .url = "/api/v2/template", }, + { + .url = "/api/v2/template/tag", + }, }; static struct neu_http_handler rest_handlers[] = { @@ -357,6 +360,12 @@ static struct neu_http_handler rest_handlers[] = { .url = "/api/v2/template", .value.handler = handle_get_template, }, + { + .method = NEU_HTTP_METHOD_POST, + .type = NEU_HTTP_HANDLER_FUNCTION, + .url = "/api/v2/template/tag", + .value.handler = handle_add_template_tags, + }, }; void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size) diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index e67a66a6..c0c1cc4a 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -197,6 +197,7 @@ static int dashb_plugin_request(neu_plugin_t * plugin, (neu_resp_get_driver_group_t *) data); break; case NEU_RESP_ADD_TAG: + case NEU_RESP_ADD_TEMPLATE_TAG: handle_add_tags_resp(header->ctx, (neu_resp_add_tag_t *) data); break; case NEU_RESP_UPDATE_TAG: diff --git a/plugins/restful/template_handle.c b/plugins/restful/template_handle.c index 6a445337..6230c5c9 100644 --- a/plugins/restful/template_handle.c +++ b/plugins/restful/template_handle.c @@ -157,6 +157,7 @@ void handle_add_template(nng_aio *aio) } if (0 != ret) { + neu_reqresp_template_fini(&cmd); NEU_JSON_RESPONSE_ERROR( ret, { neu_http_response(aio, ret, result_error); }); } @@ -319,3 +320,55 @@ void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp) neu_resp_get_templates_fini(resp); return; } + +static int send_template_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(); + + int ret = 0; + int i = 0; + + neu_reqresp_head_t header = { + .ctx = aio, + .type = type, + }; + + neu_req_add_template_tag_t cmd = { 0 }; + strcpy(cmd.tmpl, req->tmpl); + strcpy(cmd.group, req->group); + + cmd.tags = calloc(req->n_tag, sizeof(neu_datatag_t)); + if (NULL == cmd.tags) { + return NEU_ERR_EINTERNAL; + } + + for (i = 0; i < req->n_tag; i++) { + cmd.n_tag += 1; + if (0 != (ret = set_tag_by_json(&cmd.tags[i], &req->tags[i]))) { + neu_req_add_template_tag_fini(&cmd); + return ret; + } + } + + if (0 != neu_plugin_op(plugin, header, &cmd)) { + neu_req_add_template_tag_fini(&cmd); + return 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); + 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 8f77e548..a7c72717 100644 --- a/plugins/restful/template_handle.h +++ b/plugins/restful/template_handle.h @@ -30,4 +30,6 @@ void handle_get_template(nng_aio *aio); void handle_get_template_resp(nng_aio *aio, neu_resp_get_template_t *resp); void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp); +void handle_add_template_tags(nng_aio *aio); + #endif