Files
vctp2/server/handler/vmImport.go
Nathan Coad 0c2aecd989
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
less logging
2024-09-26 14:35:12 +10:00

93 lines
2.9 KiB
Go

package handler
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"vctp/db"
queries "vctp/db/queries"
models "vctp/server/models"
)
// VmImport is used for bulk import of existing VMs
func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
// Read request body
reqBody, err := io.ReadAll(r.Body)
if err != nil {
h.Logger.Error("Invalid data received", "length", len(reqBody), "error", err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{
"status": "ERROR",
"message": fmt.Sprintf("Invalid data received: '%s'", err),
})
return
} else {
h.Logger.Debug("received input data", "length", len(reqBody))
}
// Decode the JSON body into CloudEventReceived struct
var inData models.ImportReceived
if err := json.Unmarshal(reqBody, &inData); err != nil {
h.Logger.Error("Unable to decode json request body", "length", len(reqBody), "error", err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{
"status": "ERROR",
"message": fmt.Sprintf("Unable to decode json request body: '%s'", err),
})
return
} else {
//h.Logger.Debug("successfully decoded JSON")
//prettyPrint(inData)
}
ctx := context.Background()
// Query Inventory table for this VM before adding it
h.Logger.Debug("Checking inventory table for VM record")
invParams := queries.GetInventoryVmIdParams{
VmId: sql.NullString{String: inData.VmId, Valid: inData.VmId != ""},
DatacenterName: sql.NullString{String: inData.Datacenter, Valid: inData.Datacenter != ""},
}
_, err = h.Database.Queries().GetInventoryVmId(ctx, invParams)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// do the insert
// Create an instance of CreateInventoryParams
var params queries.CreateInventoryParams
// Convert vmModel to CreateInventoryParams using the utility function
db.ConvertToSQLParams(&inData, &params)
//prettyPrint(params)
// Insert the new inventory record into the database
result, err := h.Database.Queries().CreateInventory(ctx, params)
if err != nil {
h.Logger.Error("unable to perform database insert", "error", err)
} else {
h.Logger.Debug("created database record", "insert_result", result)
}
} else {
h.Logger.Error("unable to check inventory for vm", "error", err, "vm_id", inData.VmId, "datacenter_name", inData.Datacenter)
}
} else {
h.Logger.Info("not adding vm to inventory table since record alraedy exists", "vm_id", inData.VmId, "datacenter_name", inData.Datacenter)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"status": "OK",
"message": fmt.Sprintf("Successfully processed import request for VM '%s'", inData.Name),
})
return
}