Skip to content

Commit

Permalink
tests(sweeper): add missing sweepers (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh4d1 authored Apr 15, 2020
1 parent ec7afb1 commit cfe1e6f
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 43 deletions.
34 changes: 34 additions & 0 deletions scaleway/resource_account_ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,42 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
account "github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func init() {
resource.AddTestSweepers("scaleway_account_ssh_key", &resource.Sweeper{
Name: "scaleway_account_ssh_key",
F: testSweepAccountSSHKey,
})
}

func testSweepAccountSSHKey(region string) error {
scwClient, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client in sweeper: %s", err)
}
accountAPI := account.NewAPI(scwClient)

l.Debugf("sweeper: destroying the SSH keys")

listSSHKeys, err := accountAPI.ListSSHKeys(&account.ListSSHKeysRequest{}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("error listing SSH keys in sweeper: %s", err)
}

for _, sshKey := range listSSHKeys.SSHKeys {
err := accountAPI.DeleteSSHKey(&account.DeleteSSHKeyRequest{
SSHKeyID: sshKey.ID,
})
if err != nil {
return fmt.Errorf("error deleting SSH key in sweeper: %s", err)
}
}

return nil
}

func TestAccScalewayAccountSSHKey(t *testing.T) {
name := newRandomName("ssh-key")
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX [email protected]"
Expand Down
32 changes: 32 additions & 0 deletions scaleway/resource_baremetal_server_beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,40 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func init() {
resource.AddTestSweepers("scaleway_baremetal_server_beta", &resource.Sweeper{
Name: "scaleway_baremetal_server_beta",
F: testSweepBaremetalServer,
})
}

func testSweepBaremetalServer(region string) error {
return sweepZones(region, func(scwClient *scw.Client) error {
baremetalAPI := baremetal.NewAPI(scwClient)
zone, _ := scwClient.GetDefaultZone()
l.Debugf("sweeper: destroying the baremetal server in (%s)", zone)
listServers, err := baremetalAPI.ListServers(&baremetal.ListServersRequest{}, scw.WithAllPages())
if err != nil {
l.Warningf("error listing servers in (%s) in sweeper: %s", zone, err)
return nil
}

for _, server := range listServers.Servers {
_, err := baremetalAPI.DeleteServer(&baremetal.DeleteServerRequest{
ServerID: server.ID,
})
if err != nil {
return fmt.Errorf("error deleting server in sweeper: %s", err)
}
}

return nil
})
}

func TestAccScalewayBaremetalServerBetaMinimal1(t *testing.T) {
SSHKeyName := newRandomName("ssh-key")
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM7HUxRyQtB2rnlhQUcbDGCZcTJg7OvoznOiyC9W6IxH [email protected]"
Expand Down
32 changes: 32 additions & 0 deletions scaleway/resource_instance_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,40 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func init() {
resource.AddTestSweepers("scaleway_instance_ip", &resource.Sweeper{
Name: "scaleway_instance_ip",
F: testSweepInstanceIP,
})
}

func testSweepInstanceIP(region string) error {
return sweepZones(region, func(scwClient *scw.Client) error {
instanceAPI := instance.NewAPI(scwClient)
zone, _ := scwClient.GetDefaultZone()
l.Debugf("sweeper: destroying the instance ip in (%s)", zone)
listIPs, err := instanceAPI.ListIPs(&instance.ListIPsRequest{}, scw.WithAllPages())
if err != nil {
l.Warningf("error listing ips in (%s) in sweeper: %s", zone, err)
return nil
}

for _, ip := range listIPs.IPs {
err := instanceAPI.DeleteIP(&instance.DeleteIPRequest{
IP: ip.ID,
})
if err != nil {
return fmt.Errorf("error deleting ip in sweeper: %s", err)
}
}

return nil
})
}

func TestAccScalewayInstanceIP(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
32 changes: 32 additions & 0 deletions scaleway/resource_instance_placement_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,40 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func init() {
resource.AddTestSweepers("scaleway_instance_placement_group", &resource.Sweeper{
Name: "scaleway_instance_placement_group",
F: testSweepInstancePlacementGroup,
})
}

func testSweepInstancePlacementGroup(region string) error {
return sweepZones(region, func(scwClient *scw.Client) error {
instanceAPI := instance.NewAPI(scwClient)
zone, _ := scwClient.GetDefaultZone()
l.Debugf("sweeper: destroying the instance placement group in (%s)", zone)
listPlacementGroups, err := instanceAPI.ListPlacementGroups(&instance.ListPlacementGroupsRequest{}, scw.WithAllPages())
if err != nil {
l.Warningf("error listing placement groups in (%s) in sweeper: %s", zone, err)
return nil
}

for _, pg := range listPlacementGroups.PlacementGroups {
err := instanceAPI.DeletePlacementGroup(&instance.DeletePlacementGroupRequest{
PlacementGroupID: pg.ID,
})
if err != nil {
return fmt.Errorf("error deleting placement group in sweeper: %s", err)
}
}

return nil
})
}

func TestAccScalewayInstancePlacementGroup(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
45 changes: 22 additions & 23 deletions scaleway/resource_instance_security_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,33 +407,32 @@ func testAccCheckScalewayInstanceSecurityGroupDestroy(s *terraform.State) error
}

func testSweepComputeInstanceSecurityGroup(region string) error {
scwClient, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client in sweeper: %s", err)
}
instanceAPI := instance.NewAPI(scwClient)

l.Debugf("sweeper: destroying the security groups in (%s)", region)

listResp, err := instanceAPI.ListSecurityGroups(&instance.ListSecurityGroupsRequest{})
if err != nil {
return fmt.Errorf("error listing security groups in sweeper: %s", err)
}
return sweepZones(region, func(scwClient *scw.Client) error {
instanceAPI := instance.NewAPI(scwClient)
zone, _ := scwClient.GetDefaultZone()
l.Debugf("sweeper: destroying the security groups in (%s)", zone)

for _, securityGroup := range listResp.SecurityGroups {
// Can't delete default security group.
if securityGroup.OrganizationDefault {
continue
}
err = instanceAPI.DeleteSecurityGroup(&instance.DeleteSecurityGroupRequest{
SecurityGroupID: securityGroup.ID,
})
listResp, err := instanceAPI.ListSecurityGroups(&instance.ListSecurityGroupsRequest{}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("error deleting security groups in sweeper: %s", err)
l.Warningf("error listing security groups in sweeper: %s", err)
return nil
}
}

return nil
for _, securityGroup := range listResp.SecurityGroups {
// Can't delete default security group.
if securityGroup.OrganizationDefault {
continue
}
err = instanceAPI.DeleteSecurityGroup(&instance.DeleteSecurityGroupRequest{
SecurityGroupID: securityGroup.ID,
})
if err != nil {
return fmt.Errorf("error deleting security groups in sweeper: %s", err)
}
}

return nil
})
}

// Test that we can add / update / delete rules
Expand Down
42 changes: 42 additions & 0 deletions scaleway/resource_instance_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,50 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func init() {
resource.AddTestSweepers("scaleway_instance_server", &resource.Sweeper{
Name: "scaleway_instance_server",
F: testSweepInstanceServer,
})
}

func testSweepInstanceServer(region string) error {
return sweepZones(region, func(scwClient *scw.Client) error {
instanceAPI := instance.NewAPI(scwClient)
zone, _ := scwClient.GetDefaultZone()
l.Debugf("sweeper: destroying the instance server in (%s)", zone)
listServers, err := instanceAPI.ListServers(&instance.ListServersRequest{}, scw.WithAllPages())
if err != nil {
l.Warningf("error listing servers in (%s) in sweeper: %s", zone, err)
return nil
}

for _, srv := range listServers.Servers {
if srv.State == instance.ServerStateStopped || srv.State == instance.ServerStateStoppedInPlace {
err := instanceAPI.DeleteServer(&instance.DeleteServerRequest{
ServerID: srv.ID,
})
if err != nil {
return fmt.Errorf("error deleting server in sweeper: %s", err)
}
} else if srv.State == instance.ServerStateRunning {
_, err := instanceAPI.ServerAction(&instance.ServerActionRequest{
ServerID: srv.ID,
Action: instance.ServerActionTerminate,
})
if err != nil {
return fmt.Errorf("error terminating server in sweeper: %s", err)
}
}
}

return nil
})
}

func TestAccScalewayInstanceServerMinimal1(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
43 changes: 23 additions & 20 deletions scaleway/resource_instance_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func init() {
Expand All @@ -17,30 +18,32 @@ func init() {
}

func testSweepComputeInstanceVolume(region string) error {
scwClient, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client in sweeper: %s", err)
}
instanceAPI := instance.NewAPI(scwClient)

l.Debugf("sweeper: destroying the volumes in (%s)", region)

listVolumesResponse, err := instanceAPI.ListVolumes(&instance.ListVolumesRequest{})
if err != nil {
return fmt.Errorf("error listing volumes in sweeper: %s", err)
}

for _, volume := range listVolumesResponse.Volumes {
err := instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{
VolumeID: volume.ID,
})
return sweepZones(region, func(scwClient *scw.Client) error {
instanceAPI := instance.NewAPI(scwClient)
zone, _ := scwClient.GetDefaultZone()
l.Debugf("sweeper: destroying the volumes in (%s)", zone)

listVolumesResponse, err := instanceAPI.ListVolumes(&instance.ListVolumesRequest{
Zone: zone,
}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("error deleting volume in sweeper: %s", err)
return fmt.Errorf("error listing volumes in sweeper: %s", err)
}
}

return nil
for _, volume := range listVolumesResponse.Volumes {
if volume.Server == nil {
err := instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{
Zone: zone,
VolumeID: volume.ID,
})
if err != nil {
return fmt.Errorf("error deleting volume in sweeper: %s", err)
}
}
}

return nil
})
}

func TestAccScalewayInstanceVolume_Basic(t *testing.T) {
Expand Down
33 changes: 33 additions & 0 deletions scaleway/resource_k8s_cluster_beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,41 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func init() {
resource.AddTestSweepers("scaleway_k8s_cluster_beta", &resource.Sweeper{
Name: "scaleway_k8s_cluster_beta",
F: testSweepK8SCluster,
})
}

func testSweepK8SCluster(region string) error {
scwClient, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client in sweeper: %s", err)
}
k8sAPI := k8s.NewAPI(scwClient)

l.Debugf("sweeper: destroying the k8s cluster in (%s)", region)
listClusters, err := k8sAPI.ListClusters(&k8s.ListClustersRequest{}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("error listing clusters in (%s) in sweeper: %s", region, err)
}

for _, cluster := range listClusters.Clusters {
_, err := k8sAPI.DeleteCluster(&k8s.DeleteClusterRequest{
ClusterID: cluster.ID,
})
if err != nil {
return fmt.Errorf("error deleting cluster in sweeper: %s", err)
}
}

return nil
}

func TestAccScalewayK8SClusterDeprecated(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
Loading

0 comments on commit cfe1e6f

Please sign in to comment.