feature: handle delete operation for IPs

This commit is contained in:
ayurishchev committed 2026-08-23 22:24:55 +03:00
1 parent 4f597505cc
commit f8336740ad
20 files changed
+1083 -5

No files matched your search

+13
View File
@@ -17,6 +17,19 @@ type submitIPsResponse struct {
SkippedInProgress []string `json:"skipped_in_progress"`
}
type deleteIPsRequest struct {
Addresses []string `json:"addresses"`
}
type deleteIPsResponse struct {
Deleted []string `json:"deleted"`
NotFound []string `json:"not_found"`
}
type clearQueueResponse struct {
Deleted []string `json:"deleted"`
}
type validatorDTO struct {
ValidatorID string `json:"validator_id"`
OSPortID string `json:"os_port_id"`
+48
View File
@@ -111,6 +111,54 @@ func (s *Server) handleAdminCancelIP(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, okResponse{OK: true})
}
// handleAdminDeleteIP permanently removes one address and its full history
// — unlike handleAdminCancelIP, there's nothing left to look up afterward.
// Goes through the orchestrator (not a direct DB call) since a currently
// associated floating IP needs disassociating first.
func (s *Server) handleAdminDeleteIP(w http.ResponseWriter, r *http.Request) {
address := r.PathValue("ip")
if err := s.Orch.DeleteIP(r.Context(), address); err != nil {
writeDBError(w, err)
return
}
writeJSON(w, http.StatusOK, okResponse{OK: true})
}
// handleAdminDeleteIPs permanently removes a specific list of addresses in
// one call — unknown addresses are reported in not_found rather than
// failing the whole request.
func (s *Server) handleAdminDeleteIPs(w http.ResponseWriter, r *http.Request) {
var req deleteIPsRequest
if err := readJSON(r, &req); err != nil {
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
return
}
if len(req.Addresses) == 0 {
writeError(w, http.StatusBadRequest, "addresses must not be empty")
return
}
result, err := s.Orch.DeleteIPs(r.Context(), req.Addresses)
if err != nil {
writeDBError(w, err)
return
}
writeJSON(w, http.StatusOK, deleteIPsResponse{
Deleted: emptyIfNil(result.Deleted),
NotFound: emptyIfNil(result.NotFound),
})
}
// handleAdminClearQueue permanently removes every address currently in the
// queue, including those actively being checked.
func (s *Server) handleAdminClearQueue(w http.ResponseWriter, r *http.Request) {
result, err := s.Orch.ClearQueue(r.Context())
if err != nil {
writeDBError(w, err)
return
}
writeJSON(w, http.StatusOK, clearQueueResponse{Deleted: emptyIfNil(result.Deleted)})
}
// emptyIfNil turns a nil slice into an empty one so these fields always
// marshal as `[]` rather than `null`.
func emptyIfNil(s []string) []string {
+121
View File
@@ -289,3 +289,124 @@ func TestForceCancelMidCheck(t *testing.T) {
t.Fatalf("expected 404 cancelling unknown ip, status=%d body=%s", resp.StatusCode, body)
}
}
// TestDeleteIPMidCheck proves DELETE /admin/ips/{ip} disassociates the
// floating IP and permanently removes the address — unlike cancel, a
// subsequent GET on the same address is a 404, not a cancelled record.
func TestDeleteIPMidCheck(t *testing.T) {
fc, d, orch, mock := newConfigTestHarness(t)
ctx := context.Background()
mock.Seed("fip-1", "9.9.9.9", "svc-project")
fc.do(http.MethodPost, "/api/v1/admin/config/validators", createValidatorRequest{ValidatorID: "validator-1", OSPortID: "port-1"})
fc.do(http.MethodPut, "/api/v1/admin/config/targets/web", putTargetGroupRequest{Targets: []string{"https://example.test"}})
fc.do(http.MethodPut, "/api/v1/admin/config/check-types/https", putCheckTypeRequest{Enabled: true, Targets: []string{"web"}})
fc.do(http.MethodPost, "/api/v1/agents/register", registerAgentRequest{ValidatorID: "validator-1"})
fc.do(http.MethodPost, "/api/v1/admin/ips", submitIPsRequest{Addresses: []string{"9.9.9.9"}})
orch.Tick(ctx) // claim + associate FIP -> awaiting_self_check
if fip, _ := mock.GetFloatingIPByAddress(ctx, "9.9.9.9"); fip.PortID == "" {
t.Fatalf("expected fip associated before delete")
}
resp, body := fc.do(http.MethodDelete, "/api/v1/admin/ips/9.9.9.9", nil)
if resp.StatusCode != http.StatusOK {
t.Fatalf("delete: status=%d body=%s", resp.StatusCode, body)
}
if fip, _ := mock.GetFloatingIPByAddress(ctx, "9.9.9.9"); fip.PortID != "" {
t.Fatalf("expected fip disassociated after delete, still on port %q", fip.PortID)
}
if _, err := d.GetIPByAddress(ctx, "9.9.9.9"); err == nil {
t.Fatalf("expected ip row gone from db after delete")
}
v, err := d.GetValidator(ctx, "validator-1")
if err != nil {
t.Fatalf("get validator: %v", err)
}
if v.State != db.ValidatorIdle || v.CurrentIPID != nil {
t.Fatalf("expected validator freed, got state=%s current_ip=%v", v.State, v.CurrentIPID)
}
resp, body = fc.do(http.MethodGet, "/api/v1/admin/ips/9.9.9.9", nil)
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("expected 404 for deleted ip, status=%d body=%s", resp.StatusCode, body)
}
resp, body = fc.do(http.MethodDelete, "/api/v1/admin/ips/9.9.9.9", nil)
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("expected 404 deleting already-gone ip, status=%d body=%s", resp.StatusCode, body)
}
}
// TestDeleteIPsAndClearQueue exercises POST /admin/ips/delete against a
// mixed known/unknown list, then POST /admin/ips/clear against whatever
// remains in the queue (including an actively checking address).
func TestDeleteIPsAndClearQueue(t *testing.T) {
fc, d, orch, mock := newConfigTestHarness(t)
ctx := context.Background()
mock.Seed("fip-1", "1.1.1.1", "svc-project")
fc.do(http.MethodPost, "/api/v1/admin/config/validators", createValidatorRequest{ValidatorID: "validator-1", OSPortID: "port-1"})
fc.do(http.MethodPut, "/api/v1/admin/config/targets/web", putTargetGroupRequest{Targets: []string{"https://example.test"}})
fc.do(http.MethodPut, "/api/v1/admin/config/check-types/https", putCheckTypeRequest{Enabled: true, Targets: []string{"web"}})
fc.do(http.MethodPost, "/api/v1/agents/register", registerAgentRequest{ValidatorID: "validator-1"})
fc.do(http.MethodPost, "/api/v1/admin/ips", submitIPsRequest{Addresses: []string{"1.1.1.1", "2.2.2.2", "3.3.3.3"}})
orch.Tick(ctx) // claims 1.1.1.1 for validator-1, associates its fip
// POST /admin/ips/delete with an empty list is a 400.
resp, body := fc.do(http.MethodPost, "/api/v1/admin/ips/delete", deleteIPsRequest{})
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("expected 400 for empty delete list, status=%d body=%s", resp.StatusCode, body)
}
// Delete a mix of a queued address and an unknown one.
resp, body = fc.do(http.MethodPost, "/api/v1/admin/ips/delete", deleteIPsRequest{Addresses: []string{"2.2.2.2", "no-such-ip"}})
if resp.StatusCode != http.StatusOK {
t.Fatalf("delete ips: status=%d body=%s", resp.StatusCode, body)
}
var delResp deleteIPsResponse
if err := json.Unmarshal(body, &delResp); err != nil {
t.Fatalf("unmarshal delete response: %v", err)
}
if len(delResp.Deleted) != 1 || delResp.Deleted[0] != "2.2.2.2" {
t.Fatalf("expected 2.2.2.2 deleted, got %+v", delResp)
}
if len(delResp.NotFound) != 1 || delResp.NotFound[0] != "no-such-ip" {
t.Fatalf("expected no-such-ip in not_found, got %+v", delResp)
}
// Clear whatever's left — 1.1.1.1 (mid-check, fip attached) and 3.3.3.3
// (still queued).
resp, body = fc.do(http.MethodPost, "/api/v1/admin/ips/clear", nil)
if resp.StatusCode != http.StatusOK {
t.Fatalf("clear queue: status=%d body=%s", resp.StatusCode, body)
}
var clearResp clearQueueResponse
if err := json.Unmarshal(body, &clearResp); err != nil {
t.Fatalf("unmarshal clear response: %v", err)
}
if len(clearResp.Deleted) != 2 {
t.Fatalf("expected both remaining addresses deleted, got %+v", clearResp)
}
if fip, _ := mock.GetFloatingIPByAddress(ctx, "1.1.1.1"); fip.PortID != "" {
t.Fatalf("expected fip disassociated on clear, still on port %q", fip.PortID)
}
ips, err := d.ListIPs(ctx)
if err != nil {
t.Fatalf("list ips: %v", err)
}
if len(ips) != 0 {
t.Fatalf("expected empty queue after clear, got %+v", ips)
}
v, err := d.GetValidator(ctx, "validator-1")
if err != nil {
t.Fatalf("get validator: %v", err)
}
if v.State != db.ValidatorIdle || v.CurrentIPID != nil {
t.Fatalf("expected validator freed after clear, got state=%s current_ip=%v", v.State, v.CurrentIPID)
}
}
+3
View File
@@ -22,6 +22,9 @@ func (s *Server) routes(mux *http.ServeMux) {
mux.HandleFunc("POST /api/v1/admin/ips", s.handleAdminSubmitIPs)
mux.HandleFunc("GET /api/v1/admin/ips/{ip}", s.handleAdminIPDetail)
mux.HandleFunc("POST /api/v1/admin/ips/{ip}/cancel", s.handleAdminCancelIP)
mux.HandleFunc("DELETE /api/v1/admin/ips/{ip}", s.handleAdminDeleteIP)
mux.HandleFunc("POST /api/v1/admin/ips/delete", s.handleAdminDeleteIPs)
mux.HandleFunc("POST /api/v1/admin/ips/clear", s.handleAdminClearQueue)
mux.HandleFunc("GET /api/v1/admin/validators", s.handleAdminValidators)
mux.HandleFunc("GET /api/v1/admin/config/validators", s.handleConfigListValidators)