summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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) {