summaryrefslogtreecommitdiffstats
path: root/src/rest.c
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2023-08-22 13:47:56 +0200
committerJose M. Guisado <jguisado@soleta.eu>2023-08-23 11:39:19 +0200
commita67c1088efac08840458c6b66660e06e5119f0cc (patch)
tree161200717b5fc538c5b25c58112921120f671dc4 /src/rest.c
parentb2c8c9d9ab1cfaaf833f7e6ad318505df9a9ec36 (diff)
rest: add DELETE operation to /server endpoint
Expose deletion of rows from "entornos" table via the /server endpoint using the DELETE http request method. The expected payload is the server id as a string. For example: >>> DELETE /server { "id": "4" } <<< 200 OK If the specified server is currently associated with any computer ("ordenadores" table) the foreign key contraint (ON DELETE RESTRICT) will avoid the deletion and the server responds with 400 Bad Request. >>> DELETE /server { "id": "1" } <<< 400 Bad Request
Diffstat (limited to 'src/rest.c')
-rw-r--r--src/rest.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/rest.c b/src/rest.c
index 8f9c61e..c1161eb 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -5689,6 +5689,52 @@ static int og_cmd_post_server(json_t *element,
return ret;
}
+static int og_cmd_delete_server(json_t *element,
+ struct og_msg_params *params)
+{
+ const char *key, *msglog;
+ struct og_dbi *dbi;
+ dbi_result result;
+ json_t *value;
+ int err = 0;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "id")) {
+ err = og_json_parse_string(value, &params->id);
+ params->flags |= OG_REST_PARAM_ID;
+ }
+
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_msg_params_validate(params, OG_REST_PARAM_ID))
+ return -1;
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open conection database (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "DELETE FROM entornos WHERE identorno='%s'",
+ params->id);
+
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to delete server (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return 0;
+}
+
static int og_dbi_update_oglive(struct og_dbi *dbi, const char *mac,
const char * oglive)
{
@@ -6526,6 +6572,9 @@ int og_client_state_process_payload_rest(struct og_client *cli)
case OG_METHOD_GET:
err = og_cmd_get_servers(buf_reply);
break;
+ case OG_METHOD_DELETE:
+ err = og_cmd_delete_server(root, &params);
+ break;
case OG_METHOD_POST:
if (!root) {
syslog(LOG_ERR, "address add command with no payload\n");