mirror of https://git.48k.eu/ogserver
#942 Create DB image when calling POST /image/create
In case the DB entry for an image does not exist when POST /image/create is called, this patch takes care of calling it. This adds few optional json parameters to the POST /image/create API. If optional parameters are included then this patch creates the DB entry, otherwise it just creates the actual image and updates the existing entry. Request: POST /image/create { "clients":["192.168.56.11"], "disk":"1", "partition":"1", "name":"archlinux", "repository":"192.168.56.10", "id":"24", "code":"131", "description":"This is a test", "group_id":0, "center_id":1 } Response: 200 OKmaster
parent
24c8b940e6
commit
d2f20d0be0
36
src/dbi.c
36
src/dbi.c
|
@ -123,3 +123,39 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define OG_UNASSIGNED_SW_ID 0
|
||||||
|
#define OG_DEFAULT_REPO_ID 1
|
||||||
|
#define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */
|
||||||
|
|
||||||
|
int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image)
|
||||||
|
{
|
||||||
|
const char *msglog;
|
||||||
|
dbi_result result;
|
||||||
|
|
||||||
|
result = dbi_conn_queryf(dbi->conn,
|
||||||
|
"INSERT INTO imagenes (nombreca, "
|
||||||
|
"descripcion, "
|
||||||
|
"idperfilsoft, "
|
||||||
|
"idcentro, "
|
||||||
|
"comentarios, "
|
||||||
|
"grupoid, "
|
||||||
|
"idrepositorio, "
|
||||||
|
"tipo, "
|
||||||
|
"ruta) "
|
||||||
|
"VALUES ('%s', '%s', %u, %lu, '', %u, %lu, %u, '')",
|
||||||
|
image->name, image->description,
|
||||||
|
OG_UNASSIGNED_SW_ID, image->center_id,
|
||||||
|
image->group_id, OG_DEFAULT_REPO_ID,
|
||||||
|
OG_IMAGE_DEFAULT_TYPE);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
dbi_conn_error(dbi->conn, &msglog);
|
||||||
|
syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n",
|
||||||
|
__func__, __LINE__, msglog);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
dbi_result_free(result);
|
||||||
|
return dbi_conn_sequence_last(dbi->conn, NULL);
|
||||||
|
}
|
||||||
|
|
13
src/dbi.h
13
src/dbi.h
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <dbi/dbi.h>
|
#include <dbi/dbi.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
struct og_dbi_config {
|
struct og_dbi_config {
|
||||||
const char *user;
|
const char *user;
|
||||||
|
@ -24,6 +25,7 @@ void og_dbi_close(struct og_dbi *db);
|
||||||
#define OG_DB_CENTER_NAME_MAXLEN 100
|
#define OG_DB_CENTER_NAME_MAXLEN 100
|
||||||
#define OG_DB_ROOM_NAME_MAXLEN 100
|
#define OG_DB_ROOM_NAME_MAXLEN 100
|
||||||
#define OG_DB_SERIAL_NUMBER_MAXLEN 25
|
#define OG_DB_SERIAL_NUMBER_MAXLEN 25
|
||||||
|
#define OG_DB_IMAGE_DESCRIPTION_MAXLEN 250
|
||||||
#define OG_DB_IMAGE_NAME_MAXLEN 50
|
#define OG_DB_IMAGE_NAME_MAXLEN 50
|
||||||
#define OG_DB_FILESYSTEM_MAXLEN 16
|
#define OG_DB_FILESYSTEM_MAXLEN 16
|
||||||
#define OG_DB_NETDRIVER_MAXLEN 30
|
#define OG_DB_NETDRIVER_MAXLEN 30
|
||||||
|
@ -46,6 +48,16 @@ struct og_image_legacy {
|
||||||
char code[OG_DB_INT8_MAXLEN + 1];
|
char code[OG_DB_INT8_MAXLEN + 1];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct og_image {
|
||||||
|
char name[OG_DB_IMAGE_NAME_MAXLEN + 1];
|
||||||
|
char description[OG_DB_IMAGE_DESCRIPTION_MAXLEN + 1];
|
||||||
|
const char *filename;
|
||||||
|
uint64_t center_id;
|
||||||
|
uint64_t datasize;
|
||||||
|
uint64_t group_id;
|
||||||
|
struct stat image_stats;
|
||||||
|
};
|
||||||
|
|
||||||
struct og_legacy_partition {
|
struct og_legacy_partition {
|
||||||
char partition[OG_DB_SMALLINT_MAXLEN + 1];
|
char partition[OG_DB_SMALLINT_MAXLEN + 1];
|
||||||
char code[OG_DB_INT8_MAXLEN + 1];
|
char code[OG_DB_INT8_MAXLEN + 1];
|
||||||
|
@ -77,5 +89,6 @@ struct og_computer {
|
||||||
struct in_addr;
|
struct in_addr;
|
||||||
int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
|
int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
|
||||||
struct in_addr addr);
|
struct in_addr addr);
|
||||||
|
int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
40
src/rest.c
40
src/rest.c
|
@ -1589,12 +1589,6 @@ static int og_cmd_software(json_t *element, struct og_msg_params *params)
|
||||||
|
|
||||||
#define OG_IMAGE_TYPE_MAXLEN 4
|
#define OG_IMAGE_TYPE_MAXLEN 4
|
||||||
|
|
||||||
struct og_image {
|
|
||||||
const char *filename;
|
|
||||||
uint64_t datasize;
|
|
||||||
struct stat image_stats;
|
|
||||||
};
|
|
||||||
|
|
||||||
static int og_get_image_stats(const char *name,
|
static int og_get_image_stats(const char *name,
|
||||||
struct stat *image_stats)
|
struct stat *image_stats)
|
||||||
{
|
{
|
||||||
|
@ -1744,7 +1738,10 @@ static int og_cmd_images(char *buffer_reply)
|
||||||
|
|
||||||
static int og_cmd_create_image(json_t *element, struct og_msg_params *params)
|
static int og_cmd_create_image(json_t *element, struct og_msg_params *params)
|
||||||
{
|
{
|
||||||
|
char new_image_id[OG_DB_INT_MAXLEN + 1];
|
||||||
|
struct og_image image = {};
|
||||||
json_t *value, *clients;
|
json_t *value, *clients;
|
||||||
|
struct og_dbi *dbi;
|
||||||
const char *key;
|
const char *key;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
|
@ -1772,8 +1769,17 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params)
|
||||||
} else if (!strcmp(key, "code")) {
|
} else if (!strcmp(key, "code")) {
|
||||||
err = og_json_parse_string(value, ¶ms->code);
|
err = og_json_parse_string(value, ¶ms->code);
|
||||||
params->flags |= OG_REST_PARAM_CODE;
|
params->flags |= OG_REST_PARAM_CODE;
|
||||||
|
} else if (!strcmp(key, "description")) {
|
||||||
|
err = og_json_parse_string_copy(value,
|
||||||
|
image.description,
|
||||||
|
sizeof(image.description));
|
||||||
|
} else if (!strcmp(key, "group_id")) {
|
||||||
|
err = og_json_parse_uint64(value, &image.group_id);
|
||||||
|
} else if (!strcmp(key, "center_id")) {
|
||||||
|
err = og_json_parse_uint64(value, &image.center_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (err < 0)
|
if (err < 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1787,6 +1793,28 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params)
|
||||||
OG_REST_PARAM_REPO))
|
OG_REST_PARAM_REPO))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
/* If there is a description, this means the image is not in the DB. */
|
||||||
|
if (image.description[0]) {
|
||||||
|
snprintf(image.name, sizeof(image.name), "%s", params->name);
|
||||||
|
|
||||||
|
dbi = og_dbi_open(&ogconfig.db);
|
||||||
|
if (!dbi) {
|
||||||
|
syslog(LOG_ERR,
|
||||||
|
"cannot open connection database (%s:%d)\n",
|
||||||
|
__func__, __LINE__);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = og_dbi_add_image(dbi, &image);
|
||||||
|
|
||||||
|
og_dbi_close(dbi);
|
||||||
|
if (err < 0)
|
||||||
|
return err;
|
||||||
|
|
||||||
|
snprintf(new_image_id, sizeof(new_image_id), "%u", err);
|
||||||
|
params->id = new_image_id;
|
||||||
|
}
|
||||||
|
|
||||||
clients = json_copy(element);
|
clients = json_copy(element);
|
||||||
json_object_del(clients, "clients");
|
json_object_del(clients, "clients");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue