diff options
author | Roberto Hueso Gómez <rhueso@soleta.eu> | 2019-09-24 11:51:38 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-10-01 12:56:10 +0200 |
commit | 1a8ada191f779aec01e00ff429f437992bf8919c (patch) | |
tree | 0ad33248d8417f09d586f4a233260ecdd26419d5 /sources/ogAdmServer.cpp | |
parent | 7bdc8fdf756c068f070d27c644d7fc7cf9910dc4 (diff) |
#915: Add POST /image/create command to REST API in ogAdmServer
This patch implements the command "image/create" that creates an image
in a client.
Request:
POST /image/create
{ "clients" : [ "192.168.2.1" ], "disk" : "1", "partition" : "1", "code" : "1", "id" : "1", "name" : "test", "repository" : "192.168.2.2" }
Reply:
200 OK
Diffstat (limited to 'sources/ogAdmServer.cpp')
-rw-r--r-- | sources/ogAdmServer.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp index 12fdf41..19c0eb1 100644 --- a/sources/ogAdmServer.cpp +++ b/sources/ogAdmServer.cpp @@ -3359,6 +3359,10 @@ struct og_msg_params { char run_cmd[4096]; const char *disk; const char *partition; + const char *repository; + const char *name; + const char *id; + const char *code; }; static int og_json_parse_clients(json_t *element, struct og_msg_params *params) @@ -3752,6 +3756,47 @@ static int og_json_parse_partition(json_t *element, return 0; } +static int og_json_parse_name(json_t *element, struct og_msg_params *params) +{ + if (json_typeof(element) != JSON_STRING) + return -1; + + params->name = json_string_value(element); + + return 0; +} + +static int og_json_parse_repository(json_t *element, + struct og_msg_params *params) +{ + if (json_typeof(element) != JSON_STRING) + return -1; + + params->repository = json_string_value(element); + + return 0; +} + +static int og_json_parse_id(json_t *element, struct og_msg_params *params) +{ + if (json_typeof(element) != JSON_STRING) + return -1; + + params->id = json_string_value(element); + + return 0; +} + +static int og_json_parse_code(json_t *element, struct og_msg_params *params) +{ + if (json_typeof(element) != JSON_STRING) + return -1; + + params->code = json_string_value(element); + + return 0; +} + static int og_cmd_session(json_t *element, struct og_msg_params *params) { char buf[4096], iph[4096]; @@ -3919,6 +3964,54 @@ static int og_cmd_software(json_t *element, struct og_msg_params *params) CLIENTE_OCUPADO); } +static int og_cmd_create_image(json_t *element, struct og_msg_params *params) +{ + char buf[4096] = {}; + int err = 0, len; + const char *key; + json_t *value; + TRAMA *msg; + + if (json_typeof(element) != JSON_OBJECT) + return -1; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "disk")) + err = og_json_parse_disk(value, params); + else if (!strcmp(key, "partition")) + err = og_json_parse_partition(value, params); + else if (!strcmp(key, "name")) + err = og_json_parse_name(value, params); + else if (!strcmp(key, "repository")) + err = og_json_parse_repository(value, params); + else if (!strcmp(key, "clients")) + err = og_json_parse_clients(value, params); + else if (!strcmp(key, "id")) + err = og_json_parse_id(value, params); + else if (!strcmp(key, "code")) + err = og_json_parse_code(value, params); + + if (err < 0) + break; + } + + len = snprintf(buf, sizeof(buf), + "nfn=CrearImagen\rdsk=%s\rpar=%s\rcpt=%s\ridi=%s\rnci=%s\ripr=%s\r", + params->disk, params->partition, params->code, + params->id, params->name, params->repository); + + msg = og_msg_alloc(buf, len); + if (!msg) + return -1; + + og_send_cmd((char **)params->ips_array, params->ips_array_len, + CLIENTE_OCUPADO, msg); + + og_msg_free(msg); + + return 0; +} + static int og_client_method_not_found(struct og_client *cli) { /* To meet RFC 7231, this function MUST generate an Allow header field @@ -4141,6 +4234,15 @@ static int og_client_state_process_payload_rest(struct og_client *cli) return og_client_bad_request(cli); } err = og_cmd_software(root, ¶ms); + } else if (!strncmp(cmd, "image/create", strlen("image/create"))) { + if (method != OG_METHOD_POST) + return og_client_method_not_found(cli); + + if (!root) { + syslog(LOG_ERR, "command create with no payload\n"); + return og_client_bad_request(cli); + } + err = og_cmd_create_image(root, ¶ms); } else { syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd); err = og_client_not_found(cli); |