summaryrefslogtreecommitdiffstats
path: root/src/rest.c
diff options
context:
space:
mode:
authorJavier Sánchez Parra <jsanchez@soleta.eu>2022-05-19 16:48:29 +0200
committerJavier Sánchez Parra <jsanchez@soleta.eu>2022-05-26 15:15:29 +0200
commitd5e6dc0990cb22efc05dda405dfa1e49f60359f5 (patch)
tree26d604c7d4fbb6b25ae7a58e7c107b18c0a84ac0 /src/rest.c
parent9b6171300b2b4d52f2d3d519a9b17393fcf0293c (diff)
#915 Add API GET /repositories
This API returns a list of available images repositories. Request: GET /repositories Response 200 OK { "repositories": [ { "ip": "192.168.56.10", "name": "Default" }, { "ip": "192.168.57.10", "name": "Extra" } ] }
Diffstat (limited to 'src/rest.c')
-rw-r--r--src/rest.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/rest.c b/src/rest.c
index 7daf09f..05492e6 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -1885,6 +1885,54 @@ static int og_cmd_get_software(json_t *element, struct og_msg_params *params,
return 0;
}
+static const int og_cmd_get_repositories(char *buffer_reply)
+{
+ json_t *root, *repositories, *repository, *ip, *name;
+ struct og_buffer og_buffer = {
+ .data = buffer_reply,
+ };
+ struct og_dbi *dbi;
+ dbi_result result;
+
+ 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 ip, nombrerepositorio "
+ "FROM repositorios");
+
+ repositories = json_array();
+
+ while (dbi_result_next_row(result)) {
+ repository = json_object();
+ ip = json_string(dbi_result_get_string(result, "ip"));
+ name = json_string(dbi_result_get_string(result,
+ "nombrerepositorio"));
+ json_object_set_new(repository, "ip", ip);
+ json_object_set_new(repository, "name", name);
+ json_array_append_new(repositories, repository);
+ }
+
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+
+ root = json_object();
+ json_object_set_new(root, "repositories", repositories);
+
+ if (json_dump_callback(root, og_json_dump_clients, &og_buffer, 0)) {
+ json_decref(root);
+ return -1;
+ }
+
+ json_decref(root);
+
+ return 0;
+}
+
#define OG_IMAGE_TYPE_MAXLEN 4
static int og_get_image_stats(const char *name,
@@ -5828,6 +5876,18 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_software(root, &params);
else
err = og_cmd_get_software(root, &params, buf_reply);
+ } else if (!strncmp(cmd, "repositories", strlen("repositories"))) {
+ if (method != OG_METHOD_GET) {
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
+
+ if (root) {
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+
+ err = og_cmd_get_repositories(buf_reply);
} else if (!strncmp(cmd, "images", strlen("images"))) {
if (method != OG_METHOD_GET) {
err = og_client_method_not_found(cli);