diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dbi.c | 79 | ||||
-rw-r--r-- | src/dbi.h | 4 | ||||
-rw-r--r-- | src/rest.c | 37 |
3 files changed, 64 insertions, 56 deletions
@@ -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; +} @@ -58,6 +58,7 @@ struct og_image { uint64_t center_id; uint64_t datasize; uint64_t group_id; + uint64_t repo_id; uint64_t type; uint64_t id; struct stat image_stats; @@ -110,4 +111,7 @@ int og_dbi_add_image(struct og_dbi *dbi, const 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, + char *repository_ip); + #endif @@ -2154,10 +2154,8 @@ int og_json_parse_create_image(json_t *element, (char *)¶ms->image.name, sizeof(params->image.name)); params->flags |= OG_REST_PARAM_NAME; - } else if (!strcmp(key, "repository")) { - err = og_json_parse_string_copy(value, - (char *)¶ms->image.repo_ip, - sizeof(params->image.repo_ip)); + } else if (!strcmp(key, "repository_id")) { + err = og_json_parse_uint64(value, ¶ms->image.repo_id); params->flags |= OG_REST_PARAM_REPO; } else if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); @@ -2186,6 +2184,7 @@ int og_json_parse_create_image(json_t *element, static int og_cmd_create_image(json_t *element, struct og_msg_params *params) { + char repository_ip[OG_DB_IP_MAXLEN + 1]; char new_image_id[OG_DB_INT_MAXLEN + 1]; struct og_dbi *dbi; json_t *clients; @@ -2200,25 +2199,30 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) OG_REST_PARAM_PARTITION | OG_REST_PARAM_CODE | OG_REST_PARAM_ID | - OG_REST_PARAM_NAME | - OG_REST_PARAM_REPO)) + OG_REST_PARAM_NAME)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, + "cannot open connection database (%s:%d)\n", + __func__, __LINE__); return -1; + } /* If there is a description, this means the image is not in the DB. */ if (params->image.description[0]) { - dbi = og_dbi_open(&ogconfig.db); - if (!dbi) { - syslog(LOG_ERR, - "cannot open connection database (%s:%d)\n", - __func__, __LINE__); + if (!og_msg_params_validate(params, OG_REST_PARAM_REPO)) { + og_dbi_close(dbi); return -1; } err = og_dbi_add_image(dbi, ¶ms->image); - og_dbi_close(dbi); - if (err < 0) + if (err < 0) { + og_dbi_close(dbi); return err; + } snprintf(new_image_id, sizeof(new_image_id), "%u", err); params->id = new_image_id; @@ -2227,6 +2231,13 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) clients = json_copy(element); json_object_del(clients, "clients"); + err = og_dbi_get_repository_ip(dbi, atoll(params->id), repository_ip); + og_dbi_close(dbi); + if (err < 0) + return err; + + json_object_set_new(clients ,"repository", json_string(repository_ip)); + return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_CREATE, params, clients); } |