From 73313e22306b95f9d337ed40b58efffc406484ad Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Thu, 1 Jun 2023 16:57:12 +0800 Subject: [PATCH] feat(restful): add get ndriver maps api --- plugins/restful/handle.c | 6 ++++ plugins/restful/ndriver_handle.c | 62 ++++++++++++++++++++++++++++++++ plugins/restful/ndriver_handle.h | 3 ++ plugins/restful/rest.c | 5 +++ 4 files changed, 76 insertions(+) diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index 7c0d10e1..dac96443 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -442,6 +442,12 @@ static struct neu_http_handler rest_handlers[] = { .url = "/api/v2/ndriver/map", .value.handler = handle_del_ndriver_map, }, + { + .method = NEU_HTTP_METHOD_GET, + .type = NEU_HTTP_HANDLER_FUNCTION, + .url = "/api/v2/ndriver/map", + .value.handler = handle_get_ndriver_maps, + }, }; void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size) diff --git a/plugins/restful/ndriver_handle.c b/plugins/restful/ndriver_handle.c index 47859574..485bca22 100644 --- a/plugins/restful/ndriver_handle.c +++ b/plugins/restful/ndriver_handle.c @@ -84,3 +84,65 @@ void handle_del_ndriver_map(nng_aio *aio) } }) } + +void handle_get_ndriver_maps(nng_aio *aio) +{ + int ret = 0; + neu_plugin_t *plugin = neu_rest_get_plugin(); + + NEU_VALIDATE_JWT(aio); + + neu_req_get_ndriver_maps_t cmd = { 0 }; + if (neu_http_get_param_str(aio, "ndriver", cmd.ndriver, + sizeof(cmd.ndriver)) <= 0 || + strlen(cmd.ndriver) >= NEU_NODE_NAME_LEN) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { + neu_http_response(aio, error_code.error, result_error); + }) + return; + } + + neu_reqresp_head_t header = { + .ctx = aio, + .type = NEU_REQ_GET_NDRIVER_MAPS, + }; + + 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); + }); + } +} + +void handle_get_ndriver_maps_resp(nng_aio * aio, + neu_resp_get_ndriver_maps_t *groups) +{ + neu_json_ndriver_map_group_array_t group_arr = { 0 }; + + group_arr.n_group = utarray_len(groups->groups); + group_arr.groups = calloc(group_arr.n_group, sizeof(*group_arr.groups)); + + int index = 0; + utarray_foreach(groups->groups, neu_resp_ndriver_map_info_t *, group) + { + group_arr.groups[index].driver = group->driver; + group_arr.groups[index].group = group->group; + ++index; + } + + char *result = NULL; + neu_json_encode_by_fn(&group_arr, neu_json_encode_get_ndriver_maps_resp, + &result); + if (result) { + neu_http_ok(aio, result); + } else { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { + neu_http_response(aio, NEU_ERR_EINTERNAL, result_error); + }); + } + + free(result); + free(group_arr.groups); + utarray_free(groups->groups); +} diff --git a/plugins/restful/ndriver_handle.h b/plugins/restful/ndriver_handle.h index 5e2e7e56..05b3ffbd 100644 --- a/plugins/restful/ndriver_handle.h +++ b/plugins/restful/ndriver_handle.h @@ -25,5 +25,8 @@ void handle_add_ndriver_map(nng_aio *aio); void handle_del_ndriver_map(nng_aio *aio); +void handle_get_ndriver_maps(nng_aio *aio); +void handle_get_ndriver_maps_resp(nng_aio * aio, + neu_resp_get_ndriver_maps_t *groups); #endif diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index f222b70d..9bd84e3a 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -31,6 +31,7 @@ #include "global_config_handle.h" #include "group_config_handle.h" #include "handle.h" +#include "ndriver_handle.h" #include "plugin_handle.h" #include "rest.h" #include "rw_handle.h" @@ -232,6 +233,10 @@ static int dashb_plugin_request(neu_plugin_t * plugin, handle_get_templates_resp(header->ctx, (neu_resp_get_templates_t *) data); break; + case NEU_RESP_GET_NDRIVER_MAPS: + handle_get_ndriver_maps_resp(header->ctx, + (neu_resp_get_ndriver_maps_t *) data); + break; default: nlog_fatal("recv unhandle msg: %s", neu_reqresp_type_string(header->type));