From a67c0d3d5756642373097261bc503f18f123e933 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Fri, 19 Jul 2024 13:05:11 +0200 Subject: dbi: use repository_id field if front-end offers it If repository_id json field is provided, use it to narrow down the image lookup. After this, search use this same repository ID to fetch the IP address. Old web console may not provide repository_id, infer it from the image name. Always set image id field for creation and update. --- src/dbi.c | 25 +++++++++++++++---------- src/dbi.h | 2 +- src/rest.c | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/dbi.c b/src/dbi.c index a3bee37..9eb1a39 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -242,9 +242,16 @@ bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image) const char *msglog; dbi_result result; - result = dbi_conn_queryf(dbi->conn, - "SELECT nombreca, idimagen FROM imagenes WHERE nombreca = '%s'", - image->name); + /* old front-end needs this... */ + if (image->repo_id) { + result = dbi_conn_queryf(dbi->conn, + "SELECT nombreca, idimagen FROM imagenes WHERE nombreca = '%s'", + image->name); + } else { + result = dbi_conn_queryf(dbi->conn, + "SELECT nombreca, idimagen FROM imagenes WHERE nombreca = '%s' AND idrepositorio = '%s'", + image->name, image->repo_id); + } if (!result) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", @@ -262,17 +269,15 @@ bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image) return false; } -int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint64_t image_id, +int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id, char *repository_ip) { const char *msglog, *dbi_repository_ip; dbi_result result; result = dbi_conn_queryf(dbi->conn, - "SELECT r.ip FROM imagenes i " - "JOIN repositorios r USING (idrepositorio) " - "WHERE i.idimagen = %lu", - image_id); + "SELECT ip FROM repositorios WHERE idrepositorio = %u", + repo_id); if (!result) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", @@ -283,8 +288,8 @@ int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint64_t image_id, if (!dbi_result_next_row(result)) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, - "image with id %lu has no repository (%s:%d) %s\n", - image_id, __func__, __LINE__, msglog); + "repository with id %u does not exist (%s:%d) %s\n", + repo_id, __func__, __LINE__, msglog); dbi_result_free(result); return -1; } diff --git a/src/dbi.h b/src/dbi.h index f7aaea3..e16e5d1 100644 --- a/src/dbi.h +++ b/src/dbi.h @@ -144,7 +144,7 @@ int og_dbi_add_image(struct og_dbi *dbi, struct og_image *image); int og_dbi_schema_update(void); -int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint64_t image_id, +int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id, char *repository_ip); #endif diff --git a/src/rest.c b/src/rest.c index 708d53a..6ccb97b 100644 --- a/src/rest.c +++ b/src/rest.c @@ -3737,17 +3737,17 @@ static int og_cmd_add_image(json_t *element, struct og_msg_params *params, snprintf(new_image_id, sizeof(new_image_id), "%lu", params->image.id); params->id = new_image_id; - json_object_set_new(element, "id", json_string(params->id)); } else { syslog(LOG_INFO, "updating existing image `%s'\n", params->image.name); snprintf(new_image_id, sizeof(new_image_id), "%lu", params->image.id); params->id = new_image_id; } + json_object_set_new(element, "id", json_string(params->id)); clients = json_copy(element); json_object_del(clients, "clients"); - err = og_dbi_get_repository_ip(dbi, atoll(params->id), repository_ip); + err = og_dbi_get_repository_ip(dbi, params->image.repo_id, repository_ip); og_dbi_close(dbi); if (err < 0) return err; -- cgit v1.2.3-18-g5258