summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2023-10-10 17:24:44 +0200
committerJose M. Guisado <jguisado@soleta.eu>2023-10-11 10:28:22 +0200
commit4fca62a8ee24524b65d6b4526e0f8e3397b7e797 (patch)
tree0b4e65ecf0cf6f21295c496d0a38d021ee3b4160 /src
parent6f6372d0e0c19530364b168fb9c843c45a430c5a (diff)
rest: extend /repository/addv1.2.5
POST /repository/add checks validity of the repository ip address specified in the request payload. /repository/add can optionally receive a center id parameter inside its request payload. For backward compatibility, the default center id (1) is used if no center is received inside the request payload. POST /repository/add returns a JSON response payload containing relevant fields from the inserted repository. $ curl -D- \ -X POST \ -H "Authorization: a0e9ab768cbe93dab5b1998e952bcdb7" \ --json '{"name": "helloworld", "ip": "192.168.21.21a", "center": 2}' \ localhost:8888/repository/add HTTP/1.1 400 Bad Request Content-Length: 0 $ curl -D- \ -X POST \ -H "Authorization: a0e9ab768cbe93dab5b1998e952bcdb7" \ --json '{"name": "helloworld", "ip": "192.168.21.21a", "center": 2}' \ localhost:8888/repository/add HTTP/1.1 200 OK Content-Length: 54 {"id": 7, "ip": "192.168.21.21", "name": "helloworld"}
Diffstat (limited to 'src')
-rw-r--r--src/dbi.h1
-rw-r--r--src/rest.c50
2 files changed, 44 insertions, 7 deletions
diff --git a/src/dbi.h b/src/dbi.h
index ab393e7..8db3a9c 100644
--- a/src/dbi.h
+++ b/src/dbi.h
@@ -106,6 +106,7 @@ struct og_room {
};
struct og_repository {
+ unsigned int center;
char name[OG_DB_REPO_NAME_MAXLEN];
char ip[OG_DB_IP_MAXLEN];
};
diff --git a/src/rest.c b/src/rest.c
index ec4ae55..49a2eaa 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -5199,14 +5199,21 @@ static int og_cmd_post_task_add(json_t *element,
}
static int og_cmd_post_repository_add(json_t *element,
- struct og_msg_params *params)
+ struct og_msg_params *params,
+ char *buffer_reply)
{
- struct og_repository repo = {};
+ struct og_buffer og_buffer = {
+ .data = buffer_reply,
+ };
+ struct og_repository repo = {
+ .center = 1,
+ };
const char *key, *msglog;
+ json_t *value, *root;
+ struct in_addr addr;
struct og_dbi *dbi;
dbi_result result;
- json_t *value;
- int err = 0;
+ int err = 0, id;
json_object_foreach(element, key, value) {
if (!strcmp(key, "name")) {
@@ -5219,6 +5226,8 @@ static int og_cmd_post_repository_add(json_t *element,
repo.ip,
sizeof(repo.ip));
params->flags |= OG_REST_PARAM_ADDR;
+ } else if (!strcmp(key, "center")) {
+ err = og_json_parse_uint(value, &repo.center);
}
if (err < 0)
@@ -5229,6 +5238,12 @@ static int og_cmd_post_repository_add(json_t *element,
OG_REST_PARAM_ADDR))
return -1;
+ if (inet_pton(AF_INET, repo.ip, &addr) <= 0) {
+ syslog(LOG_ERR, "invalid server ip address (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
dbi = og_dbi_open(&ogconfig.db);
if (!dbi) {
syslog(LOG_ERR, "cannot open conection database (%s:%d)\n",
@@ -5238,8 +5253,9 @@ static int og_cmd_post_repository_add(json_t *element,
result = dbi_conn_queryf(dbi->conn,
"INSERT INTO repositorios("
- "nombrerepositorio, ip) VALUES ('%s', '%s')",
- repo.name, repo.ip);
+ "nombrerepositorio, ip, idcentro, grupoid) VALUES "
+ "('%s', '%s', %u, 0)",
+ repo.name, repo.ip, repo.center);
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
@@ -5249,6 +5265,26 @@ static int og_cmd_post_repository_add(json_t *element,
og_dbi_close(dbi);
return -1;
}
+
+ root = json_object();
+ if (!root) {
+ syslog(LOG_ERR, "Cannot allocate JSON object (%s:%d)\n",
+ __func__, __LINE__);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return -1;
+ }
+ id = dbi_conn_sequence_last(dbi->conn, NULL);
+ json_object_set_new(root, "id", json_integer(id));
+ json_object_set_new(root, "ip", json_string(repo.ip));
+ json_object_set_new(root, "name", json_string(repo.name));
+ json_object_set_new(root, "center", json_integer(repo.center));
+ if (json_dump_callback(root, og_json_dump_clients, &og_buffer, 0)) {
+ json_decref(root);
+ return -1;
+ }
+
+ json_decref(root);
dbi_result_free(result);
og_dbi_close(dbi);
@@ -6488,7 +6524,7 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_client_bad_request(cli);
goto err_process_rest_payload;
}
- err = og_cmd_post_repository_add(root, &params);
+ err = og_cmd_post_repository_add(root, &params, buf_reply);
} else if (!strncmp(cmd, "repository/delete",
strlen("repository/delete"))) {
if (method != OG_METHOD_POST) {