summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2020-06-29 14:09:07 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2020-06-30 16:49:11 +0200
commit608709f6206eb4061a6d59eff68babd3a8562129 (patch)
tree1df4a59715bf36d50dc262cf917681e1856720bd /src
parent37e91b2ebb14082e366ed4a9b09d76a2f6163818 (diff)
#980 Add GET /modes REST request
This patch implements HTTP GET /modes request which returns all modes available for clients: Request: GET /modes HTTP/1.0 Response: 200 OK { "modes": [ "pxe", "10", "13", "00unknown", "11", "19pxeADMIN", "12" ] }
Diffstat (limited to 'src')
-rw-r--r--src/rest.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/rest.c b/src/rest.c
index 097cbf8..43871e4 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -21,6 +21,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <jansson.h>
+#include <dirent.h>
#include <time.h>
struct ev_loop *og_loop;
@@ -837,6 +838,54 @@ static int og_cmd_reboot(json_t *element, struct og_msg_params *params)
return og_send_request(OG_METHOD_POST, OG_CMD_REBOOT, params, NULL);
}
+#define OG_TFTP_TMPL_PATH "/opt/opengnsys/tftpboot/menu.lst/templates"
+
+static int og_cmd_get_modes(json_t *element, struct og_msg_params *params,
+ char *buffer_reply)
+{
+ struct og_buffer og_buffer = {
+ .data = buffer_reply
+ };
+ json_t *root, *modes;
+ struct dirent *dent;
+ DIR *d = NULL;
+
+ root = json_object();
+ if (!root)
+ return -1;
+
+ modes = json_array();
+ if (!modes) {
+ json_decref(root);
+ return -1;
+ }
+
+ d = opendir(OG_TFTP_TMPL_PATH);
+ if (!d) {
+ json_decref(modes);
+ json_decref(root);
+ syslog(LOG_ERR, "Cannot open directory %s\n",
+ OG_TFTP_TMPL_PATH);
+ return -1;
+ }
+
+ dent = readdir(d);
+ while (dent) {
+ if (dent->d_type != DT_REG) {
+ dent = readdir(d);
+ continue;
+ }
+ json_array_append_new(modes, json_string(dent->d_name));
+ dent = readdir(d);
+ }
+
+ json_object_set_new(root, "modes", modes);
+ json_dump_callback(root, og_json_dump_clients, &og_buffer, 0);
+ json_decref(root);
+
+ return 0;
+}
+
static int og_cmd_stop(json_t *element, struct og_msg_params *params)
{
const char *key;
@@ -3116,6 +3165,11 @@ int og_client_state_process_payload_rest(struct og_client *cli)
return og_client_bad_request(cli);
}
err = og_cmd_reboot(root, &params);
+ } else if (!strncmp(cmd, "modes", strlen("modes"))) {
+ if (method != OG_METHOD_GET)
+ return og_client_method_not_found(cli);
+
+ err = og_cmd_get_modes(root, &params, buf_reply);
} else if (!strncmp(cmd, "stop", strlen("stop"))) {
if (method != OG_METHOD_POST)
return og_client_method_not_found(cli);