handle vm config modified
This commit is contained in:
@@ -6,7 +6,7 @@ templ Header() {
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<meta name="description" content="Hello world"/>
|
||||
<meta name="description" content="vCTP API endpoint"/>
|
||||
<title>vCTP API</title>
|
||||
<script src="/assets/js/htmx@v2.0.2.min.js"></script>
|
||||
<link href={ "/assets/css/output@" + version.Value + ".css" } rel="stylesheet"/>
|
||||
|
@@ -31,7 +31,7 @@ func Header() templ.Component {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta name=\"description\" content=\"Hello world\"><title>Test Page</title><script src=\"/assets/js/htmx@v2.0.2.min.js\"></script><link href=\"")
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta name=\"description\" content=\"vCTP API endpoint\"><title>vCTP API</title><script src=\"/assets/js/htmx@v2.0.2.min.js\"></script><link href=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
@@ -304,7 +304,7 @@ func (q *Queries) GetInventoryVmId(ctx context.Context, vmid sql.NullString) (In
|
||||
|
||||
const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec
|
||||
UPDATE "Inventory"
|
||||
SET "DeletionTime" = ?1
|
||||
SET "DeletionTime" = ?1
|
||||
WHERE "VmId" = ?2 AND "Datacenter" = ?3
|
||||
`
|
||||
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user