summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2019-09-30 16:16:44 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2019-10-01 12:56:10 +0200
commit1dde02e1ab31514e3a5c1c4335ebdbe510460518 (patch)
treeb658658a9080866af0847d90b1c5fb76e5e2d505
parentc87a1dbdfb3fd0a91acccd418951f879e9c33d13 (diff)
#915: Add POST /image/restore command to REST API in ogAdmServer
This patch implements the command "image/create" that creates an image in a client. Request: POST /image/restore { "clients" : [ "192.168.56.11" ], "disk" : "1", "partition" : "1", "name" : "test", "repository" : "192.168.56.10", "type" : "UNICAST", "filesystem": "1", "image_id": "1"} Reply: 200 OK
-rw-r--r--sources/ogAdmServer.cpp63
-rw-r--r--tests/restore_image.json1
-rwxr-xr-xtests/run-tests.sh1
3 files changed, 65 insertions, 0 deletions
diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp
index 8b95e3e..8ef497e 100644
--- a/sources/ogAdmServer.cpp
+++ b/sources/ogAdmServer.cpp
@@ -3363,6 +3363,8 @@ struct og_msg_params {
const char *name;
const char *id;
const char *code;
+ const char *type;
+ const char *profile;
};
static int og_json_parse_clients(json_t *element, struct og_msg_params *params)
@@ -3959,6 +3961,58 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params)
return 0;
}
+static int og_cmd_restore_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_string(value, &params->disk);
+ else if (!strcmp(key, "partition"))
+ err = og_json_parse_string(value, &params->partition);
+ else if (!strcmp(key, "name"))
+ err = og_json_parse_string(value, &params->name);
+ else if (!strcmp(key, "repository"))
+ err = og_json_parse_string(value, &params->repository);
+ else if (!strcmp(key, "clients"))
+ err = og_json_parse_clients(value, params);
+ else if (!strcmp(key, "type"))
+ err = og_json_parse_string(value, &params->type);
+ else if (!strcmp(key, "profile"))
+ err = og_json_parse_string(value, &params->profile);
+ else if (!strcmp(key, "id"))
+ err = og_json_parse_string(value, &params->id);
+
+ if (err < 0)
+ break;
+ }
+
+ len = snprintf(buf, sizeof(buf),
+ "nfn=RestaurarImagen\ridi=%s\rdsk=%s\rpar=%s\rifs=%s\r"
+ "nci=%s\ripr=%s\rptc=%s\r",
+ params->id, params->disk, params->partition,
+ params->profile, params->name,
+ params->repository, params->type);
+
+ 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
@@ -4190,6 +4244,15 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
return og_client_bad_request(cli);
}
err = og_cmd_create_image(root, &params);
+ } else if (!strncmp(cmd, "image/restore", strlen("image/restore"))) {
+ 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_restore_image(root, &params);
} else {
syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd);
err = og_client_not_found(cli);
diff --git a/tests/restore_image.json b/tests/restore_image.json
new file mode 100644
index 0000000..76992ca
--- /dev/null
+++ b/tests/restore_image.json
@@ -0,0 +1 @@
+{ "clients" : [ "192.168.56.11" ], "disk" : "1", "partition" : "1", "name" : "test", "repository" : "192.168.56.10", "type" : "UNICAST", "filesystem": "1", "image_id": "1"}
diff --git a/tests/run-tests.sh b/tests/run-tests.sh
index f454bcd..0ea97bc 100755
--- a/tests/run-tests.sh
+++ b/tests/run-tests.sh
@@ -13,3 +13,4 @@ curl -X POST -H "Authorization: $API_KEY" http://127.0.0.1:8888/refresh -d @refr
curl -X POST -H "Authorization: $API_KEY" http://127.0.0.1:8888/hardware -d @post_clients.json
curl -X POST -H "Authorization: $API_KEY" http://127.0.0.1:8888/software -d @post_clients.json
curl -X POST -H "Authorization: $API_KEY" http://127.0.0.1:8888/image/create -d @create_image.json
+curl -X POST -H "Authorization: $API_KEY" http://127.0.0.1:8888/image/restore -d @restore_image.json