rest: update client ip in /client/update

Use idordenador as discriminator value instead of the client's ip.
Update client ip if the new ip is not used by other clients.
master
Alejandro Sirgo Rica 2024-07-08 12:22:55 +02:00
parent e9a8f467f1
commit 8a23579cab
1 changed files with 12 additions and 8 deletions

View File

@ -2199,6 +2199,8 @@ static int og_cmd_post_client_update(json_t *element,
computer.ip, computer.ip,
sizeof(computer.ip)); sizeof(computer.ip));
params->flags |= OG_REST_PARAM_ADDR; params->flags |= OG_REST_PARAM_ADDR;
} else if (!strcmp(key, "id")) {
err = og_json_parse_uint(value, &computer.id);
} else if (!strcmp(key, "serial_number")) { } else if (!strcmp(key, "serial_number")) {
err = og_json_parse_string_copy(value, err = og_json_parse_string_copy(value,
computer.serial_number, computer.serial_number,
@ -2251,8 +2253,8 @@ static int og_cmd_post_client_update(json_t *element,
} }
result = dbi_conn_queryf(dbi->conn, result = dbi_conn_queryf(dbi->conn,
"SELECT ip FROM ordenadores WHERE ip='%s'", "SELECT ip FROM ordenadores WHERE ip='%s' AND idordenador<>'%u'",
computer.ip); computer.ip, computer.id);
if (!result) { if (!result) {
dbi_conn_error(dbi->conn, &msglog); dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
@ -2260,8 +2262,8 @@ static int og_cmd_post_client_update(json_t *element,
og_dbi_close(dbi); og_dbi_close(dbi);
return -1; return -1;
} }
if (dbi_result_get_numrows(result) == 0) { if (dbi_result_get_numrows(result) > 0) {
syslog(LOG_ERR, "client with that IP does not exist: %s\n", syslog(LOG_ERR, "client with IP %s already exist\n",
computer.ip); computer.ip);
dbi_result_free(result); dbi_result_free(result);
og_dbi_close(dbi); og_dbi_close(dbi);
@ -2271,8 +2273,8 @@ static int og_cmd_post_client_update(json_t *element,
dbi_result_free(result); dbi_result_free(result);
result = dbi_conn_queryf(dbi->conn, result = dbi_conn_queryf(dbi->conn,
"SELECT ip FROM ordenadores WHERE ip<>'%s' AND mac='%s'", "SELECT ip FROM ordenadores WHERE mac='%s' AND idordenador<>'%u'",
computer.ip, computer.mac); computer.mac, computer.id);
if (!result) { if (!result) {
dbi_conn_error(dbi->conn, &msglog); dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
@ -2294,6 +2296,7 @@ static int og_cmd_post_client_update(json_t *element,
result = dbi_conn_queryf(dbi->conn, result = dbi_conn_queryf(dbi->conn,
"UPDATE ordenadores" "UPDATE ordenadores"
" SET numserie='%s'," " SET numserie='%s',"
" ip='%s',"
" netdriver='%s'," " netdriver='%s',"
" maintenance=%u," " maintenance=%u,"
" netiface='%s'," " netiface='%s',"
@ -2304,14 +2307,15 @@ static int og_cmd_post_client_update(json_t *element,
" nombreordenador='%s'," " nombreordenador='%s',"
" mac='%s'," " mac='%s',"
" arranque='%s'" " arranque='%s'"
" WHERE ip='%s';", " WHERE idordenador='%u';",
computer.serial_number, computer.serial_number,
computer.ip,
computer.netdriver, computer.maintenance, computer.netdriver, computer.maintenance,
computer.netiface, computer.repo_id, computer.netiface, computer.repo_id,
computer.netmask, computer.remote, computer.netmask, computer.remote,
computer.room, computer.name, computer.room, computer.name,
computer.mac, computer.boot, computer.mac, computer.boot,
computer.ip); computer.id);
if (!result) { if (!result) {
dbi_conn_error(dbi->conn, &msglog); dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to update client in database (%s:%d) %s\n", syslog(LOG_ERR, "failed to update client in database (%s:%d) %s\n",