diff --git a/internal/services/account/testfuncs/sweep.go b/internal/services/account/testfuncs/sweep.go index 0960176e4..e02261a5e 100644 --- a/internal/services/account/testfuncs/sweep.go +++ b/internal/services/account/testfuncs/sweep.go @@ -1,13 +1,9 @@ package accounttestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - accountSDK "github.com/scaleway/scaleway-sdk-go/api/account/v3" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/account/v3/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,28 +14,5 @@ func AddTestSweepers() { } func testSweepAccountProject(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - accountAPI := accountSDK.NewProjectAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the project") - - req := &accountSDK.ProjectAPIListProjectsRequest{} - listProjects, err := accountAPI.ListProjects(req, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("failed to list projects: %w", err) - } - for _, project := range listProjects.Projects { - // Do not delete default project - if project.ID == req.OrganizationID || !acctest.IsTestResource(project.Name) { - continue - } - err = accountAPI.DeleteProject(&accountSDK.ProjectAPIDeleteProjectRequest{ - ProjectID: project.ID, - }) - if err != nil { - return fmt.Errorf("failed to delete project: %w", err) - } - } - return nil - }) + return acctest.Sweep(sweepers.SweepProjects) } diff --git a/internal/services/applesilicon/testfuncs/sweep.go b/internal/services/applesilicon/testfuncs/sweep.go index 71c099dc5..3737ad39f 100644 --- a/internal/services/applesilicon/testfuncs/sweep.go +++ b/internal/services/applesilicon/testfuncs/sweep.go @@ -1,13 +1,10 @@ package applesilicontestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" applesiliconSDK "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/instance/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,24 +15,5 @@ func AddTestSweepers() { } func testSweepAppleSiliconServer(_ string) error { - return acctest.SweepZones([]scw.Zone{scw.ZoneFrPar1}, func(scwClient *scw.Client, zone scw.Zone) error { - asAPI := applesiliconSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the apple silicon instance in (%s)", zone) - listServers, err := asAPI.ListServers(&applesiliconSDK.ListServersRequest{Zone: zone}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing apple silicon servers in (%s) in sweeper: %s", zone, err) - } - - for _, server := range listServers.Servers { - errDelete := asAPI.DeleteServer(&applesiliconSDK.DeleteServerRequest{ - ServerID: server.ID, - Zone: zone, - }) - if errDelete != nil { - return fmt.Errorf("error deleting apple silicon server in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&applesiliconSDK.API{}).Zones(), sweepers.SweepServers) } diff --git a/internal/services/baremetal/testfuncs/sweep.go b/internal/services/baremetal/testfuncs/sweep.go index ed17b57b5..04b2bf3cc 100644 --- a/internal/services/baremetal/testfuncs/sweep.go +++ b/internal/services/baremetal/testfuncs/sweep.go @@ -1,13 +1,10 @@ package baremetaltestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" baremetalSDK "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,25 +15,5 @@ func AddTestSweepers() { } func testSweepServer(_ string) error { - return acctest.SweepZones([]scw.Zone{scw.ZoneFrPar2}, func(scwClient *scw.Client, zone scw.Zone) error { - baremetalAPI := baremetalSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the baremetal server in (%s)", zone) - listServers, err := baremetalAPI.ListServers(&baremetalSDK.ListServersRequest{Zone: zone}, scw.WithAllPages()) - if err != nil { - logging.L.Warningf("error listing servers in (%s) in sweeper: %s", zone, err) - return nil - } - - for _, server := range listServers.Servers { - _, err := baremetalAPI.DeleteServer(&baremetalSDK.DeleteServerRequest{ - Zone: zone, - ServerID: server.ID, - }) - if err != nil { - return fmt.Errorf("error deleting server in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&baremetalSDK.API{}).Zones(), sweepers.SweepServers) } diff --git a/internal/services/block/testfuncs/sweep.go b/internal/services/block/testfuncs/sweep.go index a12a27b63..cb163e6ef 100644 --- a/internal/services/block/testfuncs/sweep.go +++ b/internal/services/block/testfuncs/sweep.go @@ -1,13 +1,10 @@ package blocktestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" blockSDK "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -22,57 +19,9 @@ func AddTestSweepers() { } func testSweepBlockVolume(_ string) error { - return acctest.SweepZones((&blockSDK.API{}).Zones(), func(scwClient *scw.Client, zone scw.Zone) error { - blockAPI := blockSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the block volumes in (%s)", zone) - listVolumes, err := blockAPI.ListVolumes( - &blockSDK.ListVolumesRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing volume in (%s) in sweeper: %s", zone, err) - } - - for _, volume := range listVolumes.Volumes { - err := blockAPI.DeleteVolume(&blockSDK.DeleteVolumeRequest{ - VolumeID: volume.ID, - Zone: zone, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting volume in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&blockSDK.API{}).Zones(), sweepers.SweepVolumes) } func testSweepSnapshot(_ string) error { - return acctest.SweepZones((&blockSDK.API{}).Zones(), func(scwClient *scw.Client, zone scw.Zone) error { - blockAPI := blockSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the block snapshots in (%s)", zone) - listSnapshots, err := blockAPI.ListSnapshots( - &blockSDK.ListSnapshotsRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing snapshot in (%s) in sweeper: %s", zone, err) - } - - for _, snapshot := range listSnapshots.Snapshots { - err := blockAPI.DeleteSnapshot(&blockSDK.DeleteSnapshotRequest{ - SnapshotID: snapshot.ID, - Zone: zone, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting snapshot in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&blockSDK.API{}).Zones(), sweepers.SweepSnapshots) } diff --git a/internal/services/cockpit/testfuncs/sweep.go b/internal/services/cockpit/testfuncs/sweep.go index 09534ac03..5691bdc70 100644 --- a/internal/services/cockpit/testfuncs/sweep.go +++ b/internal/services/cockpit/testfuncs/sweep.go @@ -1,22 +1,13 @@ package cockpittestfuncs import ( - "fmt" - "strings" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - accountSDK "github.com/scaleway/scaleway-sdk-go/api/account/v3" - "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1" + "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1/sweepers" "github.com/scaleway/scaleway-sdk-go/scw" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors" ) func AddTestSweepers() { - resource.AddTestSweepers("scaleway_cockpit", &resource.Sweeper{ - Name: "scaleway_cockpit", - F: testSweepCockpit, - }) resource.AddTestSweepers("scaleway_cockpit_grafana_user", &resource.Sweeper{ Name: "scaleway_cockpit_grafana_user", F: testSweepCockpitGrafanaUser, @@ -32,161 +23,13 @@ func AddTestSweepers() { } func testSweepCockpitToken(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - accountAPI := accountSDK.NewProjectAPI(scwClient) - cockpitAPI := cockpit.NewRegionalAPI(scwClient) - - listProjects, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("failed to list projects: %w", err) - } - - for _, project := range listProjects.Projects { - if !strings.HasPrefix(project.Name, "tf_tests") { - continue - } - - listTokens, err := cockpitAPI.ListTokens(&cockpit.RegionalAPIListTokensRequest{ - ProjectID: project.ID, - }, scw.WithAllPages()) - if err != nil { - if httperrors.Is404(err) { - return nil - } - - return fmt.Errorf("failed to list tokens: %w", err) - } - - for _, token := range listTokens.Tokens { - err = cockpitAPI.DeleteToken(&cockpit.RegionalAPIDeleteTokenRequest{ - TokenID: token.ID, - }) - if err != nil { - if !httperrors.Is404(err) { - return fmt.Errorf("failed to delete token: %w", err) - } - } - } - } - - return nil - }) + return acctest.Sweep(sweepers.SweepToken) } func testSweepCockpitGrafanaUser(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - accountAPI := accountSDK.NewProjectAPI(scwClient) - cockpitAPI := cockpit.NewGlobalAPI(scwClient) - - listProjects, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("failed to list projects: %w", err) - } - - for _, project := range listProjects.Projects { - if !strings.HasPrefix(project.Name, "tf_tests") { - continue - } - - listGrafanaUsers, err := cockpitAPI.ListGrafanaUsers(&cockpit.GlobalAPIListGrafanaUsersRequest{ - ProjectID: project.ID, - }, scw.WithAllPages()) - if err != nil { - if httperrors.Is404(err) { - return nil - } - - return fmt.Errorf("failed to list grafana users: %w", err) - } - - for _, grafanaUser := range listGrafanaUsers.GrafanaUsers { - err = cockpitAPI.DeleteGrafanaUser(&cockpit.GlobalAPIDeleteGrafanaUserRequest{ - ProjectID: project.ID, - GrafanaUserID: grafanaUser.ID, - }) - if err != nil { - if !httperrors.Is404(err) { - return fmt.Errorf("failed to delete grafana user: %w", err) - } - } - } - } - - return nil - }) -} - -func testSweepCockpit(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - accountAPI := accountSDK.NewProjectAPI(scwClient) - - listProjects, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("failed to list projects: %w", err) - } - - for _, project := range listProjects.Projects { - if !strings.HasPrefix(project.Name, "tf_tests") { - continue - } - - if err != nil { - if !httperrors.Is404(err) { - return fmt.Errorf("failed to deactivate cockpit: %w", err) - } - } - - if err != nil { - if !httperrors.Is404(err) { - return fmt.Errorf("failed to deactivate cockpit: %w", err) - } - } - } - - return nil - }) + return acctest.Sweep(sweepers.SweepGrafanaUser) } func testSweepCockpitSource(_ string) error { - return acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - accountAPI := accountSDK.NewProjectAPI(scwClient) - cockpitAPI := cockpit.NewRegionalAPI(scwClient) - - listProjects, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("failed to list projects: %w", err) - } - - for _, project := range listProjects.Projects { - if !strings.HasPrefix(project.Name, "tf_tests") { - continue - } - - listDatasources, err := cockpitAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{ - ProjectID: project.ID, - Region: region, - }, scw.WithAllPages()) - if err != nil { - if httperrors.Is404(err) { - return nil - } - - return fmt.Errorf("failed to list sources: %w", err) - } - - for _, datsource := range listDatasources.DataSources { - err = cockpitAPI.DeleteDataSource(&cockpit.RegionalAPIDeleteDataSourceRequest{ - DataSourceID: datsource.ID, - Region: region, - }) - if err != nil { - if !httperrors.Is404(err) { - return fmt.Errorf("failed to delete cockpit source: %w", err) - } - } - } - } - - return nil - }) + return acctest.SweepRegions(scw.AllRegions, sweepers.SweepSource) } diff --git a/internal/services/container/testfuncs/sweep.go b/internal/services/container/testfuncs/sweep.go index 2a241ed06..7b703b9d1 100644 --- a/internal/services/container/testfuncs/sweep.go +++ b/internal/services/container/testfuncs/sweep.go @@ -1,13 +1,10 @@ package containertestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" containerSDK "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -27,85 +24,13 @@ func AddTestSweepers() { } func testSweepTrigger(_ string) error { - return acctest.SweepRegions((&containerSDK.API{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - containerAPI := containerSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the container triggers in (%s)", region) - listTriggers, err := containerAPI.ListTriggers( - &containerSDK.ListTriggersRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing trigger in (%s) in sweeper: %s", region, err) - } - - for _, trigger := range listTriggers.Triggers { - _, err := containerAPI.DeleteTrigger(&containerSDK.DeleteTriggerRequest{ - TriggerID: trigger.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting trigger in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&containerSDK.API{}).Regions(), sweepers.SweepTrigger) } func testSweepContainer(_ string) error { - return acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - containerAPI := containerSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the container in (%s)", region) - listNamespaces, err := containerAPI.ListContainers( - &containerSDK.ListContainersRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing containers in (%s) in sweeper: %s", region, err) - } - - for _, cont := range listNamespaces.Containers { - _, err := containerAPI.DeleteContainer(&containerSDK.DeleteContainerRequest{ - ContainerID: cont.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting container in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&containerSDK.API{}).Regions(), sweepers.SweepContainer) } func testSweepNamespace(_ string) error { - return acctest.SweepRegions([]scw.Region{scw.RegionFrPar}, func(scwClient *scw.Client, region scw.Region) error { - containerAPI := containerSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the container namespaces in (%s)", region) - listNamespaces, err := containerAPI.ListNamespaces( - &containerSDK.ListNamespacesRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing namespaces in (%s) in sweeper: %s", region, err) - } - - for _, ns := range listNamespaces.Namespaces { - _, err := containerAPI.DeleteNamespace(&containerSDK.DeleteNamespaceRequest{ - NamespaceID: ns.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting namespace in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&containerSDK.API{}).Regions(), sweepers.SweepNamespace) } diff --git a/internal/services/flexibleip/testfuncs/sweep.go b/internal/services/flexibleip/testfuncs/sweep.go index 7921836dc..f59cda1ae 100644 --- a/internal/services/flexibleip/testfuncs/sweep.go +++ b/internal/services/flexibleip/testfuncs/sweep.go @@ -1,13 +1,10 @@ package flexibleiptestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" flexibleipSDK "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,25 +15,5 @@ func AddTestSweepers() { } func testSweepFlexibleIP(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - fipAPI := flexibleipSDK.NewAPI(scwClient) - - listIPs, err := fipAPI.ListFlexibleIPs(&flexibleipSDK.ListFlexibleIPsRequest{Zone: zone}, scw.WithAllPages()) - if err != nil { - logging.L.Warningf("error listing ips in (%s) in sweeper: %s", zone, err) - return nil - } - - for _, ip := range listIPs.FlexibleIPs { - err := fipAPI.DeleteFlexibleIP(&flexibleipSDK.DeleteFlexibleIPRequest{ - FipID: ip.ID, - Zone: zone, - }) - if err != nil { - return fmt.Errorf("error deleting ip in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&flexibleipSDK.API{}).Zones(), sweepers.SweepFlexibleIP) } diff --git a/internal/services/function/testfuncs/sweep.go b/internal/services/function/testfuncs/sweep.go index a9c4c8f41..66c24294c 100644 --- a/internal/services/function/testfuncs/sweep.go +++ b/internal/services/function/testfuncs/sweep.go @@ -1,14 +1,10 @@ package functiontestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" functionSDK "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -31,113 +27,17 @@ func AddTestSweepers() { } func testSweepFunctionTrigger(_ string) error { - return acctest.SweepRegions((&functionSDK.API{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - functionAPI := functionSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the function triggers in (%s)", region) - listTriggers, err := functionAPI.ListTriggers( - &functionSDK.ListTriggersRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing trigger in (%s) in sweeper: %s", region, err) - } - - for _, trigger := range listTriggers.Triggers { - _, err := functionAPI.DeleteTrigger(&functionSDK.DeleteTriggerRequest{ - TriggerID: trigger.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting trigger in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&functionSDK.API{}).Regions(), sweepers.SweepTriggers) } func testSweepFunctionNamespace(_ string) error { - return acctest.SweepRegions([]scw.Region{scw.RegionFrPar}, func(scwClient *scw.Client, region scw.Region) error { - functionAPI := functionSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the function namespaces in (%s)", region) - listNamespaces, err := functionAPI.ListNamespaces( - &functionSDK.ListNamespacesRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing namespaces in (%s) in sweeper: %s", region, err) - } - - for _, ns := range listNamespaces.Namespaces { - _, err := functionAPI.DeleteNamespace(&functionSDK.DeleteNamespaceRequest{ - NamespaceID: ns.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting namespace in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&functionSDK.API{}).Regions(), sweepers.SweepNamespaces) } func testSweepFunction(_ string) error { - return acctest.SweepRegions([]scw.Region{scw.RegionFrPar}, func(scwClient *scw.Client, region scw.Region) error { - functionAPI := functionSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the function in (%s)", region) - listFunctions, err := functionAPI.ListFunctions( - &functionSDK.ListFunctionsRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing functions in (%s) in sweeper: %s", region, err) - } - - for _, f := range listFunctions.Functions { - _, err := functionAPI.DeleteFunction(&functionSDK.DeleteFunctionRequest{ - FunctionID: f.ID, - Region: region, - }) - if err != nil && !httperrors.Is404(err) { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting functions in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&functionSDK.API{}).Regions(), sweepers.SweepFunctions) } func testSweepFunctionCron(_ string) error { - return acctest.SweepRegions([]scw.Region{scw.RegionFrPar}, func(scwClient *scw.Client, region scw.Region) error { - functionAPI := functionSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the function cron in (%s)", region) - listCron, err := functionAPI.ListCrons( - &functionSDK.ListCronsRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing cron in (%s) in sweeper: %s", region, err) - } - - for _, cron := range listCron.Crons { - _, err := functionAPI.DeleteCron(&functionSDK.DeleteCronRequest{ - CronID: cron.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting cron in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&functionSDK.API{}).Regions(), sweepers.SweepCrons) } diff --git a/internal/services/iam/testfuncs/sweep.go b/internal/services/iam/testfuncs/sweep.go index 222deb2e0..d532a118d 100644 --- a/internal/services/iam/testfuncs/sweep.go +++ b/internal/services/iam/testfuncs/sweep.go @@ -1,14 +1,9 @@ package iamtestfuncs import ( - "errors" - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - iamSDK "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -39,181 +34,25 @@ func AddTestSweepers() { } func testSweepUser(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - api := iamSDK.NewAPI(scwClient) - - orgID, exists := scwClient.GetDefaultOrganizationID() - if !exists { - return errors.New("missing organizationID") - } - - listUsers, err := api.ListUsers(&iamSDK.ListUsersRequest{ - OrganizationID: &orgID, - }) - if err != nil { - return fmt.Errorf("failed to list users: %w", err) - } - for _, user := range listUsers.Users { - if !acctest.IsTestResource(user.Email) { - continue - } - err = api.DeleteUser(&iamSDK.DeleteUserRequest{ - UserID: user.ID, - }) - if err != nil { - return fmt.Errorf("failed to delete user: %w", err) - } - } - return nil - }) + return acctest.Sweep(sweepers.SweepUser) } func testSweepSSHKey(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - iamAPI := iamSDK.NewAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the SSH keys") - - listSSHKeys, err := iamAPI.ListSSHKeys(&iamSDK.ListSSHKeysRequest{}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing SSH keys in sweeper: %s", err) - } - - for _, sshKey := range listSSHKeys.SSHKeys { - if !acctest.IsTestResource(sshKey.Name) { - continue - } - err := iamAPI.DeleteSSHKey(&iamSDK.DeleteSSHKeyRequest{ - SSHKeyID: sshKey.ID, - }) - if err != nil { - return fmt.Errorf("error deleting SSH key in sweeper: %s", err) - } - } - - return nil - }) + return acctest.Sweep(sweepers.SweepSSHKey) } func testSweepIamPolicy(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - api := iamSDK.NewAPI(scwClient) - - orgID, exists := scwClient.GetDefaultOrganizationID() - if !exists { - return errors.New("missing organizationID") - } - - listPols, err := api.ListPolicies(&iamSDK.ListPoliciesRequest{ - OrganizationID: orgID, - }) - if err != nil { - return fmt.Errorf("failed to list policies: %w", err) - } - for _, pol := range listPols.Policies { - if !acctest.IsTestResource(pol.Name) { - continue - } - err = api.DeletePolicy(&iamSDK.DeletePolicyRequest{ - PolicyID: pol.ID, - }) - if err != nil { - return fmt.Errorf("failed to delete policy: %w", err) - } - } - return nil - }) + return acctest.Sweep(sweepers.SweepPolicy) } func testSweepIamGroup(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - api := iamSDK.NewAPI(scwClient) - - orgID, exists := scwClient.GetDefaultOrganizationID() - if !exists { - return errors.New("missing organizationID") - } - - listApps, err := api.ListGroups(&iamSDK.ListGroupsRequest{ - OrganizationID: orgID, - }) - if err != nil { - return fmt.Errorf("failed to list groups: %w", err) - } - for _, group := range listApps.Groups { - if !acctest.IsTestResource(group.Name) { - continue - } - err = api.DeleteGroup(&iamSDK.DeleteGroupRequest{ - GroupID: group.ID, - }) - if err != nil { - return fmt.Errorf("failed to delete group: %w", err) - } - } - return nil - }) + return acctest.Sweep(sweepers.SweepGroup) } func testSweepIamApplication(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - api := iamSDK.NewAPI(scwClient) - - orgID, exists := scwClient.GetDefaultOrganizationID() - if !exists { - return errors.New("missing organizationID") - } - - listApps, err := api.ListApplications(&iamSDK.ListApplicationsRequest{ - OrganizationID: orgID, - }) - if err != nil { - return fmt.Errorf("failed to list applications: %w", err) - } - for _, app := range listApps.Applications { - if !acctest.IsTestResource(app.Name) { - continue - } - - err = api.DeleteApplication(&iamSDK.DeleteApplicationRequest{ - ApplicationID: app.ID, - }) - if err != nil { - return fmt.Errorf("failed to delete application: %w", err) - } - } - return nil - }) + return acctest.Sweep(sweepers.SweepApplication) } func testSweepIamAPIKey(_ string) error { - return acctest.Sweep(func(scwClient *scw.Client) error { - api := iamSDK.NewAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the api keys") - - orgID, exists := scwClient.GetDefaultOrganizationID() - if !exists { - return errors.New("missing organizationID") - } - - listAPIKeys, err := api.ListAPIKeys(&iamSDK.ListAPIKeysRequest{ - OrganizationID: &orgID, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("failed to list api keys: %w", err) - } - for _, key := range listAPIKeys.APIKeys { - if !acctest.IsTestResource(key.Description) { - continue - } - err = api.DeleteAPIKey(&iamSDK.DeleteAPIKeyRequest{ - AccessKey: key.AccessKey, - }) - if err != nil { - return fmt.Errorf("failed to delete api key: %w", err) - } - } - return nil - }) + return acctest.Sweep(sweepers.SweepAPIKey) } diff --git a/internal/services/inference/testfuncs/sweep.go b/internal/services/inference/testfuncs/sweep.go index 9a0db8c00..025e8784b 100644 --- a/internal/services/inference/testfuncs/sweep.go +++ b/internal/services/inference/testfuncs/sweep.go @@ -1,13 +1,10 @@ package inferencetestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" inference "github.com/scaleway/scaleway-sdk-go/api/inference/v1beta1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/inference/v1beta1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -19,29 +16,5 @@ func AddTestSweepers() { } func testSweepDeployment(_ string) error { - return acctest.SweepRegions((&inference.API{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - inferenceAPI := inference.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the inference deployments in (%s)", region) - listDeployments, err := inferenceAPI.ListDeployments( - &inference.ListDeploymentsRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing deployment in (%s) in sweeper: %s", region, err) - } - - for _, deployment := range listDeployments.Deployments { - _, err := inferenceAPI.DeleteDeployment(&inference.DeleteDeploymentRequest{ - DeploymentID: deployment.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting deployment in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&inference.API{}).Regions(), sweepers.SweepDeployment) } diff --git a/internal/services/instance/testfuncs/sweep.go b/internal/services/instance/testfuncs/sweep.go index edd621b1b..b3f0e4451 100644 --- a/internal/services/instance/testfuncs/sweep.go +++ b/internal/services/instance/testfuncs/sweep.go @@ -1,13 +1,10 @@ package instancetestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/instance/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -43,197 +40,29 @@ func AddTestSweepers() { } func testSweepVolume(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - instanceAPI := instanceSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the volumes in (%s)", zone) - - listVolumesResponse, err := instanceAPI.ListVolumes(&instanceSDK.ListVolumesRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing volumes in sweeper: %s", err) - } - - for _, volume := range listVolumesResponse.Volumes { - if volume.Server == nil { - err := instanceAPI.DeleteVolume(&instanceSDK.DeleteVolumeRequest{ - Zone: zone, - VolumeID: volume.ID, - }) - if err != nil { - return fmt.Errorf("error deleting volume in sweeper: %s", err) - } - } - } - return nil - }) + return acctest.SweepZones((&instanceSDK.API{}).Zones(), sweepers.SweepVolumes) } func testSweepSnapshot(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - api := instanceSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying instance snapshots in (%+v)", zone) - - listSnapshotsResponse, err := api.ListSnapshots(&instanceSDK.ListSnapshotsRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing instance snapshots in sweeper: %w", err) - } - - for _, snapshot := range listSnapshotsResponse.Snapshots { - err := api.DeleteSnapshot(&instanceSDK.DeleteSnapshotRequest{ - Zone: zone, - SnapshotID: snapshot.ID, - }) - if err != nil { - return fmt.Errorf("error deleting instance snapshot in sweeper: %w", err) - } - } - - return nil - }) + return acctest.SweepZones((&instanceSDK.API{}).Zones(), sweepers.SweepSnapshots) } func testSweepServer(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - instanceAPI := instanceSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the instanceSDK server in (%s)", zone) - listServers, err := instanceAPI.ListServers(&instanceSDK.ListServersRequest{Zone: zone}, scw.WithAllPages()) - if err != nil { - logging.L.Warningf("error listing servers in (%s) in sweeper: %s", zone, err) - return nil - } - - for _, srv := range listServers.Servers { - if srv.State == instanceSDK.ServerStateStopped || srv.State == instanceSDK.ServerStateStoppedInPlace { - err := instanceAPI.DeleteServer(&instanceSDK.DeleteServerRequest{ - Zone: zone, - ServerID: srv.ID, - }) - if err != nil { - return fmt.Errorf("error deleting server in sweeper: %s", err) - } - } else if srv.State == instanceSDK.ServerStateRunning { - _, err := instanceAPI.ServerAction(&instanceSDK.ServerActionRequest{ - Zone: zone, - ServerID: srv.ID, - Action: instanceSDK.ServerActionTerminate, - }) - if err != nil { - return fmt.Errorf("error terminating server in sweeper: %s", err) - } - } - } - - return nil - }) + return acctest.SweepZones((&instanceSDK.API{}).Zones(), sweepers.SweepServers) } func testSweepSecurityGroup(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - instanceAPI := instanceSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the security groups in (%s)", zone) - - listResp, err := instanceAPI.ListSecurityGroups(&instanceSDK.ListSecurityGroupsRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - logging.L.Warningf("error listing security groups in sweeper: %s", err) - return nil - } - - for _, securityGroup := range listResp.SecurityGroups { - // Can't delete default security group. - if securityGroup.ProjectDefault { - continue - } - err = instanceAPI.DeleteSecurityGroup(&instanceSDK.DeleteSecurityGroupRequest{ - Zone: zone, - SecurityGroupID: securityGroup.ID, - }) - if err != nil { - return fmt.Errorf("error deleting security groups in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&instanceSDK.API{}).Zones(), sweepers.SweepSecurityGroups) } func testSweepPlacementGroup(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - instanceAPI := instanceSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the instance placement group in (%s)", zone) - listPlacementGroups, err := instanceAPI.ListPlacementGroups(&instanceSDK.ListPlacementGroupsRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - logging.L.Warningf("error listing placement groups in (%s) in sweeper: %s", zone, err) - return nil - } - - for _, pg := range listPlacementGroups.PlacementGroups { - err := instanceAPI.DeletePlacementGroup(&instanceSDK.DeletePlacementGroupRequest{ - Zone: zone, - PlacementGroupID: pg.ID, - }) - if err != nil { - return fmt.Errorf("error deleting placement group in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&instanceSDK.API{}).Zones(), sweepers.SweepPlacementGroup) } func testSweepIP(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - instanceAPI := instanceSDK.NewAPI(scwClient) - - listIPs, err := instanceAPI.ListIPs(&instanceSDK.ListIPsRequest{Zone: zone}, scw.WithAllPages()) - if err != nil { - logging.L.Warningf("error listing ips in (%s) in sweeper: %s", zone, err) - return nil - } - - for _, ip := range listIPs.IPs { - err := instanceAPI.DeleteIP(&instanceSDK.DeleteIPRequest{ - IP: ip.ID, - Zone: zone, - }) - if err != nil { - return fmt.Errorf("error deleting ip in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&instanceSDK.API{}).Zones(), sweepers.SweepIP) } func testSweepImage(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - api := instanceSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying instance images in (%+v)", zone) - - listImagesResponse, err := api.ListImages(&instanceSDK.ListImagesRequest{ - Zone: zone, - Public: scw.BoolPtr(false), - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing instance images in sweeper: %w", err) - } - - for _, image := range listImagesResponse.Images { - err := api.DeleteImage(&instanceSDK.DeleteImageRequest{ - Zone: zone, - ImageID: image.ID, - }) - if err != nil { - return fmt.Errorf("error deleting instance image in sweeper: %w", err) - } - } - - return nil - }) + return acctest.SweepZones((&instanceSDK.API{}).Zones(), sweepers.SweepImages) } diff --git a/internal/services/iot/testfuncs/sweep.go b/internal/services/iot/testfuncs/sweep.go index c9abce7fd..81abade37 100644 --- a/internal/services/iot/testfuncs/sweep.go +++ b/internal/services/iot/testfuncs/sweep.go @@ -1,13 +1,10 @@ package iottestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" iotSDK "github.com/scaleway/scaleway-sdk-go/api/iot/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/iot/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,27 +15,5 @@ func AddTestSweepers() { } func testSweepHub(_ string) error { - return acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - iotAPI := iotSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the iot hub in (%s)", region) - listHubs, err := iotAPI.ListHubs(&iotSDK.ListHubsRequest{Region: region}, scw.WithAllPages()) - if err != nil { - logging.L.Debugf("sweeper: destroying the iot hub in (%s)", region) - return fmt.Errorf("error listing hubs in (%s) in sweeper: %s", region, err) - } - - deleteDevices := true - for _, hub := range listHubs.Hubs { - err := iotAPI.DeleteHub(&iotSDK.DeleteHubRequest{ - HubID: hub.ID, - Region: hub.Region, - DeleteDevices: &deleteDevices, - }) - if err != nil { - return fmt.Errorf("error deleting hub in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&iotSDK.API{}).Regions(), sweepers.SweepHub) } diff --git a/internal/services/ipam/testfuncs/sweep.go b/internal/services/ipam/testfuncs/sweep.go index 77a747476..435c0062d 100644 --- a/internal/services/ipam/testfuncs/sweep.go +++ b/internal/services/ipam/testfuncs/sweep.go @@ -1,13 +1,10 @@ package ipamtestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ipamSDK "github.com/scaleway/scaleway-sdk-go/api/ipam/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/ipam/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,28 +15,5 @@ func AddTestSweepers() { } func testSweepIPAMIP(_ string) error { - return acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - ipamAPI := ipamSDK.NewAPI(scwClient) - - logging.L.Debugf("sweeper: deleting the IPs in (%s)", region) - - listIPs, err := ipamAPI.ListIPs(&ipamSDK.ListIPsRequest{Region: region}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing ips in (%s) in sweeper: %s", region, err) - } - - for _, v := range listIPs.IPs { - err := ipamAPI.ReleaseIP(&ipamSDK.ReleaseIPRequest{ - IPID: v.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error releasing IP in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&ipamSDK.API{}).Regions(), sweepers.SweepIP) } diff --git a/internal/services/jobs/testfuncs/sweep.go b/internal/services/jobs/testfuncs/sweep.go index 78c3d5b67..e61bd9e56 100644 --- a/internal/services/jobs/testfuncs/sweep.go +++ b/internal/services/jobs/testfuncs/sweep.go @@ -1,13 +1,10 @@ package jobstestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" jobsSDK "github.com/scaleway/scaleway-sdk-go/api/jobs/v1alpha1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/jobs/v1alpha1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,29 +15,5 @@ func AddTestSweepers() { } func testSweepJobDefinition(_ string) error { - return acctest.SweepRegions((&jobsSDK.API{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - jobsAPI := jobsSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the jobs definitions in (%s)", region) - listJobDefinitions, err := jobsAPI.ListJobDefinitions( - &jobsSDK.ListJobDefinitionsRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing definition in (%s) in sweeper: %s", region, err) - } - - for _, definition := range listJobDefinitions.JobDefinitions { - err := jobsAPI.DeleteJobDefinition(&jobsSDK.DeleteJobDefinitionRequest{ - JobDefinitionID: definition.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting definition in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&jobsSDK.API{}).Regions(), sweepers.SweepJobDefinition) } diff --git a/internal/services/k8s/testfuncs/sweep.go b/internal/services/k8s/testfuncs/sweep.go index 4438996aa..98f9b02fa 100644 --- a/internal/services/k8s/testfuncs/sweep.go +++ b/internal/services/k8s/testfuncs/sweep.go @@ -1,13 +1,10 @@ package k8stestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" k8sSDK "github.com/scaleway/scaleway-sdk-go/api/k8s/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/k8s/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,43 +15,5 @@ func AddTestSweepers() { } func testSweepK8SCluster(_ string) error { - return acctest.SweepRegions([]scw.Region{scw.RegionFrPar, scw.RegionNlAms}, func(scwClient *scw.Client, region scw.Region) error { - k8sAPI := k8sSDK.NewAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the k8s cluster in (%s)", region) - listClusters, err := k8sAPI.ListClusters(&k8sSDK.ListClustersRequest{Region: region}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing clusters in (%s) in sweeper: %s", region, err) - } - - for _, cluster := range listClusters.Clusters { - // remove pools - listPools, err := k8sAPI.ListPools(&k8sSDK.ListPoolsRequest{ - Region: region, - ClusterID: cluster.ID, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing pool in (%s) in sweeper: %s", region, err) - } - - for _, pool := range listPools.Pools { - _, err := k8sAPI.DeletePool(&k8sSDK.DeletePoolRequest{ - Region: region, - PoolID: pool.ID, - }) - if err != nil { - return fmt.Errorf("error deleting pool in sweeper: %s", err) - } - } - _, err = k8sAPI.DeleteCluster(&k8sSDK.DeleteClusterRequest{ - Region: region, - ClusterID: cluster.ID, - }) - if err != nil { - return fmt.Errorf("error deleting cluster in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&k8sSDK.API{}).Regions(), sweepers.SweepCluster) } diff --git a/internal/services/lb/testfuncs/sweep.go b/internal/services/lb/testfuncs/sweep.go index 9e2303a07..c4ce75d30 100644 --- a/internal/services/lb/testfuncs/sweep.go +++ b/internal/services/lb/testfuncs/sweep.go @@ -1,16 +1,10 @@ package lbtestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" lbSDK "github.com/scaleway/scaleway-sdk-go/api/lb/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/lb/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/lb" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func AddTestSweepers() { @@ -25,69 +19,9 @@ func AddTestSweepers() { } func testSweepLB(_ string) error { - return acctest.SweepZones([]scw.Zone{scw.ZoneFrPar1, scw.ZoneNlAms1, scw.ZonePlWaw1}, func(scwClient *scw.Client, zone scw.Zone) error { - lbAPI := lbSDK.NewZonedAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the lbs in (%s)", zone) - listLBs, err := lbAPI.ListLBs(&lbSDK.ZonedAPIListLBsRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing lbs in (%s) in sweeper: %s", zone, err) - } - - for _, l := range listLBs.LBs { - retryInterval := lb.DefaultWaitLBRetryInterval - - if transport.DefaultWaitRetryInterval != nil { - retryInterval = *transport.DefaultWaitRetryInterval - } - - _, err := lbAPI.WaitForLbInstances(&lbSDK.ZonedAPIWaitForLBInstancesRequest{ - Zone: zone, - LBID: l.ID, - Timeout: scw.TimeDurationPtr(instance.DefaultInstanceServerWaitTimeout), - RetryInterval: &retryInterval, - }) - if err != nil { - return fmt.Errorf("error waiting for lb in sweeper: %s", err) - } - err = lbAPI.DeleteLB(&lbSDK.ZonedAPIDeleteLBRequest{ - LBID: l.ID, - ReleaseIP: true, - Zone: zone, - }) - if err != nil { - return fmt.Errorf("error deleting lb in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepZones((&lbSDK.ZonedAPI{}).Zones(), sweepers.SweepLB) } func testSweepIP(_ string) error { - return acctest.SweepZones([]scw.Zone{scw.ZoneFrPar1, scw.ZoneNlAms1, scw.ZonePlWaw1}, func(scwClient *scw.Client, zone scw.Zone) error { - lbAPI := lbSDK.NewZonedAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the lb ips in zone (%s)", zone) - listIPs, err := lbAPI.ListIPs(&lbSDK.ZonedAPIListIPsRequest{Zone: zone}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing lb ips in (%s) in sweeper: %s", zone, err) - } - - for _, ip := range listIPs.IPs { - if ip.LBID == nil { - err := lbAPI.ReleaseIP(&lbSDK.ZonedAPIReleaseIPRequest{ - Zone: zone, - IPID: ip.ID, - }) - if err != nil { - return fmt.Errorf("error deleting lb ip in sweeper: %s", err) - } - } - } - - return nil - }) + return acctest.SweepZones((&lbSDK.ZonedAPI{}).Zones(), sweepers.SweepIP) } diff --git a/internal/services/mnq/testfuncs/sweep.go b/internal/services/mnq/testfuncs/sweep.go index 7d7fa089f..43fb3fb7e 100644 --- a/internal/services/mnq/testfuncs/sweep.go +++ b/internal/services/mnq/testfuncs/sweep.go @@ -1,15 +1,10 @@ package mnqtestfuncs import ( - "fmt" - "strings" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - accountSDK "github.com/scaleway/scaleway-sdk-go/api/account/v3" mnqSDK "github.com/scaleway/scaleway-sdk-go/api/mnq/v1beta1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/mnq/v1beta1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -36,145 +31,21 @@ func AddTestSweepers() { } func testSweepSQSCredentials(_ string) error { - return acctest.SweepRegions((&mnqSDK.SqsAPI{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - mnqAPI := mnqSDK.NewSqsAPI(scwClient) - logging.L.Debugf("sweeper: destroying the mnq sqs credentials in (%s)", region) - listSqsCredentials, err := mnqAPI.ListSqsCredentials( - &mnqSDK.SqsAPIListSqsCredentialsRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing sqs credentials in (%s) in sweeper: %s", region, err) - } - - for _, credentials := range listSqsCredentials.SqsCredentials { - err := mnqAPI.DeleteSqsCredentials(&mnqSDK.SqsAPIDeleteSqsCredentialsRequest{ - SqsCredentialsID: credentials.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting sqs credentials in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&mnqSDK.SqsAPI{}).Regions(), sweepers.SweepSQSCredentials) } func testSweepSQS(_ string) error { - return acctest.SweepRegions((&mnqSDK.SqsAPI{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - accountAPI := accountSDK.NewProjectAPI(scwClient) - mnqAPI := mnqSDK.NewSqsAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the mnq sqss in (%s)", region) - - listProjects, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("failed to list projects: %w", err) - } - for _, project := range listProjects.Projects { - if !strings.HasPrefix(project.Name, "tf_tests") { - continue - } - - _, err := mnqAPI.DeactivateSqs(&mnqSDK.SqsAPIDeactivateSqsRequest{ - Region: region, - ProjectID: project.ID, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - return err - } - } - - return nil - }) + return acctest.SweepRegions((&mnqSDK.SqsAPI{}).Regions(), sweepers.SweepSQS) } func testSweepSNSCredentials(_ string) error { - return acctest.SweepRegions((&mnqSDK.SnsAPI{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - mnqAPI := mnqSDK.NewSnsAPI(scwClient) - logging.L.Debugf("sweeper: destroying the mnq sns credentials in (%s)", region) - listSnsCredentials, err := mnqAPI.ListSnsCredentials( - &mnqSDK.SnsAPIListSnsCredentialsRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing sns credentials in (%s) in sweeper: %s", region, err) - } - - for _, credentials := range listSnsCredentials.SnsCredentials { - err := mnqAPI.DeleteSnsCredentials(&mnqSDK.SnsAPIDeleteSnsCredentialsRequest{ - SnsCredentialsID: credentials.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting sns credentials in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&mnqSDK.SnsAPI{}).Regions(), sweepers.SweepSNSCredentials) } func testSweepSNS(_ string) error { - return acctest.SweepRegions((&mnqSDK.SnsAPI{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - accountAPI := accountSDK.NewProjectAPI(scwClient) - mnqAPI := mnqSDK.NewSnsAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the mnq sns in (%s)", region) - - listProjects, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("failed to list projects: %w", err) - } - for _, project := range listProjects.Projects { - if !strings.HasPrefix(project.Name, "tf_tests") { - continue - } - - _, err := mnqAPI.DeactivateSns(&mnqSDK.SnsAPIDeactivateSnsRequest{ - Region: region, - ProjectID: project.ID, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - return err - } - } - - return nil - }) + return acctest.SweepRegions((&mnqSDK.SnsAPI{}).Regions(), sweepers.SweepSNS) } func testSweepNatsAccount(_ string) error { - return acctest.SweepRegions((&mnqSDK.NatsAPI{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - mnqAPI := mnqSDK.NewNatsAPI(scwClient) - logging.L.Debugf("sweeper: destroying the mnq nats accounts in (%s)", region) - listNatsAccounts, err := mnqAPI.ListNatsAccounts( - &mnqSDK.NatsAPIListNatsAccountsRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing nats account in (%s) in sweeper: %s", region, err) - } - - for _, account := range listNatsAccounts.NatsAccounts { - err := mnqAPI.DeleteNatsAccount(&mnqSDK.NatsAPIDeleteNatsAccountRequest{ - NatsAccountID: account.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting nats account in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&mnqSDK.NatsAPI{}).Regions(), sweepers.SweepNatsAccount) } diff --git a/internal/services/mongodb/testfuncs/sweep.go b/internal/services/mongodb/testfuncs/sweep.go index 8d99e2df0..b0c942824 100644 --- a/internal/services/mongodb/testfuncs/sweep.go +++ b/internal/services/mongodb/testfuncs/sweep.go @@ -1,13 +1,10 @@ package mongodbtestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,30 +15,5 @@ func AddTestSweepers() { } func testSweepMongodbInstance(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - mongodbAPI := mongodb.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the mongodb instance in (%s)", zone) - extractRegion, err := zone.Region() - if err != nil { - return fmt.Errorf("error extract region in (%s) in sweeper: %w", zone, err) - } - listInstance, err := mongodbAPI.ListInstances(&mongodb.ListInstancesRequest{ - Region: extractRegion, - }) - if err != nil { - return fmt.Errorf("error listing mongodb instance in (%s) in sweeper: %w", zone, err) - } - - for _, instance := range listInstance.Instances { - _, err := mongodbAPI.DeleteInstance(&mongodb.DeleteInstanceRequest{ - Region: extractRegion, - InstanceID: instance.ID, - }) - if err != nil { - return fmt.Errorf("error deleting mongodb instance in sweeper: %w", err) - } - } - - return nil - }) + return acctest.SweepRegions((&mongodb.API{}).Regions(), sweepers.SweepInstances) } diff --git a/internal/services/rdb/testfuncs/sweep.go b/internal/services/rdb/testfuncs/sweep.go index b567c4ce7..5f5629f07 100644 --- a/internal/services/rdb/testfuncs/sweep.go +++ b/internal/services/rdb/testfuncs/sweep.go @@ -1,13 +1,10 @@ package rdbtestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" rdbSDK "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/rdb/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,26 +15,5 @@ func AddTestSweepers() { } func testSweepInstance(_ string) error { - return acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - rdbAPI := rdbSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the rdb instance in (%s)", region) - listInstances, err := rdbAPI.ListInstances(&rdbSDK.ListInstancesRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing rdb instances in (%s) in sweeper: %s", region, err) - } - - for _, instance := range listInstances.Instances { - _, err := rdbAPI.DeleteInstance(&rdbSDK.DeleteInstanceRequest{ - Region: region, - InstanceID: instance.ID, - }) - if err != nil { - return fmt.Errorf("error deleting rdb instance in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&rdbSDK.API{}).Regions(), sweepers.SweepInstance) } diff --git a/internal/services/redis/testfuncs/sweep.go b/internal/services/redis/testfuncs/sweep.go index 6ee1ba490..c5e71eb92 100644 --- a/internal/services/redis/testfuncs/sweep.go +++ b/internal/services/redis/testfuncs/sweep.go @@ -1,13 +1,10 @@ package redistestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" redisSDK "github.com/scaleway/scaleway-sdk-go/api/redis/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/redis/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,26 +15,5 @@ func AddTestSweepers() { } func testSweepRedisCluster(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - redisAPI := redisSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the redis cluster in (%s)", zone) - listClusters, err := redisAPI.ListClusters(&redisSDK.ListClustersRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing redis clusters in (%s) in sweeper: %w", zone, err) - } - - for _, cluster := range listClusters.Clusters { - _, err := redisAPI.DeleteCluster(&redisSDK.DeleteClusterRequest{ - Zone: zone, - ClusterID: cluster.ID, - }) - if err != nil { - return fmt.Errorf("error deleting redis cluster in sweeper: %w", err) - } - } - - return nil - }) + return acctest.SweepZones((&redisSDK.API{}).Zones(), sweepers.SweepCluster) } diff --git a/internal/services/registry/testfuncs/sweep.go b/internal/services/registry/testfuncs/sweep.go index e4f7bf001..a23dd1582 100644 --- a/internal/services/registry/testfuncs/sweep.go +++ b/internal/services/registry/testfuncs/sweep.go @@ -1,13 +1,10 @@ package registrytestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" registrySDK "github.com/scaleway/scaleway-sdk-go/api/registry/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/registry/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,27 +15,5 @@ func AddTestSweepers() { } func testSweepNamespace(_ string) error { - return acctest.SweepRegions([]scw.Region{scw.RegionFrPar, scw.RegionNlAms}, func(scwClient *scw.Client, region scw.Region) error { - registryAPI := registrySDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the registry namespaces in (%s)", region) - listNamespaces, err := registryAPI.ListNamespaces( - ®istrySDK.ListNamespacesRequest{Region: region}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing namespaces in (%s) in sweeper: %s", region, err) - } - - for _, ns := range listNamespaces.Namespaces { - _, err := registryAPI.DeleteNamespace(®istrySDK.DeleteNamespaceRequest{ - NamespaceID: ns.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting namespace in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((®istrySDK.API{}).Regions(), sweepers.SweepNamespace) } diff --git a/internal/services/sdb/testfuncs/sweep.go b/internal/services/sdb/testfuncs/sweep.go index bd36e1944..849cca9e4 100644 --- a/internal/services/sdb/testfuncs/sweep.go +++ b/internal/services/sdb/testfuncs/sweep.go @@ -1,13 +1,10 @@ package sdbtestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" sdbSDK "github.com/scaleway/scaleway-sdk-go/api/serverless_sqldb/v1alpha1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/serverless_sqldb/v1alpha1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,29 +15,5 @@ func AddTestSweepers() { } func testSweepServerlessSQLDBDatabase(_ string) error { - return acctest.SweepRegions((&sdbSDK.API{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { - sdbAPI := sdbSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the serverless sql database in (%s)", region) - listServerlessSQLDBDatabases, err := sdbAPI.ListDatabases( - &sdbSDK.ListDatabasesRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing database in (%s) in sweeper: %s", region, err) - } - - for _, database := range listServerlessSQLDBDatabases.Databases { - _, err := sdbAPI.DeleteDatabase(&sdbSDK.DeleteDatabaseRequest{ - DatabaseID: database.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting database in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&sdbSDK.API{}).Regions(), sweepers.SweepDatabase) } diff --git a/internal/services/secret/testfuncs/sweep.go b/internal/services/secret/testfuncs/sweep.go index 87c4715c6..0fc254d90 100644 --- a/internal/services/secret/testfuncs/sweep.go +++ b/internal/services/secret/testfuncs/sweep.go @@ -1,13 +1,10 @@ package secrettestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" secretSDK "github.com/scaleway/scaleway-sdk-go/api/secret/v1beta1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/secret/v1beta1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,28 +15,5 @@ func AddTestSweepers() { } func testSweepSecret(_ string) error { - return acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - secretAPI := secretSDK.NewAPI(scwClient) - - logging.L.Debugf("sweeper: deleting the secrets in (%s)", region) - - listSecrets, err := secretAPI.ListSecrets(&secretSDK.ListSecretsRequest{Region: region}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing secrets in (%s) in sweeper: %s", region, err) - } - - for _, se := range listSecrets.Secrets { - err := secretAPI.DeleteSecret(&secretSDK.DeleteSecretRequest{ - SecretID: se.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting secret in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&secretSDK.API{}).Regions(), sweepers.SweepSecret) } diff --git a/internal/services/tem/testfuncs/sweep.go b/internal/services/tem/testfuncs/sweep.go index 40fe0670e..b37f62b8b 100644 --- a/internal/services/tem/testfuncs/sweep.go +++ b/internal/services/tem/testfuncs/sweep.go @@ -1,15 +1,15 @@ package temtestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" temSDK "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1" + "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1/sweepers" "github.com/scaleway/scaleway-sdk-go/scw" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) +const skippedDomain = "test.scaleway-terraform.com" + func AddTestSweepers() { resource.AddTestSweepers("scaleway_tem_domain", &resource.Sweeper{ Name: "scaleway_tem_domain", @@ -18,31 +18,7 @@ func AddTestSweepers() { } func testSweepDomain(_ string) error { - return acctest.SweepRegions([]scw.Region{scw.RegionFrPar, scw.RegionNlAms}, func(scwClient *scw.Client, region scw.Region) error { - temAPI := temSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: revoking the tem domains in (%s)", region) - - listDomains, err := temAPI.ListDomains(&temSDK.ListDomainsRequest{Region: region}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing domains in (%s) in sweeper: %s", region, err) - } - - for _, ns := range listDomains.Domains { - if ns.Name == "test.scaleway-terraform.com" { - logging.L.Debugf("sweeper: skipping deletion of domain %s", ns.Name) - continue - } - _, err := temAPI.RevokeDomain(&temSDK.RevokeDomainRequest{ - DomainID: ns.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error revoking domain in sweeper: %s", err) - } - } - - return nil + return acctest.SweepRegions((&temSDK.API{}).Regions(), func(scwClient *scw.Client, region scw.Region) error { + return sweepers.SweepDomain(scwClient, region, skippedDomain) }) } diff --git a/internal/services/vpc/testfuncs/sweep.go b/internal/services/vpc/testfuncs/sweep.go index 36a4fbcca..0c3f7e115 100644 --- a/internal/services/vpc/testfuncs/sweep.go +++ b/internal/services/vpc/testfuncs/sweep.go @@ -1,13 +1,10 @@ package vpctestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" vpcSDK "github.com/scaleway/scaleway-sdk-go/api/vpc/v2" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/vpc/v2/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -30,100 +27,13 @@ func AddTestSweepers() { } func testSweepVPC(_ string) error { - return acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - vpcAPI := vpcSDK.NewAPI(scwClient) - - logging.L.Debugf("sweeper: deleting the VPCs in (%s)", region) - - listVPCs, err := vpcAPI.ListVPCs(&vpcSDK.ListVPCsRequest{Region: region}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing secrets in (%s) in sweeper: %s", region, err) - } - - for _, v := range listVPCs.Vpcs { - if v.IsDefault { - continue - } - err := vpcAPI.DeleteVPC(&vpcSDK.DeleteVPCRequest{ - VpcID: v.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting VPC in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&vpcSDK.API{}).Regions(), sweepers.SweepVPC) } func testSweepVPCPrivateNetwork(_ string) error { - err := acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - vpcAPI := vpcSDK.NewAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the private network in (%s)", region) - - listPNResponse, err := vpcAPI.ListPrivateNetworks(&vpcSDK.ListPrivateNetworksRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing private network in sweeper: %s", err) - } - - for _, pn := range listPNResponse.PrivateNetworks { - err := vpcAPI.DeletePrivateNetwork(&vpcSDK.DeletePrivateNetworkRequest{ - Region: region, - PrivateNetworkID: pn.ID, - }) - if err != nil { - return fmt.Errorf("error deleting private network in sweeper: %s", err) - } - } - - return nil - }) - if err != nil { - return err - } - - return nil + return acctest.SweepRegions((&vpcSDK.API{}).Regions(), sweepers.SweepPrivateNetwork) } func testSweepVPCRoute(_ string) error { - err := acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - vpcAPI := vpcSDK.NewAPI(scwClient) - vpcRouteAPI := vpcSDK.NewRoutesWithNexthopAPI(scwClient) - - logging.L.Debugf("sweeper: destroying the route in (%s)", region) - - listRoutesResponse, err := vpcRouteAPI.ListRoutesWithNexthop(&vpcSDK.RoutesWithNexthopAPIListRoutesWithNexthopRequest{ - Region: region, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing route in sweeper: %s", err) - } - - for _, routeWithNexthop := range listRoutesResponse.Routes { - if routeWithNexthop.Route != nil { - err := vpcAPI.DeleteRoute(&vpcSDK.DeleteRouteRequest{ - Region: region, - RouteID: routeWithNexthop.Route.ID, - }) - if err != nil { - return fmt.Errorf("error deleting route in sweeper: %s", err) - } - } else { - return fmt.Errorf("route is nil in RouteWithNexthop: %v", routeWithNexthop) - } - } - - return nil - }) - if err != nil { - return err - } - - return nil + return acctest.SweepRegions((&vpcSDK.API{}).Regions(), sweepers.SweepRoute) } diff --git a/internal/services/vpcgw/testfuncs/sweep.go b/internal/services/vpcgw/testfuncs/sweep.go index 48f9807b7..1664c01f9 100644 --- a/internal/services/vpcgw/testfuncs/sweep.go +++ b/internal/services/vpcgw/testfuncs/sweep.go @@ -1,13 +1,10 @@ package vpcgwtestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" vpcgwSDK "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -30,104 +27,17 @@ func AddTestSweepers() { } func testSweepVPCPublicGateway(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - api := vpcgwSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the public gateways in (%+v)", zone) - - listGatewayResponse, err := api.ListGateways(&vpcgwSDK.ListGatewaysRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing public gateway in sweeper: %w", err) - } - - for _, gateway := range listGatewayResponse.Gateways { - err := api.DeleteGateway(&vpcgwSDK.DeleteGatewayRequest{ - Zone: zone, - GatewayID: gateway.ID, - }) - if err != nil { - return fmt.Errorf("error deleting public gateway in sweeper: %w", err) - } - } - return nil - }) + return acctest.SweepZones((&vpcgwSDK.API{}).Zones(), sweepers.SweepVPCPublicGateway) } func testSweepVPCGatewayNetwork(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - api := vpcgwSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the gateway network in (%s)", zone) - - listPNResponse, err := api.ListGatewayNetworks(&vpcgwSDK.ListGatewayNetworksRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing gateway network in sweeper: %s", err) - } - - for _, gn := range listPNResponse.GatewayNetworks { - err := api.DeleteGatewayNetwork(&vpcgwSDK.DeleteGatewayNetworkRequest{ - GatewayNetworkID: gn.GatewayID, - Zone: zone, - // Cleanup the dhcp resource related. DON'T CALL THE SWEEPER DHCP - CleanupDHCP: true, - }) - if err != nil { - return fmt.Errorf("error deleting gateway network in sweeper: %s", err) - } - } - return nil - }) + return acctest.SweepZones((&vpcgwSDK.API{}).Zones(), sweepers.SweepGatewayNetworks) } func testSweepVPCPublicGatewayIP(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - api := vpcgwSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying the public gateways ip in (%s)", zone) - - listIPResponse, err := api.ListIPs(&vpcgwSDK.ListIPsRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing public gateway ip in sweeper: %s", err) - } - - for _, ip := range listIPResponse.IPs { - err := api.DeleteIP(&vpcgwSDK.DeleteIPRequest{ - Zone: zone, - IPID: ip.ID, - }) - if err != nil { - return fmt.Errorf("error deleting public gateway ip in sweeper: %s", err) - } - } - return nil - }) + return acctest.SweepZones((&vpcgwSDK.API{}).Zones(), sweepers.SweepVPCPublicGatewayIP) } func testSweepVPCPublicGatewayDHCP(_ string) error { - return acctest.SweepZones(scw.AllZones, func(scwClient *scw.Client, zone scw.Zone) error { - api := vpcgwSDK.NewAPI(scwClient) - logging.L.Debugf("sweeper: destroying public gateway dhcps in (%+v)", zone) - - listDHCPsResponse, err := api.ListDHCPs(&vpcgwSDK.ListDHCPsRequest{ - Zone: zone, - }, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing public gateway dhcps in sweeper: %w", err) - } - - for _, dhcp := range listDHCPsResponse.Dhcps { - err := api.DeleteDHCP(&vpcgwSDK.DeleteDHCPRequest{ - Zone: zone, - DHCPID: dhcp.ID, - }) - if err != nil { - return fmt.Errorf("error deleting public gateway dhcp in sweeper: %w", err) - } - } - - return nil - }) + return acctest.SweepZones((&vpcgwSDK.API{}).Zones(), sweepers.SweepVPCPublicGatewayDHCP) } diff --git a/internal/services/webhosting/testfuncs/sweep.go b/internal/services/webhosting/testfuncs/sweep.go index e75f34f42..565ec3d5d 100644 --- a/internal/services/webhosting/testfuncs/sweep.go +++ b/internal/services/webhosting/testfuncs/sweep.go @@ -1,13 +1,10 @@ package webhostingtestfuncs import ( - "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" webhostingSDK "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1" - "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1/sweepers" "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) func AddTestSweepers() { @@ -18,28 +15,5 @@ func AddTestSweepers() { } func testSweepWebhosting(_ string) error { - return acctest.SweepRegions(scw.AllRegions, func(scwClient *scw.Client, region scw.Region) error { - webhsotingAPI := webhostingSDK.NewAPI(scwClient) - - logging.L.Debugf("sweeper: deleting the hostings in (%s)", region) - - listHostings, err := webhsotingAPI.ListHostings(&webhostingSDK.ListHostingsRequest{Region: region}, scw.WithAllPages()) - if err != nil { - return fmt.Errorf("error listing hostings in (%s) in sweeper: %s", region, err) - } - - for _, hosting := range listHostings.Hostings { - _, err := webhsotingAPI.DeleteHosting(&webhostingSDK.DeleteHostingRequest{ - HostingID: hosting.ID, - Region: region, - }) - if err != nil { - logging.L.Debugf("sweeper: error (%s)", err) - - return fmt.Errorf("error deleting hosting in sweeper: %s", err) - } - } - - return nil - }) + return acctest.SweepRegions((&webhostingSDK.API{}).Regions(), sweepers.SweepWebHosting) }