cleanups and code fixes incl templ
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,10 +2,8 @@ package handler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"vctp/db"
|
||||
@@ -25,26 +23,19 @@ import (
|
||||
// @Failure 500 {object} models.ErrorResponse "Server error"
|
||||
// @Router /api/import/vm [post]
|
||||
func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeJSONError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
if h.denyLegacyAPI(w, "/api/import/vm") {
|
||||
return
|
||||
}
|
||||
|
||||
// Read request body
|
||||
reqBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
h.Logger.Error("Invalid data received", "length", len(reqBody), "error", err)
|
||||
writeJSONError(w, http.StatusInternalServerError, 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)
|
||||
writeJSONError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to decode json request body: '%s'", err))
|
||||
if err := decodeJSONBody(w, r, &inData); err != nil {
|
||||
h.Logger.Error("Unable to decode json request body", "error", err)
|
||||
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("Unable to decode json request body: '%s'", err))
|
||||
return
|
||||
} else {
|
||||
//h.Logger.Debug("successfully decoded JSON")
|
||||
@@ -66,7 +57,7 @@ func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
_, err := h.Database.Queries().GetInventoryVmId(ctx, invParams)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
|
||||
Reference in New Issue
Block a user