summaryrefslogtreecommitdiffstats
path: root/src/dbi.c
diff options
context:
space:
mode:
authorJavier Sánchez Parra <jsanchez@soleta.eu>2022-06-16 14:28:43 +0200
committerJavier Sánchez Parra <jsanchez@soleta.eu>2022-06-20 10:41:32 +0200
commit52a38d3e574fb25b47d230bc87754583eb17b4a6 (patch)
tree0cca8d4a0ef608459addd256b6740b0de044d2d6 /src/dbi.c
parenta0a3470682852d5f72967553d5debe41e86eca78 (diff)
#915 Use the repository id on image creation
POST /image/create has two modes, image creation and update. You can find more information about the "creation" mode in commit: d2f20d0be06617f421eecca111449d94672695eb On image creation, use the id to identify repositories instead of the IP. This is a preparative commit to the support of repositories with several IPs. On image update, "repository_id" field is not needed because the image already has the repository assigned. This commit maintains backward compatibility with the Web Console (old web interface), because it only use the "update" mode of /image/create. Request POST /create/image: { "clients": [ "192.168.56.11" ], "disk": "1", "partition": "1", "name": "archlinux", "repository_id": 1, "id": "0", "code": "131", "description": "This is a test", "group_id": 0, "center_id": 1 } Response 200 OK
Diffstat (limited to 'src/dbi.c')
-rw-r--r--src/dbi.c79
1 files changed, 36 insertions, 43 deletions
diff --git a/src/dbi.c b/src/dbi.c
index 0718126..a90d5ec 100644
--- a/src/dbi.c
+++ b/src/dbi.c
@@ -127,40 +127,6 @@ 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_IMAGE_DEFAULT_TYPE 1 /* monolithic */
@@ -168,14 +134,6 @@ 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'",
@@ -208,7 +166,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, repo_id,
+ image->group_id, image->repo_id,
OG_IMAGE_DEFAULT_TYPE);
if (!result) {
@@ -221,3 +179,38 @@ int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image)
dbi_result_free(result);
return dbi_conn_sequence_last(dbi->conn, NULL);
}
+
+int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint64_t image_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);
+ 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,
+ "image with id %lu has no repository (%s:%d) %s\n",
+ image_id, __func__, __LINE__, msglog);
+ dbi_result_free(result);
+ return -1;
+ }
+
+ dbi_repository_ip = dbi_result_get_string(result, "ip");
+ snprintf(repository_ip, OG_DB_IP_MAXLEN + 1, "%s", dbi_repository_ip);
+
+ dbi_result_free(result);
+
+ return 0;
+}