Skip to content

Commit 2651d2f

Browse files
committed
feat(enrollment patterns): Query enrollment patterns by name instead of via pagination
1 parent 21225bd commit 2651d2f

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

internal/command/command.go

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -562,45 +562,38 @@ func ptr[T any](v T) *T {
562562
}
563563

564564
// getEnrollmentPatternByName retrieves an enrollment pattern by its name from Command.
565-
// It paginates through the results until it finds the pattern or exhausts all pages.
566565
func getEnrollmentPatternByName(ctx context.Context, log logr.Logger, s *signer, enrollmentPatternName string) (*v1.EnrollmentPatternsEnrollmentPatternResponse, error) {
567566
log.Info(fmt.Sprintf("Looking up enrollment pattern %q in Command...", enrollmentPatternName))
568567

569568
var model *v1.EnrollmentPatternsEnrollmentPatternResponse
570569

571-
pageNumber := 1
570+
queryString := fmt.Sprintf("Name -eq \"%s\"", enrollmentPatternName)
571+
patterns, httpResp, err := s.client.GetEnrollmentPatterns(v1.ApiGetEnrollmentPatternsRequest{}.QueryString(queryString))
572572

573-
for model == nil {
574-
patterns, httpResp, err := s.client.GetEnrollmentPatterns(v1.ApiGetEnrollmentPatternsRequest{}.
575-
PageReturned(int32(pageNumber)))
576-
577-
if err != nil {
578-
// Capture the error message which should indicate the failure reason
579-
msg := ""
580-
if httpResp != nil && httpResp.Body != nil {
581-
defer httpResp.Body.Close()
582-
bodyBytes, _ := io.ReadAll(httpResp.Body)
583-
msg += string(bodyBytes)
584-
}
585-
detail := fmt.Sprintf("error fetching enrollment patterns from Command: %s. Details: %s", err, msg)
586-
return nil, fmt.Errorf("%w: %s: %w", errEnrollmentPatternFailure, detail, err)
587-
}
588-
589-
if len(patterns) == 0 {
590-
detail := fmt.Sprintf("enrollment pattern not found: %s", enrollmentPatternName)
591-
return nil, fmt.Errorf("%w: %s", errEnrollmentPatternFailure, detail)
573+
if err != nil {
574+
// Capture the error message which should indicate the failure reason
575+
msg := ""
576+
if httpResp != nil && httpResp.Body != nil {
577+
defer httpResp.Body.Close()
578+
bodyBytes, _ := io.ReadAll(httpResp.Body)
579+
msg += string(bodyBytes)
592580
}
581+
detail := fmt.Sprintf("error fetching enrollment patterns from Command: %s. Details: %s", err, msg)
582+
return nil, fmt.Errorf("%w: %s: %w", errEnrollmentPatternFailure, detail, err)
583+
}
593584

594-
pageNumber++
585+
if len(patterns) == 0 {
586+
detail := fmt.Sprintf("enrollment pattern not found: %s", enrollmentPatternName)
587+
return nil, fmt.Errorf("%w: %s", errEnrollmentPatternFailure, detail)
588+
}
595589

596-
for _, pattern := range patterns {
597-
if pattern.Name.Get() != nil && *pattern.Name.Get() == enrollmentPatternName {
598-
model = &pattern
599-
break
600-
}
601-
}
590+
if len(patterns) > 1 {
591+
detail := fmt.Sprintf("multiple enrollment patterns found: %s", enrollmentPatternName)
592+
return nil, fmt.Errorf("%w: %s", errEnrollmentPatternFailure, detail)
602593
}
603594

595+
model = &patterns[0]
596+
604597
log.Info(fmt.Sprintf("Enrollment pattern %s found in Command", enrollmentPatternName))
605598

606599
return model, nil

0 commit comments

Comments
 (0)