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
master
Jose M. Guisado 2023-08-22 13:47:56 +02:00
parent b2c8c9d9ab
commit a67c1088ef
1 changed files with 49 additions and 0 deletions

View File

@ -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");