improve responses
This commit is contained in:
@@ -29,31 +29,42 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
reqBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
h.Logger.Error("Invalid data received", "error", err)
|
||||
fmt.Fprintf(w, "Invalid data received")
|
||||
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 event models.CloudEventReceived
|
||||
if err := json.Unmarshal(reqBody, &event); err != nil {
|
||||
h.Logger.Error("unable to decode json", "error", err)
|
||||
prettyPrint(reqBody)
|
||||
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
|
||||
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")
|
||||
//h.Logger.Debug("successfully decoded JSON")
|
||||
//prettyPrint(event)
|
||||
}
|
||||
|
||||
if event.CloudEvent.Data.ConfigChanges == nil {
|
||||
h.Logger.Warn("Received event contains no config change")
|
||||
prettyPrint(event)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
fmt.Fprintf(w, "Processed update event but no config changes were found\n")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"status": "OK",
|
||||
"message": fmt.Sprintf("Received update event successfully but no config changes were found"),
|
||||
})
|
||||
return
|
||||
} else {
|
||||
h.Logger.Debug("Received event contains config change info", "source", event.CloudEvent.Source,
|
||||
"id", event.CloudEvent.ID,
|
||||
@@ -128,10 +139,6 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO - recalculate total disk size
|
||||
//h.Logger.Debug("Detected config change for VM disk")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +186,10 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
h.Logger.Debug("created database record", "insert_result", result)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "Processed update event: %v\n", result)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"status": "OK",
|
||||
"message": fmt.Sprintf("Successfully processed vm modify event"),
|
||||
})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user