Merge pull request #1301 from eeff/eeff/dev

feat(restful): add get ndriver maps api
This commit is contained in:
eeff
2023-06-02 10:14:00 +08:00
committed by GitHub
9 changed files with 186 additions and 2 deletions
+19 -2
View File
@@ -116,6 +116,8 @@ typedef enum neu_reqresp_type {
NEU_REQ_ADD_NDRIVER_MAP,
NEU_REQ_DEL_NDRIVER_MAP,
NEU_REQ_GET_NDRIVER_MAPS,
NEU_RESP_GET_NDRIVER_MAPS,
NEU_REQ_UPDATE_LOG_LEVEL,
@@ -209,8 +211,10 @@ static const char *neu_reqresp_type_string_t[] = {
[NEU_REQRESP_NODES_STATE] = "NEU_REQRESP_NODES_STATE",
[NEU_REQRESP_NODE_DELETED] = "NEU_REQRESP_NODE_DELETED",
[NEU_REQ_ADD_NDRIVER_MAP] = "NEU_REQ_ADD_NDRIVER_MAP",
[NEU_REQ_DEL_NDRIVER_MAP] = "NEU_REQ_DEL_NDRIVER_MAP",
[NEU_REQ_ADD_NDRIVER_MAP] = "NEU_REQ_ADD_NDRIVER_MAP",
[NEU_REQ_DEL_NDRIVER_MAP] = "NEU_REQ_DEL_NDRIVER_MAP",
[NEU_REQ_GET_NDRIVER_MAPS] = "NEU_REQ_GET_NDRIVER_MAPS",
[NEU_RESP_GET_NDRIVER_MAPS] = "NEU_RESP_GET_NDRIVER_MAPS",
[NEU_REQ_UPDATE_LOG_LEVEL] = "NEU_REQ_UPDATE_LOG_LEVEL",
@@ -646,6 +650,19 @@ typedef struct {
char group[NEU_GROUP_NAME_LEN];
} neu_req_ndriver_map_t;
typedef struct {
char ndriver[NEU_NODE_NAME_LEN];
} neu_req_get_ndriver_maps_t;
typedef struct {
char driver[NEU_NODE_NAME_LEN];
char group[NEU_GROUP_NAME_LEN];
} neu_resp_ndriver_map_info_t;
typedef struct {
UT_array *groups; // array of neu_resp_ndriver_map_info_t
} neu_resp_get_ndriver_maps_t;
typedef struct neu_req_update_log_level {
char node[NEU_NODE_NAME_LEN];
} neu_req_update_log_level_t;
+6
View File
@@ -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)
+62
View File
@@ -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);
}
+3
View File
@@ -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
+5
View File
@@ -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));
+7
View File
@@ -452,6 +452,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
case NEU_RESP_GET_PLUGIN:
case NEU_RESP_GET_TEMPLATE:
case NEU_RESP_GET_TEMPLATES:
case NEU_RESP_GET_NDRIVER_MAPS:
case NEU_RESP_GET_GROUP:
case NEU_RESP_ERROR:
case NEU_REQRESP_TRANS_DATA:
@@ -1391,6 +1392,12 @@ void *neu_msg_gen(neu_reqresp_head_t *header, void *data)
case NEU_REQ_DEL_NDRIVER_MAP:
data_size = sizeof(neu_req_ndriver_map_t);
break;
case NEU_REQ_GET_NDRIVER_MAPS:
data_size = sizeof(neu_req_get_ndriver_maps_t);
break;
case NEU_RESP_GET_NDRIVER_MAPS:
data_size = sizeof(neu_resp_get_ndriver_maps_t);
break;
case NEU_REQ_UPDATE_LOG_LEVEL:
data_size = sizeof(neu_req_update_log_level_t);
break;
+22
View File
@@ -946,6 +946,28 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
reply(manager, header, &error);
break;
}
case NEU_REQ_GET_NDRIVER_MAPS: {
neu_req_get_ndriver_maps_t *cmd =
(neu_req_get_ndriver_maps_t *) &header[1];
neu_resp_error_t e = { 0 };
neu_resp_get_ndriver_maps_t resp = { 0 };
// TODO
(void) cmd;
UT_icd icd = { sizeof(neu_resp_get_ndriver_maps_t), NULL, NULL, NULL };
utarray_new(resp.groups, &icd);
strcpy(header->receiver, header->sender);
if (0 == e.error) {
header->type = NEU_RESP_GET_NDRIVER_MAPS;
reply(manager, header, &resp);
} else {
header->type = NEU_RESP_ERROR;
reply(manager, header, &e);
}
break;
}
case NEU_REQ_UPDATE_LOG_LEVEL:
if (neu_node_manager_find(manager->node_manager, header->receiver) ==
NULL) {
+48
View File
@@ -116,3 +116,51 @@ void neu_json_decode_ndriver_map_free(neu_json_ndriver_map_t *req)
free(req);
}
}
int neu_json_encode_ndriver_group_array(void *json_arr, void *param)
{
neu_json_ndriver_map_group_array_t *resp =
(neu_json_ndriver_map_group_array_t *) param;
for (int i = 0; i < resp->n_group; i++) {
json_t *ob = json_object();
if (0 !=
json_object_set_new(ob, "driver",
json_string(resp->groups[i].driver)) ||
0 !=
json_object_set_new(ob, "group",
json_string(resp->groups[i].group)) ||
0 != json_array_append_new(json_arr, ob)) {
json_decref(ob);
return -1;
}
}
return 0;
}
int neu_json_encode_get_ndriver_maps_resp(void *json_obj, void *param)
{
int ret = 0;
void *group_array = neu_json_array();
if (NULL == group_array) {
return -1;
}
if (0 != neu_json_encode_ndriver_group_array(group_array, param)) {
neu_json_encode_free(group_array);
return -1;
}
neu_json_elem_t resp_elems[] = { {
.name = "groups",
.t = NEU_JSON_OBJECT,
.v.val_object = group_array,
} };
ret = neu_json_encode_field(json_obj, resp_elems,
NEU_JSON_ELEM_SIZE(resp_elems));
return ret;
}
+14
View File
@@ -36,6 +36,20 @@ int neu_json_encode_ndriver_map(void *json_object, void *param);
int neu_json_decode_ndriver_map(char *buf, neu_json_ndriver_map_t **result);
void neu_json_decode_ndriver_map_free(neu_json_ndriver_map_t *req);
typedef struct {
char *driver;
char *group;
} neu_json_ndriver_map_group_t;
typedef struct {
int n_group;
neu_json_ndriver_map_group_t *groups;
} neu_json_ndriver_map_group_array_t;
int neu_json_encode_ndriver_group_array(void *json_obj, void *param);
int neu_json_encode_get_ndriver_maps_resp(void *json_object, void *param);
#ifdef __cplusplus
}
#endif