handle vm config modified
This commit is contained in:
@@ -6,7 +6,7 @@ templ Header() {
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8"/>
|
<meta charset="UTF-8"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
<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>
|
<title>vCTP API</title>
|
||||||
<script src="/assets/js/htmx@v2.0.2.min.js"></script>
|
<script src="/assets/js/htmx@v2.0.2.min.js"></script>
|
||||||
<link href={ "/assets/css/output@" + version.Value + ".css" } rel="stylesheet"/>
|
<link href={ "/assets/css/output@" + version.Value + ".css" } rel="stylesheet"/>
|
||||||
|
@@ -31,7 +31,7 @@ func Header() templ.Component {
|
|||||||
templ_7745c5c3_Var1 = templ.NopComponent
|
templ_7745c5c3_Var1 = templ.NopComponent
|
||||||
}
|
}
|
||||||
ctx = templ.ClearChildren(ctx)
|
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 {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
@@ -304,7 +304,7 @@ func (q *Queries) GetInventoryVmId(ctx context.Context, vmid sql.NullString) (In
|
|||||||
|
|
||||||
const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec
|
const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec
|
||||||
UPDATE "Inventory"
|
UPDATE "Inventory"
|
||||||
SET "DeletionTime" = ?1
|
SET "DeletionTime" = ?1
|
||||||
WHERE "VmId" = ?2 AND "Datacenter" = ?3
|
WHERE "VmId" = ?2 AND "Datacenter" = ?3
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@@ -1,17 +1,24 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"vctp/db/queries"
|
||||||
models "vctp/server/models"
|
models "vctp/server/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// VmModify receives the CloudEvent for a VM modification or move
|
// VmModify receives the CloudEvent for a VM modification or move
|
||||||
func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var configChanges []map[string]string
|
||||||
|
params := queries.CreateUpdateParams{}
|
||||||
|
|
||||||
reqBody, err := io.ReadAll(r.Body)
|
reqBody, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.Logger.Error("Invalid data received", "error", err)
|
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")
|
h.Logger.Debug("Received event contains no config change")
|
||||||
} else {
|
} else {
|
||||||
h.Logger.Debug("Received event contains config change info")
|
h.Logger.Debug("Received event contains config change info")
|
||||||
config := h.processConfigChanges(event.CloudEvent.Data.ConfigChanges.Modified)
|
configChanges = h.processConfigChanges(event.CloudEvent.Data.ConfigChanges.Modified)
|
||||||
prettyPrint(config)
|
prettyPrint(configChanges)
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
var found = false
|
||||||
// Map to hold the JSON data
|
// Only interested in vCPU or ram changes currently
|
||||||
var config map[string]interface{}
|
for _, change := range configChanges {
|
||||||
|
//fmt.Printf("Type: %s, New Value: %s\n", change["type"], change["newValue"])
|
||||||
// Unmarshal the JSON into the map
|
switch change["type"] {
|
||||||
if len(*event.CloudEvent.Data.ConfigSpec) > 0 {
|
case "config.hardware.numCPU":
|
||||||
err := json.Unmarshal([]byte(*event.CloudEvent.Data.ConfigSpec), &config)
|
i, err := strconv.ParseInt(change["newValue"], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.Logger.Error("unable to decode json", "error", err)
|
h.Logger.Error("Unable to convert new value to int64", "new_value", change["newValue"])
|
||||||
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
|
} else {
|
||||||
return
|
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))
|
h.Logger.Debug("received update request", "body", string(reqBody))
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@@ -66,7 +96,7 @@ func (h *Handler) processConfigChanges(configChanges string) []map[string]string
|
|||||||
changes := regexp.MustCompile(`\n+`).Split(configChanges, -1)
|
changes := regexp.MustCompile(`\n+`).Split(configChanges, -1)
|
||||||
|
|
||||||
// Regular expression to match config type and the new value after '->' or '<-'
|
// 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
|
// Result will hold a list of changes with type and new value
|
||||||
var result []map[string]string
|
var result []map[string]string
|
||||||
|
Reference in New Issue
Block a user