summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJavier Sánchez Parra <jsanchez@soleta.eu>2022-05-25 17:33:36 +0200
committerJavier Sánchez Parra <jsanchez@soleta.eu>2022-05-26 15:15:52 +0200
commitc0573b9ef05bc1c22a6b5b71c2f73ba18a8696f1 (patch)
treea872689bc46cba3dd52a4bf3ace35d5d7a440c10 /src
parentb86b6e1443a6dfd21fa1520f7c4421a44d3595f3 (diff)
#915 Set repository on image creation
Assign to the image the repository indicated in the JSON body instead of a default one.
Diffstat (limited to 'src')
-rw-r--r--src/dbi.c45
-rw-r--r--src/rest.c4
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, &params->repository);
+ err = og_json_parse_string_copy(value,
+ (char *)&params->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);