Skip to content

Commit

Permalink
importStateIDFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Mar 23, 2024
1 parent f53d670 commit 6c712e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
)

Expand Down Expand Up @@ -162,7 +164,7 @@ func TestAccProjectIPAccessList_importBasic(t *testing.T) {
},
{
ResourceName: resourceName,
ImportStateIdFunc: acc.ImportStateProjecIPAccessListtIDFunc(resourceName),
ImportStateIdFunc: importStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
},
Expand Down Expand Up @@ -195,6 +197,17 @@ func TestAccProjectIPAccessList_importIncorrectId(t *testing.T) {
})
}

func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("not found: %s", resourceName)
}
ids := conversion.DecodeStateID(rs.Primary.ID)
return fmt.Sprintf("%s-%s", ids["project_id"], ids["entry"]), nil
}
}

func commonChecks(ipAddress, cidrBlock, awsSGroup, comment string) []resource.TestCheckFunc {
checks := []resource.TestCheckFunc{
acc.CheckProjectIPAccessListExists(resourceName),
Expand Down
13 changes: 0 additions & 13 deletions internal/testutil/acc/project_ip_acces_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,3 @@ func CheckDestroyProjectIPAccessList(s *terraform.State) error {
}
return nil
}

func ImportStateProjecIPAccessListtIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("not found: %s", resourceName)
}

ids := conversion.DecodeStateID(rs.Primary.ID)

return fmt.Sprintf("%s-%s", ids["project_id"], ids["entry"]), nil
}
}

0 comments on commit 6c712e8

Please sign in to comment.