From 0baf03242040b22fe4193d8e4dd4193cec12ba20 Mon Sep 17 00:00:00 2001 From: Srushti Patel <137830748+srushti-patl@users.noreply.github.com> Date: Wed, 7 Aug 2024 13:59:59 -0700 Subject: [PATCH] test: Adding Sweeper Test for Fabric Cloud Router Resource (#747) - Added sweeper test for "equinix_fabric_cloud_router" resource --- .../resources/fabric/cloud_router/sweeper.go | 74 +++++++++++++++++++ internal/sweep/sweep_test.go | 3 + 2 files changed, 77 insertions(+) create mode 100644 internal/resources/fabric/cloud_router/sweeper.go diff --git a/internal/resources/fabric/cloud_router/sweeper.go b/internal/resources/fabric/cloud_router/sweeper.go new file mode 100644 index 000000000..ada510a0a --- /dev/null +++ b/internal/resources/fabric/cloud_router/sweeper.go @@ -0,0 +1,74 @@ +package cloud_router + +import ( + "context" + "errors" + "fmt" + "log" + + "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" +) + +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) + } + err = meta.Load(ctx) + if err != nil { + log.Printf("Error loading meta: %v", err) + return err + } + 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...) +} diff --git a/internal/sweep/sweep_test.go b/internal/sweep/sweep_test.go index 3b1ad7780..64722121f 100644 --- a/internal/sweep/sweep_test.go +++ b/internal/sweep/sweep_test.go @@ -3,6 +3,8 @@ package sweep_test import ( "testing" + fabric_cloud_router "github.com/equinix/terraform-provider-equinix/internal/resources/fabric/cloud_router" + fabric_connection "github.com/equinix/terraform-provider-equinix/internal/resources/fabric/connection" "github.com/equinix/terraform-provider-equinix/internal/resources/metal/connection" "github.com/equinix/terraform-provider-equinix/internal/resources/metal/device" @@ -27,6 +29,7 @@ func TestMain(m *testing.M) { func addTestSweepers() { connection.AddTestSweeper() device.AddTestSweeper() + fabric_cloud_router.AddTestSweeper() fabric_connection.AddTestSweeper() organization.AddTestSweeper() project.AddTestSweeper()