more debugging
This commit is contained in:
@@ -17,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Vcenter struct {
|
type Vcenter struct {
|
||||||
logger *slog.Logger
|
Logger *slog.Logger
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
client *govmomi.Client
|
client *govmomi.Client
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ func New(logger *slog.Logger) *Vcenter {
|
|||||||
//defer cancel()
|
//defer cancel()
|
||||||
|
|
||||||
return &Vcenter{
|
return &Vcenter{
|
||||||
logger: logger,
|
Logger: logger,
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ func (v *Vcenter) Login(vUrl string) error {
|
|||||||
|
|
||||||
c, err := govmomi.NewClient(v.ctx, u, insecure)
|
c, err := govmomi.NewClient(v.ctx, u, insecure)
|
||||||
if err != nil {
|
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)
|
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.client = c
|
||||||
|
|
||||||
|
v.Logger.Debug("successfully connected to vCenter")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,6 +25,7 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
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)
|
||||||
fmt.Fprintf(w, "Invalid data received")
|
fmt.Fprintf(w, "Invalid data received")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -38,6 +39,9 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
h.Logger.Error("unable to decode json", "error", err)
|
h.Logger.Error("unable to decode json", "error", err)
|
||||||
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
|
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
h.Logger.Debug("successfully decoded JSON")
|
||||||
|
prettyPrint(vm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert vmModel to CreateInventoryParams using the utility function
|
// 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
|
// TODO - initiate govmomi query of source vcenter to discover related data
|
||||||
|
h.Logger.Debug("connecting to vcenter")
|
||||||
vc := vcenter.New(h.Logger)
|
vc := vcenter.New(h.Logger)
|
||||||
vc.Login(vm.CloudEvent.Source)
|
vc.Login(vm.CloudEvent.Source)
|
||||||
//vmObject, err := vc.FindVMByName(vm.CloudEvent.Data.VM.Name)
|
//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 {
|
if err != nil {
|
||||||
h.Logger.Error("Can't locate vm in vCenter", "vmID", vm.CloudEvent.Data.VM.VM.Value, "error", err)
|
h.Logger.Error("Can't locate vm in vCenter", "vmID", vm.CloudEvent.Data.VM.VM.Value, "error", err)
|
||||||
} else {
|
} else {
|
||||||
//h.Logger.Debug("found VM", "object", vmObject)
|
h.Logger.Debug("found VM", "object", vmObject)
|
||||||
//prettyPrint(vmObject)
|
//prettyPrint(vmObject)
|
||||||
|
|
||||||
// calculate VM properties we want to store
|
// calculate VM properties we want to store
|
||||||
numRam = vmObject.Vm.Config.Hardware.MemoryMB
|
numRam = vmObject.Vm.Config.Hardware.MemoryMB
|
||||||
numVcpus = vmObject.Vm.Config.Hardware.NumCPU * vmObject.Vm.Config.Hardware.NumCoresPerSocket
|
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
|
// Create an instance of CreateInventoryParams
|
||||||
params := queries.CreateInventoryParams{
|
params := queries.CreateInventoryParams{
|
||||||
@@ -77,7 +85,7 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
Vcenter: vm.CloudEvent.Source,
|
Vcenter: vm.CloudEvent.Source,
|
||||||
EventId: sql.NullString{String: vm.CloudEvent.ID, Valid: vm.CloudEvent.ID != ""},
|
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) != ""},
|
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 != ""},
|
Datacenter: sql.NullString{String: vmObject.Datacenter, Valid: vmObject.Datacenter != ""},
|
||||||
CreationTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
|
CreationTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
|
||||||
InitialVcpus: sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 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)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
fmt.Fprintf(w, "Error : %v\n", err)
|
fmt.Fprintf(w, "Error : %v\n", err)
|
||||||
return
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
fmt.Fprintf(w, "Create Request : %v\n", result)
|
fmt.Fprintf(w, "Create Request : %v\n", result)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user