handle vm config modified
Some checks are pending
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-16 11:40:06 +10:00
parent ab24b5f6b9
commit 32e3bc6e66
4 changed files with 49 additions and 19 deletions

View File

@@ -1,17 +1,24 @@
package handler
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"vctp/db/queries"
models "vctp/server/models"
)
// VmModify receives the CloudEvent for a VM modification or move
func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
var configChanges []map[string]string
params := queries.CreateUpdateParams{}
reqBody, err := io.ReadAll(r.Body)
if err != nil {
h.Logger.Error("Invalid data received", "error", err)
@@ -37,24 +44,47 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
h.Logger.Debug("Received event contains no config change")
} else {
h.Logger.Debug("Received event contains config change info")
config := h.processConfigChanges(event.CloudEvent.Data.ConfigChanges.Modified)
prettyPrint(config)
}
configChanges = h.processConfigChanges(event.CloudEvent.Data.ConfigChanges.Modified)
prettyPrint(configChanges)
/*
// Map to hold the JSON data
var config map[string]interface{}
// Unmarshal the JSON into the map
if len(*event.CloudEvent.Data.ConfigSpec) > 0 {
err := json.Unmarshal([]byte(*event.CloudEvent.Data.ConfigSpec), &config)
if err != nil {
h.Logger.Error("unable to decode json", "error", err)
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
var found = false
// Only interested in vCPU or ram changes currently
for _, change := range configChanges {
//fmt.Printf("Type: %s, New Value: %s\n", change["type"], change["newValue"])
switch change["type"] {
case "config.hardware.numCPU":
i, err := strconv.ParseInt(change["newValue"], 10, 64)
if err != nil {
h.Logger.Error("Unable to convert new value to int64", "new_value", change["newValue"])
} else {
found = true
params.NewVcpus = sql.NullInt64{Int64: i, Valid: i > 0}
}
case "config.hardware.memoryMB":
i, err := strconv.ParseInt(change["newValue"], 10, 64)
if err != nil {
h.Logger.Error("Unable to convert new value to int64", "new_value", change["newValue"])
} else {
found = true
params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0}
}
}
}
*/
// Only create a database record if we found one of the config changes we were interested in
if found {
result, err := h.Database.Queries().CreateUpdate(context.Background(), params)
if err != nil {
h.Logger.Error("unable to perform database insert", "error", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error : %v\n", err)
return
} else {
h.Logger.Debug("created database record", "insert_result", result)
}
}
}
h.Logger.Debug("received update request", "body", string(reqBody))
w.WriteHeader(http.StatusOK)
@@ -66,7 +96,7 @@ func (h *Handler) processConfigChanges(configChanges string) []map[string]string
changes := regexp.MustCompile(`\n+`).Split(configChanges, -1)
// Regular expression to match config type and the new value after '->' or '<-'
re := regexp.MustCompile(`(?P<type>[^\s]+): \d+ -[><] (?P<newValue>[^;]+);`)
re := regexp.MustCompile(`(?P<type>[^\s]+): [^\s]+ -[><] (?P<newValue>[^;]+);`)
// Result will hold a list of changes with type and new value
var result []map[string]string