Deprecate legacy VM inventory endpoints and add gating logic
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:
@@ -67,3 +67,49 @@ func TestVmCreateEventHonorsLegacyGate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLegacyInventoryEndpointsAreGatedWhenDisabled(t *testing.T) {
|
||||||
|
h := newLegacyGateHandler(false)
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
method string
|
||||||
|
path string
|
||||||
|
body string
|
||||||
|
call func(*Handler, *httptest.ResponseRecorder, *http.Request)
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "import vm",
|
||||||
|
method: http.MethodPost,
|
||||||
|
path: "/api/import/vm",
|
||||||
|
body: `{"name":"vm1"}`,
|
||||||
|
call: func(h *Handler, rr *httptest.ResponseRecorder, req *http.Request) { h.VmImport(rr, req) },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "cleanup vm",
|
||||||
|
method: http.MethodDelete,
|
||||||
|
path: "/api/inventory/vm/delete?vm_id=vm-1&datacenter_name=dc1",
|
||||||
|
call: func(h *Handler, rr *httptest.ResponseRecorder, req *http.Request) { h.VmCleanup(rr, req) },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "update vm details",
|
||||||
|
method: http.MethodPost,
|
||||||
|
path: "/api/inventory/vm/update",
|
||||||
|
call: func(h *Handler, rr *httptest.ResponseRecorder, req *http.Request) { h.VmUpdateDetails(rr, req) },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body))
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
tc.call(h, rr, req)
|
||||||
|
if rr.Code != http.StatusGone {
|
||||||
|
t.Fatalf("expected %d, got %d", http.StatusGone, rr.Code)
|
||||||
|
}
|
||||||
|
if !strings.Contains(rr.Body.String(), "deprecated") {
|
||||||
|
t.Fatalf("expected deprecated response, got: %s", rr.Body.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,9 +10,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// VmCleanup removes a VM from inventory by ID and datacenter.
|
// VmCleanup removes a VM from inventory by ID and datacenter.
|
||||||
// @Summary Cleanup VM inventory entry
|
// @Summary Cleanup VM inventory entry (deprecated)
|
||||||
// @Description Removes a VM inventory entry by VM ID and datacenter name.
|
// @Description Deprecated: Removes a VM inventory entry by VM ID and datacenter name.
|
||||||
// @Tags inventory
|
// @Tags inventory
|
||||||
|
// @Deprecated
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param vm_id query string true "VM ID"
|
// @Param vm_id query string true "VM ID"
|
||||||
// @Param datacenter_name query string true "Datacenter name"
|
// @Param datacenter_name query string true "Datacenter name"
|
||||||
@@ -20,6 +21,10 @@ import (
|
|||||||
// @Failure 400 {object} models.ErrorResponse "Invalid request"
|
// @Failure 400 {object} models.ErrorResponse "Invalid request"
|
||||||
// @Router /api/inventory/vm/delete [delete]
|
// @Router /api/inventory/vm/delete [delete]
|
||||||
func (h *Handler) VmCleanup(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) VmCleanup(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if h.denyLegacyAPI(w, "/api/inventory/vm/delete") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// Get the parameters
|
// Get the parameters
|
||||||
|
|||||||
@@ -15,9 +15,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// VmImport ingests a bulk VM import payload.
|
// VmImport ingests a bulk VM import payload.
|
||||||
// @Summary Import VMs
|
// @Summary Import VMs (deprecated)
|
||||||
// @Description Imports existing VM inventory data in bulk.
|
// @Description Deprecated: Imports existing VM inventory data in bulk.
|
||||||
// @Tags inventory
|
// @Tags inventory
|
||||||
|
// @Deprecated
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param import body models.ImportReceived true "Bulk import payload"
|
// @Param import body models.ImportReceived true "Bulk import payload"
|
||||||
@@ -25,6 +26,10 @@ import (
|
|||||||
// @Failure 500 {object} models.ErrorResponse "Server error"
|
// @Failure 500 {object} models.ErrorResponse "Server error"
|
||||||
// @Router /api/import/vm [post]
|
// @Router /api/import/vm [post]
|
||||||
func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if h.denyLegacyAPI(w, "/api/import/vm") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Read request body
|
// Read request body
|
||||||
reqBody, err := io.ReadAll(r.Body)
|
reqBody, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -9,14 +9,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// VmUpdateDetails refreshes inventory metadata from vCenter.
|
// VmUpdateDetails refreshes inventory metadata from vCenter.
|
||||||
// @Summary Refresh VM details
|
// @Summary Refresh VM details (deprecated)
|
||||||
// @Description Queries vCenter and updates inventory records with missing details.
|
// @Description Deprecated: Queries vCenter and updates inventory records with missing details.
|
||||||
// @Tags inventory
|
// @Tags inventory
|
||||||
|
// @Deprecated
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 200 {object} models.StatusMessageResponse "Update completed"
|
// @Success 200 {object} models.StatusMessageResponse "Update completed"
|
||||||
// @Failure 500 {object} models.ErrorResponse "Server error"
|
// @Failure 500 {object} models.ErrorResponse "Server error"
|
||||||
// @Router /api/inventory/vm/update [post]
|
// @Router /api/inventory/vm/update [post]
|
||||||
func (h *Handler) VmUpdateDetails(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) VmUpdateDetails(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if h.denyLegacyAPI(w, "/api/inventory/vm/update") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var matchFound bool
|
var matchFound bool
|
||||||
var inventoryId int64
|
var inventoryId int64
|
||||||
var srmPlaceholder string
|
var srmPlaceholder string
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st
|
|||||||
mux.HandleFunc("/api/inventory/vm/delete", h.VmCleanup)
|
mux.HandleFunc("/api/inventory/vm/delete", h.VmCleanup)
|
||||||
|
|
||||||
// add missing data to VMs
|
// add missing data to VMs
|
||||||
//mux.HandleFunc("/api/inventory/vm/update", h.VmUpdateDetails)
|
mux.HandleFunc("/api/inventory/vm/update", h.VmUpdateDetails)
|
||||||
|
|
||||||
// Legacy/maintenance endpoints are gated by settings.enable_legacy_api.
|
// Legacy/maintenance endpoints are gated by settings.enable_legacy_api.
|
||||||
mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
|
mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
|
||||||
|
|||||||
Reference in New Issue
Block a user