|
| 1 | +package sweepers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "github.com/equinix/equinix-sdk-go/services/fabricv4" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +func testSweepServiceProfiles() error { |
| 13 | + var errs []error |
| 14 | + log.Printf("[DEBUG] Sweeping Fabric Service Profiles") |
| 15 | + ctx := context.Background() |
| 16 | + fabric, err := newFabricClient() |
| 17 | + |
| 18 | + limit := int32(100) |
| 19 | + offset := int32(0) |
| 20 | + equalOperator := string(fabricv4.EXPRESSIONOPERATOR_EQUAL) |
| 21 | + state := string(fabricv4.SERVICEPROFILESORTBY_STATE) |
| 22 | + |
| 23 | + serviceProfileSearchRequest := fabricv4.ServiceProfileSearchRequest{ |
| 24 | + Filter: &fabricv4.ServiceProfileFilter{ |
| 25 | + ServiceProfileAndFilter: &fabricv4.ServiceProfileAndFilter{ |
| 26 | + And: []fabricv4.ServiceProfileSimpleExpression{ |
| 27 | + { |
| 28 | + Property: &state, |
| 29 | + Operator: &equalOperator, |
| 30 | + Values: []string{string(fabricv4.SERVICEPROFILESTATEENUM_ACTIVE), string(fabricv4.SERVICEPROFILESTATEENUM_PENDING_APPROVAL)}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + Pagination: &fabricv4.PaginationRequest{ |
| 36 | + Limit: &limit, |
| 37 | + Offset: &offset, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + fabricServiceProfiles, _, err := fabric.ServiceProfilesApi.SearchServiceProfiles(ctx).ServiceProfileSearchRequest(serviceProfileSearchRequest).Execute() |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("error getting fabric service profiles list for sweeping fabric service profiles: %s", err) |
| 45 | + } |
| 46 | + |
| 47 | + for _, serviceProfile := range fabricServiceProfiles.Data { |
| 48 | + if isSweepableFabricTestResource(serviceProfile.GetName()) { |
| 49 | + log.Printf("[DEBUG] Deleting Service Profile: %s", serviceProfile.GetName()) |
| 50 | + _, resp, err := fabric.ServiceProfilesApi.DeleteServiceProfileByUuid(ctx, serviceProfile.GetUuid()).Execute() |
| 51 | + if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusNotFound { |
| 52 | + errs = append(errs, fmt.Errorf("error deleting fabric service profile: %s", err)) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return errors.Join(errs...) |
| 58 | +} |
0 commit comments