rest: no assumption on description field in create/image

do not use description field to decide if this is a new image or an update.

add og_dbi_get_image() to check if the image exists. If it is not found, then
add new image entry to database.

update og_dbi_add_image() to update the image.id field.
master v1.2.5-7
OpenGnSys Support Team 2023-12-19 12:02:55 +01:00
parent bad351b1d7
commit 7292c30659
3 changed files with 42 additions and 24 deletions

View File

@ -169,28 +169,11 @@ int og_dbi_get_room_info(struct og_dbi *dbi, struct og_room *room,
#define OG_UNASSIGNED_SW_ID 0
#define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */
int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image)
int og_dbi_add_image(struct og_dbi *dbi, struct og_image *image)
{
const char *msglog;
dbi_result result;
result = dbi_conn_queryf(dbi->conn,
"SELECT nombreca FROM imagenes WHERE nombreca = '%s'",
image->name);
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)) {
syslog(LOG_ERR, "image creation attempt with already used image name (%s:%d)\n",
__func__, __LINE__);
dbi_result_free(result);
return -1;
}
dbi_result_free(result);
result = dbi_conn_queryf(dbi->conn,
"INSERT INTO imagenes (nombreca, "
@ -216,7 +199,34 @@ 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);
image->id = dbi_conn_sequence_last(dbi->conn, NULL);
return 0;
}
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);
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
__func__, __LINE__, msglog);
return false;
}
if (dbi_result_next_row(result)) {
image->id = dbi_result_get_uint(result, "idimagen");
dbi_result_free(result);
return true;
}
dbi_result_free(result);
return false;
}
int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint64_t image_id,

View File

@ -119,7 +119,8 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
struct in_addr addr);
int og_dbi_get_room_info(struct og_dbi *dbi, struct og_room *room,
uint32_t room_id);
int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image);
bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image);
int og_dbi_add_image(struct og_dbi *dbi, struct og_image *image);
int og_dbi_schema_update(void);

View File

@ -2644,6 +2644,7 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params)
struct og_dbi *dbi;
json_t *clients;
int err = 0;
bool found;
err = og_json_parse_create_image(element, params);
if (err < 0)
@ -2665,23 +2666,29 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params)
return -1;
}
/* If there is a description, this means the image is not in the DB. */
if (params->image.description[0]) {
found = og_dbi_get_image(dbi, &params->image);
if (!found) {
if (!og_msg_params_validate(params, OG_REST_PARAM_REPO)) {
syslog(LOG_ERR,
"missing repo parameter in request (%s:%d)\n",
__func__, __LINE__);
og_dbi_close(dbi);
return -1;
}
err = og_dbi_add_image(dbi, &params->image);
if (err < 0) {
og_dbi_close(dbi);
return err;
}
snprintf(new_image_id, sizeof(new_image_id), "%u", err);
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;
}
clients = json_copy(element);