From 677fab7f70412eaa1dc1e7e39ff030b0de790655 Mon Sep 17 00:00:00 2001 From: srushti-patl Date: Thu, 1 Aug 2024 13:20:43 -0700 Subject: [PATCH] feat: Adding Sweeper Test for Fabric Cloud Router Resource --- equinix/equinix_sweeper_test.go | 6 +- .../resource_fabric_cloud_router_sweeper.go | 69 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 equinix/resource_fabric_cloud_router_sweeper.go diff --git a/equinix/equinix_sweeper_test.go b/equinix/equinix_sweeper_test.go index b37a0ed4b..203c2e29a 100644 --- a/equinix/equinix_sweeper_test.go +++ b/equinix/equinix_sweeper_test.go @@ -8,13 +8,13 @@ import ( "time" "github.com/equinix/terraform-provider-equinix/internal/config" - "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) const tstResourcePrefix = "tfacc" func TestMain(m *testing.M) { + addTestSweeper() resource.TestMain(m) } @@ -48,3 +48,7 @@ func sharedConfigForRegion(region string) (*config.Config, error) { func isSweepableTestResource(namePrefix string) bool { return strings.HasPrefix(namePrefix, tstResourcePrefix) } + +func addTestSweeper() { + AddTestSweeper() +} diff --git a/equinix/resource_fabric_cloud_router_sweeper.go b/equinix/resource_fabric_cloud_router_sweeper.go new file mode 100644 index 000000000..68bb03c9a --- /dev/null +++ b/equinix/resource_fabric_cloud_router_sweeper.go @@ -0,0 +1,69 @@ +package equinix + +import ( + "context" + "errors" + "fmt" + "github.com/equinix/equinix-sdk-go/services/fabricv4" + equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" + "github.com/equinix/terraform-provider-equinix/internal/sweep" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "log" +) + +func AddTestSweeper() { + resource.AddTestSweepers("equinix_fabric_cloud_router", &resource.Sweeper{ + Name: "equinix_fabric_cloud_router", + Dependencies: []string{}, + F: testSweepCloudRouters, + }) +} + +func testSweepCloudRouters(region string) error { + var errs []error + log.Printf("[DEBUG] Sweeping Fabric Cloud Routers") + ctx := context.Background() + meta, err := sweep.GetConfigForFabric() + if err != nil { + return fmt.Errorf("error getting configuration for sweeping Fabric Cloud Routers: %s", err) + } + meta.Load(ctx) + fabric := meta.NewFabricClientForTesting() + + equinixStatus := "/state" + equalOperator := string(fabricv4.EXPRESSIONOPERATOR_EQUAL) + limit := int32(100) + cloudRouterSearchRequest := fabricv4.CloudRouterSearchRequest{ + Filter: &fabricv4.CloudRouterFilters{ + And: []fabricv4.CloudRouterFilter{ + { + CloudRouterSimpleExpression: &fabricv4.CloudRouterSimpleExpression{ + Property: &equinixStatus, + Operator: &equalOperator, + Values: []string{string(fabricv4.EQUINIXSTATUS_PROVISIONED)}, + }, + }, + }, + }, + Pagination: &fabricv4.PaginationRequest{ + Limit: &limit, + }, + } + + fabricCloudRouters, _, err := fabric.CloudRoutersApi.SearchCloudRouters(ctx).CloudRouterSearchRequest(cloudRouterSearchRequest).Execute() + if err != nil { + return fmt.Errorf("error getting cloud router list for sweeping fabric cloud routers: %s", err) + } + + for _, cloudRouter := range fabricCloudRouters.Data { + if sweep.IsSweepableFabricTestResource(cloudRouter.GetName()) { + log.Printf("[DEBUG] Deleting Cloud Routers: %s", cloudRouter.GetName()) + resp, err := fabric.CloudRoutersApi.DeleteCloudRouterByUuid(ctx, cloudRouter.GetUuid()).Execute() + if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { + errs = append(errs, fmt.Errorf("error deleting fabric Cloud Router: %s", err)) + } + } + } + + return errors.Join(errs...) +}