diff options
Diffstat (limited to 'src/rest.c')
-rw-r--r-- | src/rest.c | 40 |
1 files changed, 39 insertions, 1 deletions
@@ -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); |