test converting import data to db params
This commit is contained in:
@@ -1,21 +1,48 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"vctp/db"
|
||||||
|
queries "vctp/db/queries"
|
||||||
|
models "vctp/server/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// VmImport is used for bulk import of existing VMs
|
// VmImport is used for bulk import of existing VMs
|
||||||
func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) VmImport(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
|
||||||
|
} else {
|
||||||
|
h.Logger.Debug("received input data", "length", len(reqBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
h.Logger.Debug("received import request", "body", string(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", "error", err)
|
||||||
|
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
h.Logger.Debug("successfully decoded JSON")
|
||||||
|
prettyPrint(inData)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create an instance of CreateInventoryParams
|
||||||
|
var params queries.CreateInventoryParams
|
||||||
|
|
||||||
|
// Convert vmModel to CreateInventoryParams using the utility function
|
||||||
|
db.ConvertToSQLParams(&inData, ¶ms)
|
||||||
|
|
||||||
|
prettyPrint(params)
|
||||||
|
|
||||||
|
//h.Logger.Debug("received import request", "body", string(reqBody))
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
fmt.Fprintf(w, "Import Request (%d): %v\n", len(reqBody), string(reqBody))
|
fmt.Fprintf(w, "Import Request (%d): %v\n", len(reqBody), string(reqBody))
|
||||||
}
|
}
|
||||||
|
@@ -64,6 +64,21 @@ type CloudEventReceived struct {
|
|||||||
} `json:"cloudEvent"`
|
} `json:"cloudEvent"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ImportReceived struct {
|
||||||
|
Name string `json:"Name"`
|
||||||
|
Vcenter string `json:"Vcenter"`
|
||||||
|
VmId string `json:"VmId"`
|
||||||
|
InitialRam int `json:"InitialRam"`
|
||||||
|
PowerState int `json:"PowerState"`
|
||||||
|
CreationTime int `json:"CreationTime"`
|
||||||
|
InitialVcpus int `json:"InitialVcpus"`
|
||||||
|
ProvisionedDisk float64 `json:"ProvisionedDisk"`
|
||||||
|
Folder string `json:"Folder"`
|
||||||
|
ResourcePool string `json:"ResourcePool"`
|
||||||
|
Datacenter string `json:"Datacenter"`
|
||||||
|
Cluster string `json:"Cluster"`
|
||||||
|
}
|
||||||
|
|
||||||
type ConfigChangesReceived struct {
|
type ConfigChangesReceived struct {
|
||||||
Modified string `json:"modified"`
|
Modified string `json:"modified"`
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user