Skip to content

Commit

Permalink
checkDestroy
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Mar 23, 2024
1 parent 6c712e8 commit 84bcc77
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMigProjectIPAccessList_settingIPAddress(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Expand All @@ -42,7 +42,7 @@ func TestMigProjectIPAccessList_settingCIDRBlock(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Expand All @@ -69,7 +69,7 @@ func TestMigProjectIPAccessList_settingAWSSecurityGroup(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckPeeringEnvAWS(t) },
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package projectipaccesslist_test

import (
"context"
"fmt"
"os"
"regexp"
Expand Down Expand Up @@ -30,7 +31,7 @@ func TestAccProjectIPAccesslist_settingIPAddress(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWithIPAddress(projectID, ipAddress, comment),
Expand All @@ -56,7 +57,7 @@ func TestAccProjectIPAccessList_settingCIDRBlock(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWithCIDRBlock(projectID, cidrBlock, comment),
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestAccProjectIPAccessList_settingAWSSecurityGroup(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckPeeringEnvAWS(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWithAWSSecurityGroup(projectID, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion, awsSGroup, comment),
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestAccProjectIPAccessList_settingMultiple(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWithMultiple(projectID, accessList, false),
Expand All @@ -157,7 +158,7 @@ func TestAccProjectIPAccessList_importBasic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWithIPAddress(projectID, ipAddress, comment),
Expand All @@ -182,7 +183,7 @@ func TestAccProjectIPAccessList_importIncorrectId(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyProjectIPAccessList,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWithIPAddress(projectID, ipAddress, comment),
Expand All @@ -197,6 +198,20 @@ func TestAccProjectIPAccessList_importIncorrectId(t *testing.T) {
})
}

func checkDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "mongodbatlas_project_ip_access_list" {
continue
}
ids := conversion.DecodeStateID(rs.Primary.ID)
_, _, err := acc.ConnV2().ProjectIPAccessListApi.GetProjectIpList(context.Background(), ids["project_id"], ids["entry"]).Execute()
if err == nil {
return fmt.Errorf("project ip access list entry (%s) still exists", ids["entry"])
}
}
return nil
}

func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down
14 changes: 0 additions & 14 deletions internal/testutil/acc/project_ip_acces_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,3 @@ func CheckProjectIPAccessListExists(resourceName string) resource.TestCheckFunc
return nil
}
}

func CheckDestroyProjectIPAccessList(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "mongodbatlas_project_ip_access_list" {
continue
}
ids := conversion.DecodeStateID(rs.Primary.ID)
_, _, err := ConnV2().ProjectIPAccessListApi.GetProjectIpList(context.Background(), ids["project_id"], ids["entry"]).Execute()
if err == nil {
return fmt.Errorf("project ip access list entry (%s) still exists", ids["entry"])
}
}
return nil
}

0 comments on commit 84bcc77

Please sign in to comment.