Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions .github/workflows/unit-tests.yaml

This file was deleted.

77 changes: 77 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,44 @@ servers:

paths:
/rules:
delete:
operationId: BulkDeleteUserDefinedAlertRules
summary: Bulk delete user-defined alert rules
description: >
Deletes one or more user-defined alert rules by their stable IDs.
Each rule is deleted independently; per-rule status is returned in
the response so partial success is visible to the caller.
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/BulkDeleteAlertRulesRequest"
responses:
"200":
description: Deletion results (may include per-rule errors)
content:
application/json:
schema:
$ref: "#/components/schemas/BulkDeleteAlertRulesResponse"
"400":
description: Invalid request body
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
"401":
description: Missing or invalid authorization token
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
"500":
description: Unexpected server error
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
post:
operationId: CreateAlertRule
summary: Create an alert rule
Expand Down Expand Up @@ -141,6 +179,45 @@ components:
type: string
description: Computed stable ID for the created alert rule.

BulkDeleteAlertRulesRequest:
type: object
required:
- ruleIds
properties:
ruleIds:
type: array
minItems: 1
items:
type: string
description: List of stable alert rule IDs to delete.

DeleteAlertRuleResult:
type: object
required:
- id
- status_code
properties:
id:
type: string
description: The stable alert rule ID that was processed.
status_code:
type: integer
description: HTTP status code for this rule's deletion result.
message:
type: string
description: Error message if deletion failed; omitted on success.

BulkDeleteAlertRulesResponse:
type: object
required:
- rules
properties:
rules:
type: array
items:
$ref: "#/components/schemas/DeleteAlertRuleResult"
description: Per-rule deletion results.

ErrorResponse:
type: object
required:
Expand Down
46 changes: 46 additions & 0 deletions internal/managementrouter/api_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/managementrouter/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package managementrouter
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -98,3 +101,15 @@ func parseError(err error) (int, string) {
log.WithError(err).Error("unexpected management API error")
return http.StatusInternalServerError, "An unexpected error occurred"
}

func parseParam(raw string, name string) (string, error) {
decoded, err := url.PathUnescape(raw)
if err != nil {
return "", fmt.Errorf("invalid %s encoding", name)
}
value := strings.TrimSpace(decoded)
if value == "" {
return "", fmt.Errorf("missing %s", name)
}
return value, nil
}
56 changes: 56 additions & 0 deletions internal/managementrouter/user_defined_alert_rule_bulk_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package managementrouter

import (
"encoding/json"
"net/http"
)

// BulkDeleteUserDefinedAlertRules implements ServerInterface.
func (hr *httpRouter) BulkDeleteUserDefinedAlertRules(w http.ResponseWriter, req *http.Request) {
req.Body = http.MaxBytesReader(w, req.Body, maxRequestBodyBytes)

var payload BulkDeleteAlertRulesRequest
if err := json.NewDecoder(req.Body).Decode(&payload); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if len(payload.RuleIds) == 0 {
writeError(w, http.StatusBadRequest, "ruleIds is required")
return
}

results := make([]DeleteAlertRuleResult, 0, len(payload.RuleIds))

for _, rawId := range payload.RuleIds {
id, err := parseParam(rawId, "ruleId")
if err != nil {
msg := err.Error()
results = append(results, DeleteAlertRuleResult{
Id: rawId,
StatusCode: http.StatusBadRequest,
Message: &msg,
})
continue
}

if err := hr.managementClient.DeleteUserDefinedAlertRuleById(req.Context(), id); err != nil {
status, message := parseError(err)
results = append(results, DeleteAlertRuleResult{
Id: id,
StatusCode: status,
Message: &message,
})
continue
}
results = append(results, DeleteAlertRuleResult{
Id: id,
StatusCode: http.StatusNoContent,
})
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(BulkDeleteAlertRulesResponse{Rules: results}); err != nil {
log.WithError(err).Warn("failed to encode bulk delete response")
}
}
Loading