From e4a3f0e6b34aaa5fab338223323eb17a65100aeb Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Fri, 28 Apr 2023 15:17:29 +0800 Subject: [PATCH] feat(restful): add template group add/update api --- plugins/restful/handle.c | 15 ++++++++ plugins/restful/template_handle.c | 59 +++++++++++++++++++++++++++++++ plugins/restful/template_handle.h | 3 ++ 3 files changed, 77 insertions(+) diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index 519786c3..5509fa08 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/group", + }, { .url = "/api/v2/template/tag", }, @@ -393,6 +396,18 @@ static struct neu_http_handler rest_handlers[] = { .url = "/api/v2/template/inst", .value.handler = handle_instantiate_template, }, + { + .method = NEU_HTTP_METHOD_POST, + .type = NEU_HTTP_HANDLER_FUNCTION, + .url = "/api/v2/template/group", + .value.handler = handle_add_template_group, + }, + { + .method = NEU_HTTP_METHOD_PUT, + .type = NEU_HTTP_HANDLER_FUNCTION, + .url = "/api/v2/template/group", + .value.handler = handle_update_template_group, + }, }; 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 0ead7da0..5c07d0e5 100644 --- a/plugins/restful/template_handle.c +++ b/plugins/restful/template_handle.c @@ -321,6 +321,65 @@ void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp) return; } +static inline int +send_template_mod_group_req(nng_aio *aio, neu_reqresp_type_e type, + neu_json_template_mod_group_req_t *req) +{ + neu_plugin_t *plugin = neu_rest_get_plugin(); + + neu_reqresp_head_t header = { + .ctx = aio, + .type = type, + }; + + neu_req_add_template_group_t cmd = { 0 }; + strcpy(cmd.tmpl, req->tmpl); + strcpy(cmd.group, req->group); + cmd.interval = req->interval; + + if (0 != neu_plugin_op(plugin, header, &cmd)) { + return NEU_ERR_IS_BUSY; + } + + return 0; +} + +static inline void process_mod_template_group(nng_aio * aio, + neu_reqresp_type_e type) +{ + NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( + aio, neu_json_template_mod_group_req_t, + neu_json_decode_template_mod_group_req, { + if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_NAME_TOO_LONG, { + neu_http_response(aio, NEU_ERR_GROUP_NAME_TOO_LONG, + result_error); + }); + } else if (req->interval < NEU_GROUP_INTERVAL_LIMIT) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_PARAMETER_INVALID, { + neu_http_response(aio, NEU_ERR_GROUP_PARAMETER_INVALID, + result_error); + }); + } else { + int ret = send_template_mod_group_req(aio, type, req); + if (ret != 0) { + NEU_JSON_RESPONSE_ERROR( + ret, { neu_http_response(aio, ret, result_error); }); + } + } + }) +} + +void handle_add_template_group(nng_aio *aio) +{ + process_mod_template_group(aio, NEU_REQ_ADD_TEMPLATE_GROUP); +} + +void handle_update_template_group(nng_aio *aio) +{ + process_mod_template_group(aio, NEU_REQ_UPDATE_TEMPLATE_GROUP); +} + static int send_template_mod_tags_req(nng_aio *aio, neu_reqresp_type_e type, neu_json_template_mod_tags_req_t *req) { diff --git a/plugins/restful/template_handle.h b/plugins/restful/template_handle.h index 73a93798..2acd1b4e 100644 --- a/plugins/restful/template_handle.h +++ b/plugins/restful/template_handle.h @@ -30,6 +30,9 @@ 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_group(nng_aio *aio); +void handle_update_template_group(nng_aio *aio); + void handle_add_template_tags(nng_aio *aio); void handle_update_template_tags(nng_aio *aio); void handle_del_template_tags(nng_aio *aio);