Files
vctp2/server/handler/vmCreate.go
Nathan Coad 03701f9566
Some checks are pending
continuous-integration/drone/push Build is passing
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
search for vm with datacenter info in cloud event
2024-09-13 10:08:36 +10:00

147 lines
4.6 KiB
Go

package handler
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
"runtime"
"strconv"
"time"
queries "vctp/db/queries"
"vctp/internal/vcenter"
models "vctp/server/models"
)
// VmCreate receives the CloudEvent for a VM creation
func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
var (
unixTimestamp int64
numVcpus int32
numRam int32
datacenter string
)
reqBody, err := io.ReadAll(r.Body)
if err != nil {
h.Logger.Error("Invalid data received", "error", err)
fmt.Fprintf(w, "Invalid data received")
w.WriteHeader(http.StatusInternalServerError)
return
} else {
h.Logger.Debug("received input data", "length", len(reqBody))
}
// Decode the JSON body into vmModel struct
var vm models.CloudEventReceived
if err := json.Unmarshal(reqBody, &vm); err != nil {
h.Logger.Error("unable to decode json", "error", err)
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
} else {
h.Logger.Debug("successfully decoded JSON")
prettyPrint(vm)
}
// Convert vmModel to CreateInventoryParams using the utility function
//var params queries.CreateInventoryParams
//db.ConvertToSQLParams(&vm, &params)
// Parse the datetime string to a time.Time object
eventTime, err := time.Parse(time.RFC3339, vm.CloudEvent.Time)
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()
}
// TODO - initiate govmomi query of source vcenter to discover related data
h.Logger.Debug("connecting to vcenter")
vc := vcenter.New(h.Logger)
vc.Login(vm.CloudEvent.Source)
//vmObject, err := vc.FindVMByName(vm.CloudEvent.Data.VM.Name)
//vmObject, err := vc.FindVMByID(vm.CloudEvent.Data.VM.VM.Value)
vmObject, err := vc.FindVMByIDWithDatacenter(vm.CloudEvent.Data.VM.VM.Value, vm.CloudEvent.Data.Datacenter.Datacenter.Value)
if err != nil {
h.Logger.Error("Can't locate vm in vCenter", "vmID", vm.CloudEvent.Data.VM.VM.Value, "error", err)
} else if vmObject == nil {
h.Logger.Debug("didn't find VM", "vm_id", vm.CloudEvent.Data.VM.VM.Value)
numRam = 0
numVcpus = 0
datacenter = ""
} else {
h.Logger.Debug("found VM", "object", vmObject)
//prettyPrint(vmObject)
// calculate VM properties we want to store
numRam = vmObject.Vm.Config.Hardware.MemoryMB
numVcpus = vmObject.Vm.Config.Hardware.NumCPU * vmObject.Vm.Config.Hardware.NumCoresPerSocket
}
err = vc.Logout()
if err != nil {
h.Logger.Error("unable to logout of vcenter", "error", err)
}
// Create an instance of CreateInventoryParams
params := queries.CreateInventoryParams{
Name: vm.CloudEvent.Data.VM.Name,
Vcenter: vm.CloudEvent.Source,
EventId: sql.NullString{String: vm.CloudEvent.ID, Valid: vm.CloudEvent.ID != ""},
EventKey: sql.NullString{String: strconv.Itoa(vm.CloudEvent.Data.Key), Valid: strconv.Itoa(vm.CloudEvent.Data.Key) != ""},
VmId: sql.NullString{String: vm.CloudEvent.Data.VM.VM.Value, Valid: vm.CloudEvent.Data.VM.VM.Value != ""},
Datacenter: sql.NullString{String: datacenter, Valid: datacenter != ""},
CreationTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
InitialVcpus: sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 0},
InitialRam: sql.NullInt64{Int64: int64(numRam), Valid: numRam > 0},
}
h.Logger.Debug("database params", "params", params)
// Insert the new inventory record into the database
result, err := h.Database.Queries().CreateInventory(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 create request", "body", string(reqBody))
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Create Request : %v\n", result)
}
func prettyPrint(args ...interface{}) {
var caller string
timeNow := time.Now().Format("01-02-2006 15:04:05")
prefix := fmt.Sprintf("[%s] %s -- ", "PrettyPrint", timeNow)
_, fileName, fileLine, ok := runtime.Caller(1)
if ok {
caller = fmt.Sprintf("%s:%d", fileName, fileLine)
} else {
caller = ""
}
fmt.Printf("\n%s%s\n", prefix, caller)
if len(args) == 2 {
label := args[0]
value := args[1]
s, _ := json.MarshalIndent(value, "", "\t")
fmt.Printf("%s%s: %s\n", prefix, label, string(s))
} else {
s, _ := json.MarshalIndent(args, "", "\t")
fmt.Printf("%s%s\n", prefix, string(s))
}
}