From 7e557f39797cef143e384a16034db9f2fde05a3a Mon Sep 17 00:00:00 2001 From: Gerrit Date: Mon, 2 Oct 2023 16:17:10 +0200 Subject: [PATCH] Add filters. --- cmd/metal-api/internal/issues/types.go | 2 +- .../internal/service/machine-service.go | 57 ++++++++++++++++--- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/cmd/metal-api/internal/issues/types.go b/cmd/metal-api/internal/issues/types.go index 8b15a672f..6362c4352 100644 --- a/cmd/metal-api/internal/issues/types.go +++ b/cmd/metal-api/internal/issues/types.go @@ -24,7 +24,7 @@ func AllIssueTypes() []IssueType { } } -func newIssueFromType(t IssueType) (issueImpl, error) { +func NewIssueFromType(t IssueType) (issueImpl, error) { switch t { case IssueTypeNoPartition: return &IssueNoPartition{}, nil diff --git a/cmd/metal-api/internal/service/machine-service.go b/cmd/metal-api/internal/service/machine-service.go index c8fd91b39..578612b6d 100644 --- a/cmd/metal-api/internal/service/machine-service.go +++ b/cmd/metal-api/internal/service/machine-service.go @@ -503,7 +503,55 @@ func (r *machineResource) issues(request *restful.Request, response *restful.Res return } - ms := metal.Machines{} + var ( + ms = metal.Machines{} + + severity = issues.IssueSeverityMinor + only []issues.IssueType + omit []issues.IssueType + lastErrorThreshold = issues.DefaultLastErrorThreshold() + ) + + if requestPayload.Severity != "" { + severity, err = issues.SeverityFromString(requestPayload.Severity) + if err != nil { + r.sendError(request, response, httperrors.BadRequest(err)) + return + } + } + + if len(requestPayload.Omit) > 0 { + for _, o := range requestPayload.Omit { + o := o + + _, err := issues.NewIssueFromType(o) + if err != nil { + r.sendError(request, response, httperrors.BadRequest(err)) + return + } + + omit = append(omit, o) + } + } + + if len(requestPayload.Only) > 0 { + for _, o := range requestPayload.Only { + o := o + + _, err := issues.NewIssueFromType(o) + if err != nil { + r.sendError(request, response, httperrors.BadRequest(err)) + return + } + + only = append(only, o) + } + } + + if requestPayload.LastErrorThreshold > 0 { + lastErrorThreshold = requestPayload.LastErrorThreshold + } + err = r.ds.SearchMachines(&requestPayload.MachineSearchQuery, &ms) if err != nil { r.sendError(request, response, defaultError(err)) @@ -516,13 +564,6 @@ func (r *machineResource) issues(request *restful.Request, response *restful.Res return } - var ( - severity = issues.IssueSeverityMinor - only []issues.IssueType - omit []issues.IssueType - lastErrorThreshold = issues.DefaultLastErrorThreshold() - ) - issues, err := issues.FindIssues(&issues.IssueConfig{ Machines: ms, EventContainers: ecs,