summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJavier Sánchez Parra <jsanchez@soleta.eu>2022-06-08 17:24:33 +0200
committerJavier Sánchez Parra <jsanchez@soleta.eu>2022-07-01 12:09:16 +0200
commit86ccc3c2e837762363559d5aab2fcb3a9202fa6b (patch)
treeeb445a9051e3b0db03888aad3f11040e43d4c8d2 /src
parent9ecf638ba28ebcdecac69b04518aa67b576e30dc (diff)
#915 Add POST /repository/add
This method adds a new repository to the database. Request: POST /repository/add { "name": "Repository 1", "ip": "192.168.56.10" } Response: 200 OK
Diffstat (limited to 'src')
-rw-r--r--src/json.h5
-rw-r--r--src/rest.c66
2 files changed, 71 insertions, 0 deletions
diff --git a/src/json.h b/src/json.h
index 8ed50e0..881a10a 100644
--- a/src/json.h
+++ b/src/json.h
@@ -131,4 +131,9 @@ struct og_procedure {
int og_json_parse_procedure(json_t *element, struct og_procedure *proc);
+struct og_repository {
+ const char *name;
+ const char *ip;
+};
+
#endif
diff --git a/src/rest.c b/src/rest.c
index 7e01e51..f354fa8 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -4992,6 +4992,59 @@ static int og_cmd_post_task_add(json_t *element,
return err;
}
+static int og_cmd_post_repository_add(json_t *element,
+ struct og_msg_params *params)
+{
+ struct og_repository repo = {};
+ const char *key, *msglog;
+ struct og_dbi *dbi;
+ dbi_result result;
+ json_t *value;
+ int err = 0;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "name")) {
+ err = og_json_parse_string(value, &repo.name);
+ params->flags |= OG_REST_PARAM_NAME;
+ } else if (!strcmp(key, "ip")) {
+ err = og_json_parse_string(value, &repo.ip);
+ params->flags |= OG_REST_PARAM_ADDR;
+ }
+
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_msg_params_validate(params, OG_REST_PARAM_NAME |
+ OG_REST_PARAM_ADDR))
+ return -1;
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open conection database (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "INSERT INTO repositorios("
+ "nombrerepositorio, ip) VALUES ('%s', '%s')",
+ repo.name, repo.ip);
+
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR,
+ "failed to add repository to database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+
+ return err;
+}
+
static int og_cmd_post_room_add(json_t *element,
struct og_msg_params *params)
{
@@ -5936,6 +5989,19 @@ int og_client_state_process_payload_rest(struct og_client *cli)
}
err = og_cmd_get_repositories(buf_reply);
+ } else if (!strncmp(cmd, "repository/add", strlen("repository/add"))) {
+ if (method != OG_METHOD_POST) {
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
+
+ if (!root) {
+ syslog(LOG_ERR,
+ "command repository add with no payload\n");
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+ err = og_cmd_post_repository_add(root, &params);
} else if (!strncmp(cmd, "images", strlen("images"))) {
if (method != OG_METHOD_GET) {
err = og_client_method_not_found(cli);