diff options
-rw-r--r-- | src/rest.c | 182 | ||||
-rw-r--r-- | src/rest.h | 2 |
2 files changed, 184 insertions, 0 deletions
@@ -5656,6 +5656,160 @@ static int og_cmd_oglive_set(json_t *element, struct og_msg_params *params) return 0; } +static int og_cmd_oglive_add(json_t *element, struct og_msg_params *params) +{ + const char *oglive_str; + bool is_update = false; + int is_default_value; + const char *msglog; + struct og_dbi *dbi; + uint64_t flags = 0; + dbi_result result; + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "name")) { + err = og_json_parse_string(value, &oglive_str); + flags |= OG_REST_PARAM_NAME; + } else { + err = -1; + } + + if (err < 0) + return err; + } + + if (!og_flags_validate(flags, OG_REST_PARAM_NAME)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "SELECT name FROM oglive WHERE name = '%s'", + oglive_str); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + + if (dbi_result_next_row(result)) { + is_update = true; + syslog(LOG_INFO, "Found oglive '%s' in database, updating entry", oglive_str); + } + + dbi_result_free(result); + + is_default_value = 0; + + if (is_update) + result = dbi_conn_queryf(dbi->conn, + "UPDATE oglive SET creation_date = NOW(), is_default = %d " + "WHERE name = '%s'", + is_default_value, + oglive_str); + else + result = dbi_conn_queryf(dbi->conn, + "INSERT INTO oglive (name, creation_date, is_default) " + "VALUES ('%s', NOW(), %d)", + oglive_str, + is_default_value); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%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_cmd_oglive_delete(json_t *element, struct og_msg_params *params) +{ + const char *oglive_str; + const char *msglog; + struct og_dbi *dbi; + uint64_t flags = 0; + dbi_result result; + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "name")) { + err = og_json_parse_string(value, &oglive_str); + flags |= OG_REST_PARAM_NAME; + } else { + err = -1; + } + + if (err < 0) + return err; + } + + if (!og_flags_validate(flags, OG_REST_PARAM_NAME)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "SELECT oglivedir FROM ordenadores WHERE oglivedir = '%s'", + oglive_str); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + + if (dbi_result_next_row(result)) { + syslog(LOG_ERR, "failed to delete oglive '%s', live in use by a client", oglive_str); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM oglive WHERE name = '%s'", + oglive_str); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + + if (dbi_result_get_numrows_affected(result) == 0) + syslog(LOG_WARNING, "No live entries found with name %s in database ", oglive_str); + + dbi_result_free(result); + og_dbi_close(dbi); + + return 0; +} + static int og_cmd_get_server_stats(char *buffer_reply) { json_t *root, *time_obj, *memory, *swap; @@ -5842,6 +5996,8 @@ struct { [OG_URI_EFI] = { "efi", }, [OG_URI_PART_SETUP] = { "setup", }, [OG_URI_OGLIVE_LIST] = { "oglive/list", }, + [OG_URI_OGLIVE_ADD] = { "oglive/add", }, + [OG_URI_OGLIVE_DELETE] = { "oglive/delete", }, [OG_URI_OGLIVE_SET] = { "oglive/set", }, [OG_URI_CENTER_ADD] = { "center/add", }, [OG_URI_CENTER_UPDATE] = { "center/update", }, @@ -6489,6 +6645,32 @@ int og_client_state_process_payload_rest(struct og_client *cli) } err = og_cmd_oglive_list(buf_reply); + } else if (!strncmp(cmd, "oglive/add", strlen("oglive/add"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command oglive add with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_oglive_add(root, ¶ms); + } else if (!strncmp(cmd, "oglive/delete", strlen("oglive/delete"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command oglive delete with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_oglive_delete(root, ¶ms); } else if (!strncmp(cmd, "oglive/set", strlen("oglive/set"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -142,6 +142,8 @@ enum og_rest_uri { OG_URI_CACHE_FETCH, OG_URI_PART_SETUP, OG_URI_OGLIVE_LIST, + OG_URI_OGLIVE_ADD, + OG_URI_OGLIVE_DELETE, OG_URI_OGLIVE_SET, OG_URI_CENTER_ADD, OG_URI_CENTER_UPDATE, |