From d91fb4c102b2686a00672712ba3206101f65969f Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Sat, 23 Mar 2024 07:12:41 +0100 Subject: [PATCH] checkExists --- .../resource_project_ip_access_list_test.go | 24 ++++++++++++++-- .../testutil/acc/project_ip_acces_list.go | 28 ------------------- 2 files changed, 21 insertions(+), 31 deletions(-) delete mode 100644 internal/testutil/acc/project_ip_acces_list.go diff --git a/internal/service/projectipaccesslist/resource_project_ip_access_list_test.go b/internal/service/projectipaccesslist/resource_project_ip_access_list_test.go index 8aea81382a..79e307f862 100644 --- a/internal/service/projectipaccesslist/resource_project_ip_access_list_test.go +++ b/internal/service/projectipaccesslist/resource_project_ip_access_list_test.go @@ -128,7 +128,7 @@ func TestAccProjectIPAccessList_settingMultiple(t *testing.T) { entry["comment"] = fmt.Sprintf("TestAcc for %s (%s)", entryName, ipAddr) accessList = append(accessList, entry) - checks = append(checks, acc.CheckProjectIPAccessListExists(fmt.Sprintf(resourceFmt, i))) + checks = append(checks, checkExists(fmt.Sprintf(resourceFmt, i))) } resource.ParallelTest(t, resource.TestCase{ @@ -198,6 +198,24 @@ func TestAccProjectIPAccessList_importIncorrectId(t *testing.T) { }) } +func checkExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("not found: %s", resourceName) + } + if rs.Primary.ID == "" { + return fmt.Errorf("no ID is set") + } + 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) does not exist", ids["entry"]) + } + return nil + } +} + func checkDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "mongodbatlas_project_ip_access_list" { @@ -225,8 +243,8 @@ func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { func commonChecks(ipAddress, cidrBlock, awsSGroup, comment string) []resource.TestCheckFunc { checks := []resource.TestCheckFunc{ - acc.CheckProjectIPAccessListExists(resourceName), - acc.CheckProjectIPAccessListExists(dataSourceName), + checkExists(resourceName), + checkExists(dataSourceName), resource.TestCheckResourceAttrSet(resourceName, "project_id"), resource.TestCheckResourceAttrSet(dataSourceName, "project_id"), resource.TestCheckResourceAttr(resourceName, "comment", comment), diff --git a/internal/testutil/acc/project_ip_acces_list.go b/internal/testutil/acc/project_ip_acces_list.go deleted file mode 100644 index 548e7e509d..0000000000 --- a/internal/testutil/acc/project_ip_acces_list.go +++ /dev/null @@ -1,28 +0,0 @@ -package acc - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" -) - -func CheckProjectIPAccessListExists(resourceName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[resourceName] - if !ok { - return fmt.Errorf("not found: %s", resourceName) - } - if rs.Primary.ID == "" { - return fmt.Errorf("no ID is set") - } - 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) does not exist", ids["entry"]) - } - return nil - } -}