From 5ff92a826efad2acb4589755fb78fa4cdbda2614 Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Thu, 23 Nov 2023 13:32:59 +0100 Subject: rest: add POST repository/update Add POST repository/update request to update repository. Repository is identified by its id, that is provided in the payload. This can be tested with curl like: curl -X POST -H "Authorization: $API_KEY" http://127.0.0.1:8888/repository/update -d '{ "repo_id" : 16, "center" : 3, "name": "newName", "ip":"127.0.0.1" }' --- src/rest.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rest.h | 1 + 2 files changed, 83 insertions(+) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index 5cd6791..8bbe063 100644 --- a/src/rest.c +++ b/src/rest.c @@ -5405,6 +5405,74 @@ static int og_cmd_post_task_add(json_t *element, return err; } +static int og_cmd_post_repository_update(json_t *element, + struct og_msg_params *params, + char *buffer_reply) +{ + struct og_repository repo = {}; + const char *key, *msglog; + unsigned int repo_id; + struct og_dbi *dbi; + dbi_result result; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "repo_id")) { + err = og_json_parse_uint(value, &repo_id); + params->flags |= OG_REST_PARAM_ID; + } + if (!strcmp(key, "name")) { + err = og_json_parse_string_copy(value, + repo.name, + sizeof(repo.name)); + } else if (!strcmp(key, "ip")) { + err = og_json_parse_string_copy(value, + repo.ip, + sizeof(repo.ip)); + } else if (!strcmp(key, "center")) { + err = og_json_parse_uint(value, &repo.center); + } + if (err < 0) + return err; + } + if (!og_msg_params_validate(params, OG_REST_PARAM_ID)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "UPDATE repositorios" + " SET nombrerepositorio='%s'," + " ip='%s'," + " idcentro=%u" + " WHERE idrepositorio=%u;", + repo.name, repo.ip, repo.center, repo_id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to update repository in database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + if (!dbi_result_get_numrows_affected(result)) { + syslog(LOG_ERR, "repository with id %u does not exist (%s:%d)\n", + repo_id, __func__, __LINE__); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + + dbi_result_free(result); + og_dbi_close(dbi); + return err; +} + static int og_cmd_post_repository_add(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -6429,6 +6497,7 @@ struct { [OG_URI_SOFTWARE] = { "software", }, [OG_URI_REPO] = { "repositories", }, [OG_URI_REPO_ADD] = { "repository/add", }, + [OG_URI_REPO_UPDATE] = { "repository/update", }, [OG_URI_REPO_DELETE] = { "repository/delete", }, [OG_URI_IMAGES] = { "images", }, [OG_URI_IMAGE_CREATE] = { "image/create" }, @@ -6867,6 +6936,19 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } err = og_cmd_post_repository_add(root, ¶ms, buf_reply); + } else if (!strncmp(cmd, "repository/update", strlen("repository/update"))) { + 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_update(root, ¶ms, buf_reply); } else if (!strncmp(cmd, "repository/delete", strlen("repository/delete"))) { if (method != OG_METHOD_POST) { diff --git a/src/rest.h b/src/rest.h index 5e21b39..6cd69be 100644 --- a/src/rest.h +++ b/src/rest.h @@ -108,6 +108,7 @@ enum og_rest_uri { OG_URI_SOFTWARE, OG_URI_REPO, OG_URI_REPO_ADD, + OG_URI_REPO_UPDATE, OG_URI_REPO_DELETE, OG_URI_IMAGES, OG_URI_IMAGE_CREATE, -- cgit v1.2.3-18-g5258