From d2f20d0be06617f421eecca111449d94672695eb Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Tue, 13 Oct 2020 14:46:28 +0200 Subject: #942 Create DB image when calling POST /image/create In case the DB entry for an image does not exist when POST /image/create is called, this patch takes care of calling it. This adds few optional json parameters to the POST /image/create API. If optional parameters are included then this patch creates the DB entry, otherwise it just creates the actual image and updates the existing entry. Request: POST /image/create { "clients":["192.168.56.11"], "disk":"1", "partition":"1", "name":"archlinux", "repository":"192.168.56.10", "id":"24", "code":"131", "description":"This is a test", "group_id":0, "center_id":1 } Response: 200 OK --- src/dbi.c | 36 ++++++++++++++++++++++++++++++++++++ src/dbi.h | 13 +++++++++++++ src/rest.c | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 83 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/dbi.c b/src/dbi.c index 968b00e..33c4a8e 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -123,3 +123,39 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, return 0; } + +#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; + + result = dbi_conn_queryf(dbi->conn, + "INSERT INTO imagenes (nombreca, " + "descripcion, " + "idperfilsoft, " + "idcentro, " + "comentarios, " + "grupoid, " + "idrepositorio, " + "tipo, " + "ruta) " + "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, + OG_IMAGE_DEFAULT_TYPE); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + dbi_result_free(result); + return dbi_conn_sequence_last(dbi->conn, NULL); +} diff --git a/src/dbi.h b/src/dbi.h index 7bfe311..8cd6fe6 100644 --- a/src/dbi.h +++ b/src/dbi.h @@ -3,6 +3,7 @@ #include #include +#include struct og_dbi_config { const char *user; @@ -24,6 +25,7 @@ void og_dbi_close(struct og_dbi *db); #define OG_DB_CENTER_NAME_MAXLEN 100 #define OG_DB_ROOM_NAME_MAXLEN 100 #define OG_DB_SERIAL_NUMBER_MAXLEN 25 +#define OG_DB_IMAGE_DESCRIPTION_MAXLEN 250 #define OG_DB_IMAGE_NAME_MAXLEN 50 #define OG_DB_FILESYSTEM_MAXLEN 16 #define OG_DB_NETDRIVER_MAXLEN 30 @@ -46,6 +48,16 @@ struct og_image_legacy { char code[OG_DB_INT8_MAXLEN + 1]; }; +struct og_image { + char name[OG_DB_IMAGE_NAME_MAXLEN + 1]; + char description[OG_DB_IMAGE_DESCRIPTION_MAXLEN + 1]; + const char *filename; + uint64_t center_id; + uint64_t datasize; + uint64_t group_id; + struct stat image_stats; +}; + struct og_legacy_partition { char partition[OG_DB_SMALLINT_MAXLEN + 1]; char code[OG_DB_INT8_MAXLEN + 1]; @@ -77,5 +89,6 @@ struct og_computer { struct in_addr; int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, struct in_addr addr); +int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image); #endif diff --git a/src/rest.c b/src/rest.c index a69e5be..8b28b1d 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1589,12 +1589,6 @@ static int og_cmd_software(json_t *element, struct og_msg_params *params) #define OG_IMAGE_TYPE_MAXLEN 4 -struct og_image { - const char *filename; - uint64_t datasize; - struct stat image_stats; -}; - static int og_get_image_stats(const char *name, struct stat *image_stats) { @@ -1744,7 +1738,10 @@ static int og_cmd_images(char *buffer_reply) static int og_cmd_create_image(json_t *element, struct og_msg_params *params) { + char new_image_id[OG_DB_INT_MAXLEN + 1]; + struct og_image image = {}; json_t *value, *clients; + struct og_dbi *dbi; const char *key; int err = 0; @@ -1772,8 +1769,17 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) } else if (!strcmp(key, "code")) { err = og_json_parse_string(value, ¶ms->code); params->flags |= OG_REST_PARAM_CODE; + } else if (!strcmp(key, "description")) { + err = og_json_parse_string_copy(value, + image.description, + sizeof(image.description)); + } else if (!strcmp(key, "group_id")) { + err = og_json_parse_uint64(value, &image.group_id); + } else if (!strcmp(key, "center_id")) { + err = og_json_parse_uint64(value, &image.center_id); } + if (err < 0) break; } @@ -1787,6 +1793,28 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) OG_REST_PARAM_REPO)) return -1; + /* If there is a description, this means the image is not in the DB. */ + if (image.description[0]) { + snprintf(image.name, sizeof(image.name), "%s", params->name); + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, + "cannot open connection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + err = og_dbi_add_image(dbi, &image); + + og_dbi_close(dbi); + if (err < 0) + return err; + + snprintf(new_image_id, sizeof(new_image_id), "%u", err); + params->id = new_image_id; + } + clients = json_copy(element); json_object_del(clients, "clients"); -- cgit v1.2.3-18-g5258