From b63bbfd8326ef6f9bfa97605b8b728a77f60df97 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Tue, 14 May 2024 17:59:22 +0200 Subject: rest: extend GET /session to support for more than one client GET /session only supports for one single client IP address, extend it to remove this artificial limit. Add "clients" json attribute which is an alias of the existing "client" for consistency with other existing endpoints. --- src/rest.c | 92 ++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index a426a0f..985e017 100644 --- a/src/rest.c +++ b/src/rest.c @@ -856,22 +856,66 @@ static int og_cmd_session(json_t *element, struct og_msg_params *params) return og_send_request(OG_METHOD_POST, OG_CMD_SESSION, params, clients); } +static int og_json_os_array_get(struct og_dbi *dbi, json_t *array, const char *ip) +{ + unsigned int disk, partition; + const char *os_name; + const char *msglog; + dbi_result result; + json_t *item; + + result = dbi_conn_queryf(dbi->conn, + "SELECT op.numdisk, op.numpar, nom.nombreso " + "FROM ordenadores o " + "INNER JOIN ordenadores_particiones op " + " ON o.idordenador = op.idordenador " + "INNER JOIN nombresos nom " + " ON op.idnombreso = nom.idnombreso " + "WHERE o.ip = '%s'", ip); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + while (dbi_result_next_row(result)) { + item = json_object(); + if (!item) { + dbi_result_free(result); + return -1; + } + + disk = dbi_result_get_uint(result, "numdisk"); + partition = dbi_result_get_uint(result, "numpar"); + os_name = dbi_result_get_string(result, "nombreso"); + + json_object_set_new(item, "disk", json_integer(disk)); + json_object_set_new(item, "partition", json_integer(partition)); + json_object_set_new(item, "name", json_string(os_name)); + json_array_append_new(array, item); + } + dbi_result_free(result); + + return 0; +} + static int og_cmd_get_session(json_t *element, struct og_msg_params *params, char *buffer_reply) { - json_t *value, *root, *array, *item; - const char *key, *msglog, *os_name; - unsigned int disk, partition; + json_t *value, *root, *array; struct og_dbi *dbi; - dbi_result result; - int err = 0; + const char *key; + int err = 0, i; struct og_buffer og_buffer = { .data = buffer_reply }; json_object_foreach(element, key, value) { - if (!strcmp(key, "client")) + if (!strcmp(key, "client")) /* alias for backward compatibility */ + err = og_json_parse_clients(value, params); + else if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); else err = -1; @@ -890,50 +934,20 @@ static int og_cmd_get_session(json_t *element, struct og_msg_params *params, return -1; } - result = dbi_conn_queryf(dbi->conn, - "SELECT op.numdisk, op.numpar, nom.nombreso " - "FROM ordenadores o " - "INNER JOIN ordenadores_particiones op " - " ON o.idordenador = op.idordenador " - "INNER JOIN nombresos nom " - " ON op.idnombreso = nom.idnombreso " - "WHERE o.ip = '%s'", - params->ips_array[0]); - 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; - } - array = json_array(); if (!array) { - dbi_result_free(result); og_dbi_close(dbi); return -1; } - while (dbi_result_next_row(result)) { - item = json_object(); - if (!item) { - dbi_result_free(result); - og_dbi_close(dbi); + for (i = 0; i < params->ips_array_len; i++) { + if (og_json_os_array_get(dbi, array, params->ips_array[i]) < 0) { json_decref(array); + og_dbi_close(dbi); return -1; } - - disk = dbi_result_get_uint(result, "numdisk"); - partition = dbi_result_get_uint(result, "numpar"); - os_name = dbi_result_get_string(result, "nombreso"); - - json_object_set_new(item, "disk", json_integer(disk)); - json_object_set_new(item, "partition", json_integer(partition)); - json_object_set_new(item, "name", json_string(os_name)); - json_array_append_new(array, item); } - dbi_result_free(result); og_dbi_close(dbi); root = json_object(); -- cgit v1.2.3-18-g5258