From c0573b9ef05bc1c22a6b5b71c2f73ba18a8696f1 Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Wed, 25 May 2022 17:33:36 +0200 Subject: #915 Set repository on image creation Assign to the image the repository indicated in the JSON body instead of a default one. --- src/dbi.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- src/rest.c | 4 +++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/dbi.c b/src/dbi.c index aaf6c8b..0718126 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -127,14 +127,55 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, return 0; } +const int og_dbi_get_repository(const struct og_dbi *dbi, const char *repo_ip) +{ + const char *msglog; + dbi_result result; + int repo_id; + + /* database can store duplicated repositories, limit query to return + * only one */ + result = dbi_conn_queryf(dbi->conn, + "SELECT idrepositorio FROM repositorios " + "WHERE ip = '%s' LIMIT 1", + repo_ip); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + if (!dbi_result_next_row(result)) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, + "software profile does not exist in database (%s:%d) %s\n", + __func__, __LINE__, msglog); + dbi_result_free(result); + return -1; + } + + repo_id = dbi_result_get_int(result, "idrepositorio"); + dbi_result_free(result); + + return repo_id; +} + #define OG_UNASSIGNED_SW_ID 0 -#define OG_DEFAULT_REPO_ID 1 #define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */ int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image) { const char *msglog; dbi_result result; + int repo_id; + + repo_id = og_dbi_get_repository(dbi, image->repo_ip); + if (repo_id < 0) { + syslog(LOG_ERR, "failed to get repository (%s:%d)\n", + __func__, __LINE__); + return -1; + } result = dbi_conn_queryf(dbi->conn, "SELECT nombreca FROM imagenes WHERE nombreca = '%s'", @@ -167,7 +208,7 @@ int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image) "VALUES ('%s', '%s', %u, %lu, '', %u, %lu, %u, '')", image->name, image->description, OG_UNASSIGNED_SW_ID, image->center_id, - image->group_id, OG_DEFAULT_REPO_ID, + image->group_id, repo_id, OG_IMAGE_DEFAULT_TYPE); if (!result) { diff --git a/src/rest.c b/src/rest.c index b5fcda7..5f6d3ec 100644 --- a/src/rest.c +++ b/src/rest.c @@ -2127,7 +2127,9 @@ int og_json_parse_create_image(json_t *element, sizeof(params->image.name)); params->flags |= OG_REST_PARAM_NAME; } else if (!strcmp(key, "repository")) { - err = og_json_parse_string(value, ¶ms->repository); + err = og_json_parse_string_copy(value, + (char *)¶ms->image.repo_ip, + sizeof(params->image.repo_ip)); params->flags |= OG_REST_PARAM_REPO; } else if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); -- cgit v1.2.3-18-g5258