From a67c1088efac08840458c6b66660e06e5119f0cc Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Tue, 22 Aug 2023 13:47:56 +0200 Subject: 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 --- src/rest.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src') 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, ¶ms->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, ¶ms); + break; case OG_METHOD_POST: if (!root) { syslog(LOG_ERR, "address add command with no payload\n"); -- cgit v1.2.3-18-g5258