summaryrefslogtreecommitdiffstats
path: root/src/rest.c
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2020-10-13 14:46:28 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2020-10-14 12:05:25 +0200
commitd2f20d0be06617f421eecca111449d94672695eb (patch)
tree6547ad52f265f89158553d26bc9e7eac9ed72518 /src/rest.c
parent24c8b940e614fa795b679628be2e6c0d68bd41e5 (diff)
#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
Diffstat (limited to 'src/rest.c')
-rw-r--r--src/rest.c40
1 files changed, 34 insertions, 6 deletions
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, &params->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");