diff options
author | Roberto Hueso Gómez <rhueso@soleta.eu> | 2020-10-16 13:24:12 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2020-10-16 14:40:33 +0200 |
commit | 25c1c16e99a2fbc19002b2ca06d0266f5276d3a8 (patch) | |
tree | 810ccf6585a84c3a0bdc83efce99a4624ad98ab7 /src/rest.c | |
parent | d8b78bb9221866a544066b551c6d31188adf4035 (diff) |
#942 Add GET /software REST API function
This function retrieves a list of the software installed in a specific
disk and partition of a client. This list can be updated by using POST
/software beforehand.
Request:
GET /software
{
"client": ["192.168.56.11"],
"disk": 1,
"partition": 1
}
Response:
200 OK
{
"software": [
"grub-pc-bin 2",
"grub-pc 2.02",
"grub2-common 2.02",
"groff-base 1.22.3",
"gpg 2.2.4",
"gnupg-utils 2.2.4",
"gnupg-l10n 2.2.4",
"gnupg 2.2.4",
"gnome-video-effects 0.4.3",
"gnome-user-guide 3.28.2+git20180715",
"gnome-user-docs 3.28.2+git20180715",
"gnome-todo-common 3.28.1",
"gnome-mines 3.28.0",
"gnome-menus 3.13.3",
"gnome-mahjongg 3.22.0",
"gnome-keyring-pkcs11 3.28.0.2",
"gnome-keyring 3.28.0.2",
"gnome-initial-setup 3.28.0",
"accountsservice 0.6.45"
]
}
Diffstat (limited to 'src/rest.c')
-rw-r--r-- | src/rest.c | 99 |
1 files changed, 97 insertions, 2 deletions
@@ -1587,6 +1587,97 @@ static int og_cmd_software(json_t *element, struct og_msg_params *params) return og_send_request(OG_METHOD_POST, OG_CMD_SOFTWARE, params, clients); } +static int og_cmd_get_software(json_t *element, struct og_msg_params *params, + char *buffer_reply) +{ + json_t *value, *software, *root; + const char *key, *msglog, *name; + uint64_t disk, partition; + uint64_t flags = 0; + struct og_dbi *dbi; + dbi_result result; + int err = 0; + + struct og_buffer og_buffer = { + .data = buffer_reply + }; + + if (json_typeof(element) != JSON_OBJECT) + return -1; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "client")) { + err = og_json_parse_clients(value, params); + } else if (!strcmp(key, "disk")) { + err = og_json_parse_uint64(value, &disk); + flags |= OG_REST_PARAM_DISK; + } else if (!strcmp(key, "partition")) { + err = og_json_parse_uint64(value, &partition); + flags |= OG_REST_PARAM_PARTITION; + } + + if (err < 0) + break; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR) || + !og_flags_validate(flags, OG_REST_PARAM_DISK | + OG_REST_PARAM_PARTITION)) + 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, + "SELECT s.descripcion " + "FROM softwares s " + "INNER JOIN perfilessoft_softwares pss " + "ON s.idsoftware = pss.idsoftware " + "INNER JOIN ordenadores_particiones op " + "ON pss.idperfilsoft = op.idperfilsoft " + "INNER JOIN ordenadores o " + "ON o.idordenador = op.idordenador " + "WHERE o.ip='%s' AND " + " op.numdisk=%lu AND " + " op.numpar=%lu", + params->ips_array[0], disk, partition); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + software = json_array(); + if (!software) { + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + + while (dbi_result_next_row(result)) { + name = dbi_result_get_string(result, "descripcion"); + json_array_append_new(software, json_string(name)); + } + + dbi_result_free(result); + og_dbi_close(dbi); + + root = json_object(); + if (!root) { + json_decref(software); + return -1; + } + json_object_set_new(root, "software", software); + + json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); + json_decref(root); + return 0; +} + #define OG_IMAGE_TYPE_MAXLEN 4 static int og_get_image_stats(const char *name, @@ -3742,14 +3833,18 @@ int og_client_state_process_payload_rest(struct og_client *cli) else if (method == OG_METHOD_POST) err = og_cmd_hardware(root, ¶ms); } else if (!strncmp(cmd, "software", strlen("software"))) { - if (method != OG_METHOD_POST) + if (method != OG_METHOD_POST && method != OG_METHOD_GET) return og_client_method_not_found(cli); if (!root) { syslog(LOG_ERR, "command software with no payload\n"); return og_client_bad_request(cli); } - err = og_cmd_software(root, ¶ms); + + if (method == OG_METHOD_POST) + err = og_cmd_software(root, ¶ms); + else + err = og_cmd_get_software(root, ¶ms, buf_reply); } else if (!strncmp(cmd, "images", strlen("images"))) { if (method != OG_METHOD_GET) return og_client_method_not_found(cli); |