From f64b1c2767640a5010cf16f37b36e62152691954 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Tue, 19 Dec 2023 12:42:12 +0100 Subject: rest: add POST image/update add explicit API to update an image --- src/rest.c | 40 +++++++++++++++++++++++++++++++++++++++- src/rest.h | 1 + 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/rest.c b/src/rest.c index c72d91e..02fbf8a 100644 --- a/src/rest.c +++ b/src/rest.c @@ -261,6 +261,7 @@ static const char *og_cmd_to_uri[OG_CMD_MAX] = { [OG_CMD_HARDWARE] = "hardware", [OG_CMD_SOFTWARE] = "software", [OG_CMD_IMAGE_CREATE] = "image/create", + [OG_CMD_IMAGE_UPDATE] = "image/update", [OG_CMD_IMAGE_RESTORE] = "image/restore", [OG_CMD_SETUP] = "setup", [OG_CMD_RUN_SCHEDULE] = "run/schedule", @@ -2637,7 +2638,8 @@ int og_json_parse_create_image(json_t *element, return 0; } -static int og_cmd_create_image(json_t *element, struct og_msg_params *params) +static int og_cmd_add_image(json_t *element, struct og_msg_params *params, + bool update) { char repository_ip[OG_DB_IP_MAXLEN + 1]; char new_image_id[OG_DB_INT_MAXLEN + 1]; @@ -2668,6 +2670,13 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) found = og_dbi_get_image(dbi, ¶ms->image); if (!found) { + if (update) { + syslog(LOG_ERR, "cannot update image file `%s', it does not exist\n", + params->image.name); + og_dbi_close(dbi); + return -1; + } + if (!og_msg_params_validate(params, OG_REST_PARAM_REPO)) { syslog(LOG_ERR, "missing repo parameter in request (%s:%d)\n", @@ -2686,6 +2695,13 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) params->id = new_image_id; json_object_set_new(element, "id", json_string(params->id)); } else { + if (!update) { + syslog(LOG_ERR, "cannot create image file `%s', it already exists\n", + params->image.name); + og_dbi_close(dbi); + return -1; + } + 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; @@ -2706,6 +2722,16 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) clients); } +static int og_cmd_create_image(json_t *element, struct og_msg_params *params) +{ + return og_cmd_add_image(element, params, false); +} + +static int og_cmd_update_image(json_t *element, struct og_msg_params *params) +{ + return og_cmd_add_image(element, params, true); +} + int og_json_parse_restore_image(json_t *element, struct og_msg_params *params) { const char *key; @@ -7052,6 +7078,18 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } err = og_cmd_create_image(root, ¶ms); + } else if (!strncmp(cmd, "image/update", strlen("image/update"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, "command create with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_update_image(root, ¶ms); } else if (!strncmp(cmd, "image/restore", strlen("image/restore"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); diff --git a/src/rest.h b/src/rest.h index 3c1cf93..ba6d3c2 100644 --- a/src/rest.h +++ b/src/rest.h @@ -35,6 +35,7 @@ enum og_cmd_type { OG_CMD_HARDWARE, OG_CMD_SOFTWARE, OG_CMD_IMAGE_CREATE, + OG_CMD_IMAGE_UPDATE, OG_CMD_IMAGE_RESTORE, OG_CMD_SETUP, OG_CMD_RUN_SCHEDULE, -- cgit v1.2.3-18-g5258