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" }'
master v1.2.5-3
Javier Hernandez 2023-11-23 13:32:59 +01:00 committed by OpenGnSys Support Team
parent 0ab4721714
commit 5ff92a826e
2 changed files with 83 additions and 0 deletions

View File

@ -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, &params, 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, &params, buf_reply);
} else if (!strncmp(cmd, "repository/delete",
strlen("repository/delete"))) {
if (method != OG_METHOD_POST) {

View File

@ -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,