more json output
This commit is contained in:
@@ -15,13 +15,18 @@ import (
|
|||||||
|
|
||||||
// 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) {
|
||||||
|
// Read request body
|
||||||
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)
|
h.Logger.Error("Invalid data received", "length", len(reqBody), "error", err)
|
||||||
fmt.Fprintf(w, "Invalid data received")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ERROR",
|
||||||
|
"message": fmt.Sprintf("Invalid data received: '%s'", err),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
h.Logger.Debug("received input data", "length", len(reqBody))
|
h.Logger.Debug("received input data", "length", len(reqBody))
|
||||||
}
|
}
|
||||||
@@ -29,8 +34,13 @@ func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Decode the JSON body into CloudEventReceived struct
|
// Decode the JSON body into CloudEventReceived struct
|
||||||
var inData models.ImportReceived
|
var inData models.ImportReceived
|
||||||
if err := json.Unmarshal(reqBody, &inData); err != nil {
|
if err := json.Unmarshal(reqBody, &inData); err != nil {
|
||||||
h.Logger.Error("unable to decode json", "error", err)
|
h.Logger.Error("Unable to decode json request body", "length", len(reqBody), "error", err)
|
||||||
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
|
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
|
return
|
||||||
} else {
|
} else {
|
||||||
h.Logger.Debug("successfully decoded JSON")
|
h.Logger.Debug("successfully decoded JSON")
|
||||||
@@ -55,7 +65,7 @@ func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Convert vmModel to CreateInventoryParams using the utility function
|
// Convert vmModel to CreateInventoryParams using the utility function
|
||||||
db.ConvertToSQLParams(&inData, ¶ms)
|
db.ConvertToSQLParams(&inData, ¶ms)
|
||||||
prettyPrint(params)
|
//prettyPrint(params)
|
||||||
|
|
||||||
// Insert the new inventory record into the database
|
// Insert the new inventory record into the database
|
||||||
result, err := h.Database.Queries().CreateInventory(ctx, params)
|
result, err := h.Database.Queries().CreateInventory(ctx, params)
|
||||||
@@ -72,7 +82,11 @@ func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
|
|||||||
h.Logger.Info("not adding vm to inventory table since record alraedy exists", "vm_id", inData.VmId, "datacenter_name", inData.Datacenter)
|
h.Logger.Info("not adding vm to inventory table since record alraedy exists", "vm_id", inData.VmId, "datacenter_name", inData.Datacenter)
|
||||||
}
|
}
|
||||||
|
|
||||||
//h.Logger.Debug("received import request", "body", string(reqBody))
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
fmt.Fprintf(w, "Import Request (%d): %v\n", len(reqBody), string(reqBody))
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "OK",
|
||||||
|
"message": fmt.Sprintf("Successfully processed import request for VM '%s'", inData.Name),
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user