From 65b397f59fd39b742c56a9be7da4b8bce8b88eb1 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Fri, 11 Oct 2024 11:22:33 +0200 Subject: rest: fix GET /session payload structure Split partition data by client instead of a single array with every partition. Old payload structure: { 'sessions': [{'disk': 1, 'partition': 1, 'name': 'unknown'}, {'disk': 1, 'partition': 2, 'name': 'unknown'}, {'disk': 1, 'partition': 1, 'name': 'Ubuntu 22.04.4 LTS 64 bits'}] } New payload structure: { 'sessions': [ {'addr': '1.1.1.1', 'partitions': ['disk': 1, 'partition': 1, 'name': 'Ubuntu 22.04.4 LTS 64 bits']}, {'addr': '1.1.1.2', 'partitions': [{'disk': 1, 'partition': 1, 'name': 'unknown'}, {'disk': 1, 'partition': 2, 'name': 'unknown'}]} ] } --- src/rest.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/rest.c b/src/rest.c index dee85e1..a0997a1 100644 --- a/src/rest.c +++ b/src/rest.c @@ -954,7 +954,7 @@ static int og_json_os_array_get(struct og_dbi *dbi, json_t *array, const char *i static int og_cmd_get_session(json_t *element, struct og_msg_params *params, char *buffer_reply) { - json_t *value, *root, *array; + json_t *value, *root, *array, *client_data, *partitions; struct og_dbi *dbi; const char *key; int err = 0, i; @@ -992,11 +992,31 @@ static int og_cmd_get_session(json_t *element, struct og_msg_params *params, } for (i = 0; i < params->ips_array_len; i++) { - if (og_json_os_array_get(dbi, array, params->ips_array[i]) < 0) { + client_data = json_object(); + if (!client_data) { json_decref(array); og_dbi_close(dbi); return -1; } + json_object_set_new(client_data, "addr", json_string(params->ips_array[i])); + + partitions = json_array(); + if (!partitions) { + json_decref(client_data); + json_decref(array); + og_dbi_close(dbi); + return -1; + } + + if (og_json_os_array_get(dbi, partitions, params->ips_array[i]) < 0) { + json_decref(partitions); + json_decref(client_data); + json_decref(array); + og_dbi_close(dbi); + return -1; + } + json_object_set_new(client_data, "partitions", partitions); + json_array_append_new(array, client_data); } og_dbi_close(dbi); -- cgit v1.2.3-18-g5258