Skip to content

Commit

Permalink
chore: factor all sweepers in the sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone committed Dec 23, 2024
1 parent 93859f4 commit 5888a48
Show file tree
Hide file tree
Showing 27 changed files with 86 additions and 1,621 deletions.
31 changes: 2 additions & 29 deletions internal/services/account/testfuncs/sweep.go
Original file line number Diff line number Diff line change
@@ -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"

Check failure on line 5 in internal/services/account/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

no required module provides package github.com/scaleway/scaleway-sdk-go/api/account/v3/sweepers; to add it:

Check failure on line 5 in internal/services/account/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

could not import github.com/scaleway/scaleway-sdk-go/api/account/v3/sweepers (invalid package name: "")

Check failure on line 5 in internal/services/account/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/scaleway/scaleway-sdk-go/api/account/v3/sweepers; to add it:
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/logging"
)

func AddTestSweepers() {
Expand All @@ -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)
}
26 changes: 2 additions & 24 deletions internal/services/applesilicon/testfuncs/sweep.go
Original file line number Diff line number Diff line change
@@ -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"

Check failure on line 6 in internal/services/applesilicon/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

no required module provides package github.com/scaleway/scaleway-sdk-go/api/instance/v1/sweepers; to add it:

Check failure on line 6 in internal/services/applesilicon/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

could not import github.com/scaleway/scaleway-sdk-go/api/instance/v1/sweepers (invalid package name: "")

Check failure on line 6 in internal/services/applesilicon/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/scaleway/scaleway-sdk-go/api/instance/v1/sweepers; to add it:
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/logging"
)

func AddTestSweepers() {
Expand All @@ -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)
}
27 changes: 2 additions & 25 deletions internal/services/baremetal/testfuncs/sweep.go
Original file line number Diff line number Diff line change
@@ -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"

Check failure on line 6 in internal/services/baremetal/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

no required module provides package github.com/scaleway/scaleway-sdk-go/api/baremetal/v1/sweepers; to add it:

Check failure on line 6 in internal/services/baremetal/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

could not import github.com/scaleway/scaleway-sdk-go/api/baremetal/v1/sweepers (invalid package name: "")

Check failure on line 6 in internal/services/baremetal/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/scaleway/scaleway-sdk-go/api/baremetal/v1/sweepers; to add it:
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/logging"
)

func AddTestSweepers() {
Expand All @@ -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)
}
57 changes: 3 additions & 54 deletions internal/services/block/testfuncs/sweep.go
Original file line number Diff line number Diff line change
@@ -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"

Check failure on line 6 in internal/services/block/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

no required module provides package github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1/sweepers; to add it:

Check failure on line 6 in internal/services/block/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

could not import github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1/sweepers (invalid package name: "")

Check failure on line 6 in internal/services/block/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1/sweepers; to add it:
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/logging"
)

func AddTestSweepers() {
Expand All @@ -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)
}
165 changes: 4 additions & 161 deletions internal/services/cockpit/testfuncs/sweep.go
Original file line number Diff line number Diff line change
@@ -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"

Check failure on line 5 in internal/services/cockpit/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

no required module provides package github.com/scaleway/scaleway-sdk-go/api/cockpit/v1/sweepers; to add it:

Check failure on line 5 in internal/services/cockpit/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

could not import github.com/scaleway/scaleway-sdk-go/api/cockpit/v1/sweepers (invalid package name: "")

Check failure on line 5 in internal/services/cockpit/testfuncs/sweep.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/scaleway/scaleway-sdk-go/api/cockpit/v1/sweepers; to add it:
"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,
Expand All @@ -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)
}
Loading

0 comments on commit 5888a48

Please sign in to comment.