Merge pull request #1198 from eeff/eeff/dev

feat: template del tags api
This commit is contained in:
eeff
2023-04-14 17:24:14 +08:00
committed by GitHub
9 changed files with 246 additions and 5 deletions
+9
View File
@@ -529,6 +529,15 @@ typedef struct {
char ** tags;
} neu_req_del_template_tag_t;
static inline void
neu_req_del_template_tag_fini(neu_req_del_template_tag_t *req)
{
for (uint16_t i = 0; i < req->n_tag; ++i) {
free(req->tags[i]);
}
free(req->tags);
}
typedef struct neu_req_get_template_tag {
char tmpl[NEU_TEMPLATE_NAME_LEN];
char group[NEU_GROUP_NAME_LEN];
+6
View File
@@ -372,6 +372,12 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/template/tag",
.value.handler = handle_update_template_tags,
},
{
.method = NEU_HTTP_METHOD_DELETE,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/template/tag",
.value.handler = handle_del_template_tags,
},
};
void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size)
+52 -5
View File
@@ -321,8 +321,8 @@ void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp)
return;
}
static int send_template_tags_req(nng_aio *aio, neu_reqresp_type_e type,
neu_json_template_mod_tags_req_t *req)
static int send_template_mod_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();
@@ -359,13 +359,47 @@ static int send_template_tags_req(nng_aio *aio, neu_reqresp_type_e type,
return ret;
}
static int send_template_del_tags_req(nng_aio * aio,
neu_json_template_del_tags_req_t *req)
{
int ret = 0;
neu_plugin_t *plugin = neu_rest_get_plugin();
neu_reqresp_head_t header = {
.ctx = aio,
.type = NEU_REQ_DEL_TEMPLATE_TAG,
};
neu_req_del_template_tag_t cmd = { 0 };
strcpy(cmd.tmpl, req->tmpl);
strcpy(cmd.group, req->group);
cmd.n_tag = req->n_tags;
cmd.tags = calloc(req->n_tags, sizeof(char *));
if (NULL == cmd.tags) {
return NEU_ERR_EINTERNAL;
}
for (int i = 0; i < req->n_tags; i++) {
cmd.tags[i] = req->tags[i];
req->tags[i] = NULL; // ownership moved
}
if (ret != neu_plugin_op(plugin, header, &cmd)) {
neu_req_del_template_tag_fini(&cmd);
ret = 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);
send_template_mod_tags_req(aio, NEU_REQ_ADD_TEMPLATE_TAG, req);
if (0 != ret) {
NEU_JSON_RESPONSE_ERROR(
ret, { neu_http_response(aio, ret, result_error); });
@@ -378,8 +412,21 @@ void handle_update_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_UPDATE_TEMPLATE_TAG, req);
int ret = send_template_mod_tags_req(
aio, NEU_REQ_UPDATE_TEMPLATE_TAG, req);
if (0 != ret) {
NEU_JSON_RESPONSE_ERROR(
ret, { neu_http_response(aio, ret, result_error); });
}
})
}
void handle_del_template_tags(nng_aio *aio)
{
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_template_del_tags_req_t,
neu_json_decode_template_del_tags_req, {
int ret = send_template_del_tags_req(aio, req);
if (0 != ret) {
NEU_JSON_RESPONSE_ERROR(
ret, { neu_http_response(aio, ret, result_error); });
+1
View File
@@ -32,5 +32,6 @@ void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp);
void handle_add_template_tags(nng_aio *aio);
void handle_update_template_tags(nng_aio *aio);
void handle_del_template_tags(nng_aio *aio);
#endif
+17
View File
@@ -415,6 +415,23 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
reply(manager, header, &resp);
break;
}
case NEU_REQ_DEL_TEMPLATE_TAG: {
neu_req_del_template_tag_t *cmd =
(neu_req_del_template_tag_t *) &header[1];
neu_resp_error_t resp = { 0 };
resp.error = neu_manager_del_template_tags(manager, cmd);
if (0 == resp.error) {
// TODO: storage
}
neu_req_del_template_tag_fini(cmd);
header->type = NEU_RESP_ERROR;
strcpy(header->receiver, header->sender);
reply(manager, header, &resp);
break;
}
case NEU_REQ_ADD_NODE: {
neu_req_add_node_t *cmd = (neu_req_add_node_t *) &header[1];
int error =
+24
View File
@@ -291,6 +291,30 @@ int neu_manager_update_template_tags(neu_manager_t * manager,
return ret;
}
int neu_manager_del_template_tags(neu_manager_t * manager,
neu_req_del_template_tag_t *req)
{
neu_template_t *tmpl =
neu_template_manager_find(manager->template_manager, req->tmpl);
if (NULL == tmpl) {
return NEU_ERR_TEMPLATE_NOT_FOUND;
}
neu_group_t *grp = neu_template_get_group(tmpl, req->group);
if (NULL == grp) {
return NEU_ERR_GROUP_NOT_EXIST;
}
int i = 0;
for (; i < req->n_tag; ++i) {
// the only possible error is that the tag does not exist
// in which case we just ignore and continue
neu_group_del_tag(grp, req->tags[i]);
}
return 0;
}
UT_array *neu_manager_get_driver_group(neu_manager_t *manager)
{
UT_array *drivers =
+2
View File
@@ -71,6 +71,8 @@ int neu_manager_add_template_tags(neu_manager_t * manager,
int neu_manager_update_template_tags(neu_manager_t * manager,
neu_req_add_template_tag_t *req,
uint16_t * index_p);
int neu_manager_del_template_tags(neu_manager_t * manager,
neu_req_del_template_tag_t *req);
UT_array *neu_manager_get_driver_group(neu_manager_t *manager);
+122
View File
@@ -17,6 +17,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include <string.h>
#include <jansson.h>
#include "json/json.h"
@@ -498,3 +500,123 @@ void neu_json_decode_template_mod_tags_req_free(
free(req->group);
free(req);
}
int neu_json_encode_template_del_tags_req(void *json_object, void *param)
{
int ret = 0;
neu_json_template_del_tags_req_t *req = param;
void * tag_array = neu_json_array();
neu_json_del_tags_req_name_t *p_name = req->tags;
for (int i = 0; i < req->n_tags; i++) {
neu_json_elem_t tag_elems[] = {
{
.name = NULL,
.t = NEU_JSON_STR,
.v.val_str = *p_name,
},
};
tag_array = neu_json_encode_array_value(tag_array, tag_elems,
NEU_JSON_ELEM_SIZE(tag_elems));
p_name++;
}
neu_json_elem_t req_elems[] = { {
.name = "template",
.t = NEU_JSON_STR,
.v.val_str = req->tmpl,
},
{
.name = "group",
.t = NEU_JSON_STR,
.v.val_str = req->group,
},
{
.name = "tags",
.t = NEU_JSON_OBJECT,
.v.val_object = tag_array,
} };
ret = neu_json_encode_field(json_object, req_elems,
NEU_JSON_ELEM_SIZE(req_elems));
return ret;
}
int neu_json_decode_template_del_tags_req(
char *buf, neu_json_template_del_tags_req_t **result)
{
int ret = 0;
void * json_obj = NULL;
neu_json_template_del_tags_req_t *req = NULL;
json_t * names_arr = NULL;
req = calloc(1, sizeof(*req));
if (req == NULL) {
return -1;
}
json_obj = neu_json_decode_new(buf);
if (NULL == json_obj) {
goto error;
}
neu_json_elem_t req_elems[] = { {
.name = "template",
.t = NEU_JSON_STR,
},
{
.name = "group",
.t = NEU_JSON_STR,
} };
ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems),
req_elems);
req->tmpl = req_elems[0].v.val_str;
req->group = req_elems[1].v.val_str;
if (ret != 0) {
goto error;
}
names_arr = json_object_get(json_obj, "tags");
if (!json_is_array(names_arr)) {
goto error;
}
size_t n_tags = json_array_size(names_arr);
req->tags = calloc(n_tags, sizeof(req->tags[0]));
for (size_t i = 0; i < n_tags; i++) {
req->n_tags += 1;
json_t *name = json_array_get(names_arr, i);
if (!json_is_string(name) ||
NULL == (req->tags[i] = strdup(json_string_value(name)))) {
goto error;
}
}
*result = req;
neu_json_decode_free(json_obj);
return 0;
error:
neu_json_decode_template_del_tags_req_free(req);
neu_json_decode_free(json_obj);
return -1;
}
void neu_json_decode_template_del_tags_req_free(
neu_json_template_del_tags_req_t *req)
{
if (NULL == req) {
return;
}
free(req->tmpl);
free(req->group);
for (int i = 0; i < req->n_tags; i++) {
free(req->tags[i]);
}
free(req->tags);
free(req);
}
+13
View File
@@ -90,4 +90,17 @@ int neu_json_decode_template_mod_tags_req(
void neu_json_decode_template_mod_tags_req_free(
neu_json_template_mod_tags_req_t *req);
typedef struct {
char * tmpl;
char * group;
int n_tags;
char **tags;
} neu_json_template_del_tags_req_t;
int neu_json_encode_template_del_tags_req(void *json_object, void *param);
int neu_json_decode_template_del_tags_req(
char *buf, neu_json_template_del_tags_req_t **result);
void neu_json_decode_template_del_tags_req_free(
neu_json_template_del_tags_req_t *req);
#endif