85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"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
|
|
|
|
reqBody, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
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
|
|
}
|
|
|
|
// Convert vmModel to CreateInventoryParams using the utility function
|
|
//var params queries.CreateInventoryParams
|
|
//db.ConvertToSQLParams(&vm, ¶ms)
|
|
|
|
// 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
|
|
vc := vcenter.New(h.Logger)
|
|
vc.Login(vm.CloudEvent.Source)
|
|
vmObject, err := vc.FindVMByName(vm.CloudEvent.Data.VM.Name)
|
|
if err != nil {
|
|
h.Logger.Error("Can't locate vm in vCenter", "vmName", vm.CloudEvent.Data.VM.Name, "error", err)
|
|
} else {
|
|
h.Logger.Debug("found VM", "object", vmObject)
|
|
}
|
|
vc.Logout()
|
|
|
|
// Create an instance of CreateInventoryParams
|
|
params := queries.CreateInventoryParams{
|
|
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 != ""},
|
|
CreationTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
|
|
}
|
|
|
|
h.Logger.Debug("database params", "vm", vm)
|
|
|
|
// 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
|
|
}
|
|
|
|
h.Logger.Debug("received create request", "body", string(reqBody))
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprintf(w, "Create Request : %v\n", result)
|
|
}
|