more debugging
Some checks are pending
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
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-13 09:39:13 +10:00
parent f0278388cb
commit 424474f4fd
2 changed files with 19 additions and 7 deletions

View File

@@ -17,7 +17,7 @@ import (
)
type Vcenter struct {
logger *slog.Logger
Logger *slog.Logger
ctx context.Context
client *govmomi.Client
}
@@ -34,7 +34,7 @@ func New(logger *slog.Logger) *Vcenter {
//defer cancel()
return &Vcenter{
logger: logger,
Logger: logger,
ctx: context.Background(),
}
}
@@ -67,7 +67,7 @@ func (v *Vcenter) Login(vUrl string) error {
c, err := govmomi.NewClient(v.ctx, u, insecure)
if err != nil {
v.logger.Error("Unable to connect to vCenter", "error", err)
v.Logger.Error("Unable to connect to vCenter", "error", err)
return fmt.Errorf("unable to connect to vCenter : %s", err)
}
@@ -75,6 +75,8 @@ func (v *Vcenter) Login(vUrl string) error {
v.client = c
v.Logger.Debug("successfully connected to vCenter")
return nil
}

View File

@@ -25,6 +25,7 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
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
@@ -38,6 +39,9 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
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
@@ -55,6 +59,7 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
}
// 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)
@@ -62,14 +67,17 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
if err != nil {
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()
err = vc.Logout()
if err != nil {
h.Logger.Error("unable to logout of vcenter", "error", err)
}
// Create an instance of CreateInventoryParams
params := queries.CreateInventoryParams{
@@ -77,7 +85,7 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
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: "VirtualMachine-" + vm.CloudEvent.Data.VM.VM.Value, Valid: vm.CloudEvent.Data.VM.VM.Value != ""},
VmId: sql.NullString{String: 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},
@@ -93,9 +101,11 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
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))
//h.Logger.Debug("received create request", "body", string(reqBody))
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Create Request : %v\n", result)
}