add more fields to the Update database record
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"vctp/db/queries"
|
||||
models "vctp/server/models"
|
||||
)
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
||||
var configChanges []map[string]string
|
||||
params := queries.CreateUpdateParams{}
|
||||
var unixTimestamp int64
|
||||
|
||||
reqBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -76,6 +78,39 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Only create a database record if we found one of the config changes we were interested in
|
||||
if found {
|
||||
// lookup Iid from Inventory table for this VM
|
||||
// also figure out what to do if we didn't find an entry for this VM in the Inventory table. Create one?
|
||||
h.Logger.Debug("Checking inventory table for VM record")
|
||||
invParams := queries.GetInventoryVmIdParams{
|
||||
VmId: sql.NullString{String: event.CloudEvent.Data.VM.VM.Value, Valid: event.CloudEvent.Data.VM.VM.Value != ""},
|
||||
DatacenterName: sql.NullString{String: event.CloudEvent.Data.Datacenter.Name, Valid: event.CloudEvent.Data.Datacenter.Name != ""},
|
||||
}
|
||||
invResult, err := h.Database.Queries().GetInventoryVmId(context.Background(), invParams)
|
||||
|
||||
if err != nil {
|
||||
h.Logger.Error("unable to find existing inventory record for this VM", "error", err)
|
||||
// TODO - how to handle?
|
||||
} else {
|
||||
params.InventoryId = sql.NullInt64{Int64: invResult.Iid, Valid: invResult.Iid > 0}
|
||||
}
|
||||
|
||||
// Parse the datetime string to a time.Time object
|
||||
eventTime, err := time.Parse(time.RFC3339, event.CloudEvent.Data.CreatedTime)
|
||||
if err != nil {
|
||||
h.Logger.Warn("unable to convert cloud event time to timestamp", "error", err)
|
||||
unixTimestamp = time.Now().Unix()
|
||||
} else {
|
||||
// Convert to Unix timestamp
|
||||
unixTimestamp = eventTime.Unix()
|
||||
}
|
||||
|
||||
// populate other parameters for the Update database record
|
||||
params.EventId = sql.NullString{String: event.CloudEvent.ID, Valid: event.CloudEvent.ID != ""}
|
||||
params.EventKey = sql.NullString{String: strconv.Itoa(event.CloudEvent.Data.Key), Valid: event.CloudEvent.Data.Key > 0}
|
||||
params.UpdateTime = sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0}
|
||||
params.UpdateType = "reconfigure"
|
||||
|
||||
// Create the Update database record
|
||||
result, err := h.Database.Queries().CreateUpdate(context.Background(), params)
|
||||
if err != nil {
|
||||
h.Logger.Error("unable to perform database insert", "error", err)
|
||||
|
Reference in New Issue
Block a user