some vm properties are populated
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
queries "vctp/db/queries"
|
||||
"vctp/internal/vcenter"
|
||||
@@ -15,7 +16,11 @@ import (
|
||||
|
||||
// VmCreate receives the CloudEvent for a VM creation
|
||||
func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
|
||||
var unixTimestamp int64
|
||||
var (
|
||||
unixTimestamp int64
|
||||
numVcpus int32
|
||||
numRam int32
|
||||
)
|
||||
|
||||
reqBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -51,11 +56,17 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO - initiate govmomi query of source vcenter to discover related data
|
||||
vc := vcenter.New(h.Logger)
|
||||
vc.Login(vm.CloudEvent.Source)
|
||||
vmObject, err := vc.FindVMByName(vm.CloudEvent.Data.VM.Name)
|
||||
//vmObject, err := vc.FindVMByName(vm.CloudEvent.Data.VM.Name)
|
||||
vmObject, err := vc.FindVMByID(vm.CloudEvent.Data.VM.VM.Value)
|
||||
if err != nil {
|
||||
h.Logger.Error("Can't locate vm in vCenter", "vmName", vm.CloudEvent.Data.VM.Name, "error", err)
|
||||
h.Logger.Error("Can't locate vm in vCenter", "vmID", vm.CloudEvent.Data.VM.VM.Value, "error", err)
|
||||
} else {
|
||||
h.Logger.Debug("found VM", "object", vmObject)
|
||||
//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
|
||||
}
|
||||
vc.Logout()
|
||||
|
||||
@@ -64,7 +75,10 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
|
||||
Name: vm.CloudEvent.Data.VM.Name,
|
||||
Vcenter: vm.CloudEvent.Source,
|
||||
VmId: sql.NullString{String: "VirtualMachine-" + vm.CloudEvent.Data.VM.VM.Value, Valid: vm.CloudEvent.Data.VM.VM.Value != ""},
|
||||
Datacenter: sql.NullString{String: vmObject.Datacenter, Valid: vmObject.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", "vm", vm)
|
||||
@@ -82,3 +96,30 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user