Changes to start using the new Go API

master
Ronald 1 year ago
parent 37465d6b09
commit a96e986461

@ -2,4 +2,7 @@
LEVEL = "DEBUG"
[API]
ADDRESS = "http://localhost:8000"
; Python/FASTAPI
; ADDRESS = "http://localhost:8000"
; Go/GIN
ADDRESS = "http://localhost:8080/api/v1"

@ -53,22 +53,36 @@ api_request_get :: proc(endpoint: string) -> (body: client.Body_Type, allocation
// Returns the host ID, or if the host doesn't exist in
// LSM then it will return -1
get_host_id :: proc(hostname: string) -> (int, bool) {
endpoint := fmt.tprintf("%s/get_host_id_for_hostname/%s", PC.api_address, hostname)
get_host_id :: proc(hostname: string) -> (host_id: int, ok: bool) {
request: client.Request
client.request_init(&request, .Get)
defer client.request_destroy(&request)
body, allocation, ok := api_request_get(endpoint)
if !ok {
return 0, ok
endpoint := fmt.tprintf("%s/hosts/get_host_id_for_hostname/%s", PC.api_address, hostname)
response, err := client.request(&request, endpoint)
if err != nil {
return 0, false
}
defer client.body_destroy(body, allocation)
defer client.response_destroy(&response)
host_id := strconv.atoi(body.(client.Body_Plain))
body, allocation, body_err := client.response_body(&response)
if body_err != nil {
return 0, false
}
if response.status == .Not_Found && strings.contains(body.(client.Body_Plain), "record not found") {
return -1, true
} else if response.status != .OK {
return 0, false
}
host_id = strconv.atoi(body.(client.Body_Plain))
return host_id, true
}
get_cpus_for_host_from_id :: proc(host_id: int) -> (cpus: [dynamic]CPU, ok: bool) {
endpoint := fmt.tprintf("%s/get_cpus_by_host_id/%d", PC.api_address, host_id)
endpoint := fmt.tprintf("%s/cpus/get_cpus_by_host_id/%d", PC.api_address, host_id)
body, allocation, request_ok := api_request_get(endpoint)
if !request_ok {
@ -115,15 +129,19 @@ add_cpu_to_lsm :: proc(cpu_name: string, socket, cores: int, usage: f64, host_id
defer client.request_destroy(&request)
post_body := Base_CPU{cpu_name, socket, cores, usage, host_id}
log.info("TEST TEST TEST", post_body)
if err := client.with_json(&request, post_body); err != nil {
return false
}
response, err := client.request(&request, fmt.tprintf("%s/add_cpu_to_host/", PC.api_address))
if err != nil do log.fatal("failed to add host to api with the error: %s", err)
response, err := client.request(&request, fmt.tprintf("%s/cpus/add_cpu_to_host", PC.api_address))
if err != nil {
log.fatal("failed to add host to api with the error: ", err)
os.exit(1)
}
defer client.response_destroy(&response)
log.debugf("got response status %v when attempting to add cpu to lsm", response.status)
if response.status != .Created {
return false
}
@ -202,14 +220,23 @@ _main :: proc() {
// TODO: Handle this
}
response, err := client.request(&request, fmt.tprintf("%s/add_host/", PC.api_address))
if err != nil do log.fatal("failed to add host to api with the error: %s", err)
response, err := client.request(&request, fmt.tprintf("%s/hosts/add_host", PC.api_address))
if err != nil {
log.fatal("failed to add host to LSM with the error: %s", err)
return
}
defer client.response_destroy(&response)
if response.status == .Created {
log.info("successfully added host to lsm")
PC.host_id, ok = get_host_id(PC.hostname)
if !ok do log.fatal("failed to get host id, exiting")
if !ok {
log.fatal("failed to get host id, exiting")
return
}
} else {
log.fatal("failed to add host to LSM got status code:", response.status)
return
}
} else {
log.info("host found in lsm")

Loading…
Cancel
Save