Merge pull request #1307 from eeff/eeff/dev

feat(restful): add get ndriver tags api
This commit is contained in:
eeff
2023-06-06 10:38:28 +08:00
committed by GitHub
13 changed files with 354 additions and 9 deletions
+9
View File
@@ -125,6 +125,9 @@ static struct neu_http_handler cors_handler[] = {
{
.url = "/api/v2/ndriver/map",
},
{
.url = "/api/v2/ndriver/tag",
},
};
static struct neu_http_handler rest_handlers[] = {
@@ -448,6 +451,12 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/ndriver/map",
.value.handler = handle_get_ndriver_maps,
},
{
.method = NEU_HTTP_METHOD_GET,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/ndriver/tag",
.value.handler = handle_get_ndriver_tags,
},
};
void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size)
+85 -3
View File
@@ -93,9 +93,9 @@ void handle_get_ndriver_maps(nng_aio *aio)
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) {
ret = neu_http_get_param_str(aio, "ndriver", cmd.ndriver,
sizeof(cmd.ndriver));
if (ret <= 0 || (size_t) ret >= sizeof(cmd.ndriver)) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, {
neu_http_response(aio, error_code.error, result_error);
})
@@ -146,3 +146,85 @@ void handle_get_ndriver_maps_resp(nng_aio * aio,
free(group_arr.groups);
utarray_free(groups->groups);
}
void handle_get_ndriver_tags(nng_aio *aio)
{
int ret = 0;
neu_plugin_t *plugin = neu_rest_get_plugin();
NEU_VALIDATE_JWT(aio);
neu_req_get_ndriver_tags_t cmd = { 0 };
ret = neu_http_get_param_str(aio, "ndriver", cmd.ndriver,
sizeof(cmd.ndriver));
if (ret <= 0 || (size_t) ret >= sizeof(cmd.ndriver)) {
ret = NEU_ERR_PARAM_IS_WRONG;
goto end;
}
ret = neu_http_get_param_str(aio, "driver", cmd.driver, sizeof(cmd.driver));
if (ret <= 0 || (size_t) ret >= sizeof(cmd.driver)) {
ret = NEU_ERR_PARAM_IS_WRONG;
goto end;
}
ret = neu_http_get_param_str(aio, "group", cmd.group, sizeof(cmd.group));
if (ret <= 0 || (size_t) ret >= sizeof(cmd.group)) {
ret = NEU_ERR_PARAM_IS_WRONG;
goto end;
}
neu_reqresp_head_t header = {
.ctx = aio,
.type = NEU_REQ_GET_NDRIVER_TAGS,
};
if (0 != (ret = neu_plugin_op(plugin, header, &cmd))) {
ret = NEU_ERR_IS_BUSY;
}
end:
if (0 != ret) {
NEU_JSON_RESPONSE_ERROR(ret,
{ neu_http_response(aio, ret, result_error); });
}
}
void handle_get_ndriver_tags_resp(nng_aio * aio,
neu_resp_get_ndriver_tags_t *tags)
{
char * result = NULL;
neu_json_ndriver_tag_array_t tags_res = { 0 };
tags_res.len = utarray_len(tags->tags);
tags_res.data = calloc(tags_res.len, sizeof(tags_res.data[0]));
if (NULL == tags_res.data) {
goto end;
}
for (int i = 0; i < tags_res.len; ++i) {
neu_ndriver_tag_t *tag = utarray_eltptr(tags->tags, (size_t) i);
tags_res.data[i].name = tag->name;
tags_res.data[i].address = tag->address;
tags_res.data[i].attribute = tag->attribute;
tags_res.data[i].type = tag->type;
tags_res.data[i].params = tag->params;
}
neu_json_encode_by_fn(&tags_res, neu_json_encode_get_ndriver_tags_resp,
&result);
end:
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(tags_res.data);
utarray_free(tags->tags);
}
+4
View File
@@ -29,4 +29,8 @@ void handle_get_ndriver_maps(nng_aio *aio);
void handle_get_ndriver_maps_resp(nng_aio * aio,
neu_resp_get_ndriver_maps_t *groups);
void handle_get_ndriver_tags(nng_aio *aio);
void handle_get_ndriver_tags_resp(nng_aio * aio,
neu_resp_get_ndriver_tags_t *tags);
#endif
+4
View File
@@ -237,6 +237,10 @@ static int dashb_plugin_request(neu_plugin_t * plugin,
handle_get_ndriver_maps_resp(header->ctx,
(neu_resp_get_ndriver_maps_t *) data);
break;
case NEU_RESP_GET_NDRIVER_TAGS:
handle_get_ndriver_tags_resp(header->ctx,
(neu_resp_get_ndriver_tags_t *) data);
break;
default:
nlog_fatal("recv unhandle msg: %s",
neu_reqresp_type_string(header->type));