mirror of
https://github.com/emqx/neuron.git
synced 2026-10-22 17:50:36 +08:00
Merge pull request #1239 from eeff/eeff/dev
feat(restful): add template group get api
This commit is contained in:
@@ -414,6 +414,12 @@ static struct neu_http_handler rest_handlers[] = {
|
||||
.url = "/api/v2/template/group",
|
||||
.value.handler = handle_del_template_group,
|
||||
},
|
||||
{
|
||||
.method = NEU_HTTP_METHOD_GET,
|
||||
.type = NEU_HTTP_HANDLER_FUNCTION,
|
||||
.url = "/api/v2/template/group",
|
||||
.value.handler = handle_get_template_group,
|
||||
},
|
||||
};
|
||||
|
||||
void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size)
|
||||
|
||||
@@ -573,3 +573,31 @@ void handle_instantiate_template(nng_aio *aio)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
void handle_get_template_group(nng_aio *aio)
|
||||
{
|
||||
neu_plugin_t *plugin = neu_rest_get_plugin();
|
||||
|
||||
NEU_VALIDATE_JWT(aio);
|
||||
|
||||
neu_reqresp_head_t header = {
|
||||
.ctx = aio,
|
||||
.type = NEU_REQ_GET_TEMPLATE_GROUP,
|
||||
};
|
||||
|
||||
neu_req_get_template_group_t cmd = { 0 };
|
||||
int ret = neu_http_get_param_str(aio, "name", cmd.tmpl, sizeof(cmd.tmpl));
|
||||
if (ret <= 0 || (size_t) ret >= sizeof(cmd.tmpl)) {
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, {
|
||||
neu_http_response(aio, NEU_ERR_PARAM_IS_WRONG, result_error);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ 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_del_template_group(nng_aio *aio);
|
||||
void handle_get_template_group(nng_aio *aio);
|
||||
|
||||
void handle_add_template_tags(nng_aio *aio);
|
||||
void handle_update_template_tags(nng_aio *aio);
|
||||
|
||||
@@ -438,6 +438,25 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
reply(manager, header, &e);
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_GET_TEMPLATE_GROUP: {
|
||||
neu_req_get_template_group_t *cmd =
|
||||
(neu_req_get_template_group_t *) &header[1];
|
||||
neu_resp_error_t e = { 0 };
|
||||
neu_resp_get_group_t resp = { 0 };
|
||||
|
||||
neu_msg_exchange(header);
|
||||
|
||||
e.error = neu_manager_get_template_group(manager, cmd, &resp.groups);
|
||||
if (NEU_ERR_SUCCESS != e.error) {
|
||||
header->type = NEU_RESP_ERROR;
|
||||
reply(manager, header, &e);
|
||||
break;
|
||||
}
|
||||
|
||||
header->type = NEU_RESP_GET_GROUP;
|
||||
reply(manager, header, &resp);
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_ADD_TEMPLATE_TAG: {
|
||||
neu_req_add_template_tag_t *cmd =
|
||||
(neu_req_add_template_tag_t *) &header[1];
|
||||
|
||||
@@ -265,6 +265,47 @@ int neu_manager_del_template_group(neu_manager_t * manager,
|
||||
return neu_template_del_group(tmpl, req->group);
|
||||
}
|
||||
|
||||
static int accumulate_group_info(neu_group_t *grp, void *data)
|
||||
{
|
||||
UT_array ** vec = data;
|
||||
neu_resp_group_info_t info = { 0 };
|
||||
|
||||
info.interval = neu_group_get_interval(grp);
|
||||
info.tag_count = neu_group_tag_size(grp);
|
||||
strncpy(info.name, neu_group_get_name(grp), sizeof(info.name));
|
||||
|
||||
utarray_push_back(*vec, &info);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int neu_manager_get_template_group(neu_manager_t * manager,
|
||||
neu_req_get_template_group_t *req,
|
||||
UT_array ** group_info_p)
|
||||
{
|
||||
neu_template_t *tmpl =
|
||||
neu_template_manager_find(manager->template_manager, req->tmpl);
|
||||
if (NULL == tmpl) {
|
||||
return NEU_ERR_TEMPLATE_NOT_FOUND;
|
||||
}
|
||||
|
||||
UT_array *vec = NULL;
|
||||
UT_icd icd = { sizeof(neu_resp_group_info_t), NULL, NULL, NULL };
|
||||
|
||||
utarray_new(vec, &icd);
|
||||
if (NULL == vec) {
|
||||
return NEU_ERR_EINTERNAL;
|
||||
}
|
||||
|
||||
int ret = neu_template_for_each_group(tmpl, accumulate_group_info, &vec);
|
||||
if (0 == ret) {
|
||||
*group_info_p = vec;
|
||||
} else {
|
||||
utarray_free(vec);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int neu_manager_add_template_tags(neu_manager_t * manager,
|
||||
neu_req_add_template_tag_t *req,
|
||||
uint16_t * index_p)
|
||||
|
||||
@@ -71,6 +71,9 @@ int neu_manager_update_template_group(neu_manager_t * manager,
|
||||
neu_req_update_template_group_t *req);
|
||||
int neu_manager_del_template_group(neu_manager_t * manager,
|
||||
neu_req_del_template_group_t *req);
|
||||
int neu_manager_get_template_group(neu_manager_t * manager,
|
||||
neu_req_get_template_group_t *req,
|
||||
UT_array ** group_info_p);
|
||||
int neu_manager_add_template_tags(neu_manager_t * manager,
|
||||
neu_req_add_template_tag_t *req,
|
||||
uint16_t * index_p);
|
||||
|
||||
Reference in New Issue
Block a user