diff --git a/internal/service/networkpeering/resource_network_peering.go b/internal/service/networkpeering/resource_network_peering.go index f99b489197..c39b1fc70d 100644 --- a/internal/service/networkpeering/resource_network_peering.go +++ b/internal/service/networkpeering/resource_network_peering.go @@ -283,23 +283,11 @@ func resourceMongoDBAtlasNetworkPeeringRead(ctx context.Context, d *schema.Resou return diag.FromErr(fmt.Errorf(errorPeersRead, peerID, err)) } - - /* This fix the bug https://github.com/mongodb/terraform-provider-mongodbatlas/issues/53 - * If the region name of the peering connection resource is the same as the container resource, - * the API returns it as a null value, so this causes the issue mentioned. - */ - var acepterRegionName string - if peer.AccepterRegionName != "" { - acepterRegionName = peer.AccepterRegionName - } else { - container, _, err := conn.Containers.Get(ctx, projectID, peer.ContainerID) - if err != nil { - return diag.FromErr(err) - } - acepterRegionName = strings.ToLower(strings.ReplaceAll(container.RegionName, "_", "-")) + accepterRegionName, err := ensureAccepterRegionName(ctx, peer, conn, projectID) + if err != nil { + return diag.FromErr(err) } - - if err := d.Set("accepter_region_name", acepterRegionName); err != nil { + if err := d.Set("accepter_region_name", accepterRegionName); err != nil { return diag.FromErr(fmt.Errorf("error setting `accepter_region_name` for Network Peering Connection (%s): %s", peerID, err)) } @@ -393,6 +381,25 @@ func resourceMongoDBAtlasNetworkPeeringRead(ctx context.Context, d *schema.Resou return nil } +func ensureAccepterRegionName(ctx context.Context, peer *matlas.Peer, conn *matlas.Client, projectID string) (string, error) { + /* This fix the bug https://github.com/mongodb/terraform-provider-mongodbatlas/issues/53 + * If the region name of the peering connection resource is the same as the container resource, + * the API returns it as a null value, so this causes the issue mentioned. + */ + var acepterRegionName string + if peer.AccepterRegionName != "" { + acepterRegionName = peer.AccepterRegionName + } else { + container, _, err := conn.Containers.Get(ctx, projectID, peer.ContainerID) + if err != nil { + return "", err + } + // network_peering resource must use region of format eu-west-2 while network_container uses EU_WEST_2. + acepterRegionName = conversion.MongoDBRegionToAWSRegion(container.RegionName) + } + return acepterRegionName, nil +} + func resourceMongoDBAtlasNetworkPeeringUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { // Get client connection. conn := meta.(*config.MongoDBClient).Atlas diff --git a/internal/service/networkpeering/resource_network_peering_migration_test.go b/internal/service/networkpeering/resource_network_peering_migration_test.go new file mode 100644 index 0000000000..c6970497b7 --- /dev/null +++ b/internal/service/networkpeering/resource_network_peering_migration_test.go @@ -0,0 +1,11 @@ +package networkpeering_test + +import ( + "testing" + + "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig" +) + +func TestMigNetworkNetworkPeering_basicAWS(t *testing.T) { + mig.CreateAndRunTest(t, basicAWSTestCase(t)) +} diff --git a/internal/service/networkpeering/resource_network_peering_test.go b/internal/service/networkpeering/resource_network_peering_test.go index 7457a0ff59..3db9e81787 100644 --- a/internal/service/networkpeering/resource_network_peering_test.go +++ b/internal/service/networkpeering/resource_network_peering_test.go @@ -15,64 +15,21 @@ import ( matlas "go.mongodb.org/atlas/mongodbatlas" ) -func TestAccNetworkNetworkPeering_basicAWS(t *testing.T) { - var ( - peer matlas.Peer - resourceName = "mongodbatlas_network_peering.test" - dataSourceName = "data.mongodbatlas_network_peering.test" - pluralDataSourceName = "data.mongodbatlas_network_peerings.test" - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - vpcID = os.Getenv("AWS_VPC_ID") - vpcCIDRBlock = os.Getenv("AWS_VPC_CIDR_BLOCK") - awsAccountID = os.Getenv("AWS_ACCOUNT_ID") - awsRegion = os.Getenv("AWS_REGION") - providerName = "AWS" - projectName = acc.RandomProjectName() - ) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckPeeringEnvAWS(t) }, - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - CheckDestroy: acc.CheckDestroyNetworkPeering, - Steps: []resource.TestStep{ - { - Config: testAccMongoDBAtlasNetworkPeeringConfigAWS(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion), - Check: resource.ComposeTestCheckFunc( - testAccCheckMongoDBAtlasNetworkPeeringExists(resourceName, &peer), - resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "container_id"), - resource.TestCheckResourceAttr(resourceName, "provider_name", providerName), - resource.TestCheckResourceAttr(resourceName, "vpc_id", vpcID), - resource.TestCheckResourceAttr(resourceName, "aws_account_id", awsAccountID), - - resource.TestCheckResourceAttrSet(dataSourceName, "project_id"), - resource.TestCheckResourceAttrSet(dataSourceName, "container_id"), - resource.TestCheckResourceAttr(dataSourceName, "provider_name", providerName), - resource.TestCheckResourceAttr(dataSourceName, "vpc_id", vpcID), - resource.TestCheckResourceAttr(dataSourceName, "aws_account_id", awsAccountID), +var ( + resourceName = "mongodbatlas_network_peering.test" + resourceNameContainer = "mongodbatlas_network_container.test" + dataSourceName = "data.mongodbatlas_network_peering.test" + pluralDataSourceName = "data.mongodbatlas_network_peerings.test" +) - resource.TestCheckResourceAttrSet(pluralDataSourceName, "results.#"), - resource.TestCheckResourceAttrSet(pluralDataSourceName, "results.0.provider_name"), - resource.TestCheckResourceAttrSet(pluralDataSourceName, "results.0.vpc_id"), - resource.TestCheckResourceAttrSet(pluralDataSourceName, "results.0.aws_account_id"), - ), - }, - { - ResourceName: resourceName, - ImportStateIdFunc: testAccCheckMongoDBAtlasNetworkPeeringImportStateIDFunc(resourceName), - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"accepter_region_name", "container_id"}, - }, - }, - }) +func TestAccNetworkNetworkPeering_basicAWS(t *testing.T) { + resource.ParallelTest(t, *basicAWSTestCase(t)) } func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) { acc.SkipTestForCI(t) var ( peer matlas.Peer - resourceName = "mongodbatlas_network_peering.test" projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID") directoryID = os.Getenv("AZURE_DIRECTORY_ID") subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID") @@ -87,9 +44,9 @@ func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) { CheckDestroy: acc.CheckDestroyNetworkPeering, Steps: []resource.TestStep{ { - Config: testAccMongoDBAtlasNetworkPeeringConfigAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName), + Config: configAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName), Check: resource.ComposeTestCheckFunc( - testAccCheckMongoDBAtlasNetworkPeeringExists(resourceName, &peer), + checkExists(resourceName, &peer), resource.TestCheckResourceAttrSet(resourceName, "project_id"), resource.TestCheckResourceAttrSet(resourceName, "container_id"), resource.TestCheckResourceAttr(resourceName, "provider_name", providerName), @@ -99,7 +56,7 @@ func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) { }, { ResourceName: resourceName, - ImportStateIdFunc: testAccCheckMongoDBAtlasNetworkPeeringImportStateIDFunc(resourceName), + ImportStateIdFunc: importStateIDFunc(resourceName), ImportState: true, ImportStateVerify: true, ImportStateVerifyIgnore: []string{"container_id"}, @@ -112,7 +69,6 @@ func TestAccNetworkRSNetworkPeering_basicGCP(t *testing.T) { acc.SkipTestForCI(t) var ( peer matlas.Peer - resourceName = "mongodbatlas_network_peering.test" projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID") providerName = "GCP" gcpProjectID = os.Getenv("GCP_PROJECT_ID") @@ -125,9 +81,9 @@ func TestAccNetworkRSNetworkPeering_basicGCP(t *testing.T) { CheckDestroy: acc.CheckDestroyNetworkPeering, Steps: []resource.TestStep{ { - Config: testAccMongoDBAtlasNetworkPeeringConfigGCP(projectID, providerName, gcpProjectID, networkName), + Config: configGCP(projectID, providerName, gcpProjectID, networkName), Check: resource.ComposeTestCheckFunc( - testAccCheckMongoDBAtlasNetworkPeeringExists(resourceName, &peer), + checkExists(resourceName, &peer), resource.TestCheckResourceAttrSet(resourceName, "project_id"), resource.TestCheckResourceAttrSet(resourceName, "container_id"), resource.TestCheckResourceAttrSet(resourceName, "network_name"), @@ -139,7 +95,7 @@ func TestAccNetworkRSNetworkPeering_basicGCP(t *testing.T) { }, { ResourceName: resourceName, - ImportStateIdFunc: testAccCheckMongoDBAtlasNetworkPeeringImportStateIDFunc(resourceName), + ImportStateIdFunc: importStateIDFunc(resourceName), ImportState: true, ImportStateVerify: true, }, @@ -148,23 +104,28 @@ func TestAccNetworkRSNetworkPeering_basicGCP(t *testing.T) { } func TestAccNetworkRSNetworkPeering_AWSDifferentRegionName(t *testing.T) { - acc.SkipTestForCI(t) var ( - peer matlas.Peer - resourcePeerName = "mongodbatlas_network_peering.diff_region" - resourceContainerName = "mongodbatlas_network_container.test" - projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID") - vpcID = os.Getenv("AWS_VPC_ID") - vpcCIDRBlock = os.Getenv("AWS_VPC_CIDR_BLOCK") - awsAccountID = os.Getenv("AWS_ACCOUNT_ID") - containerRegion = "US_WEST_2" - peerRegion = strings.ToLower(strings.ReplaceAll(os.Getenv("AWS_REGION"), "_", "-")) - providerName = "AWS" + vpcID = os.Getenv("AWS_VPC_ID") + vpcCIDRBlock = os.Getenv("AWS_VPC_CIDR_BLOCK") + awsAccountID = os.Getenv("AWS_ACCOUNT_ID") + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + containerRegion = "US_WEST_2" + peerRegion = conversion.MongoDBRegionToAWSRegion(os.Getenv("AWS_REGION")) + providerName = "AWS" + projectName = acc.RandomProjectName() ) + checks := commonChecksAWS(vpcID, providerName, awsAccountID, vpcCIDRBlock, peerRegion) + checks = acc.AddAttrSetChecks(resourceNameContainer, checks, "project_id", "atlas_cidr_block", "provider_name", "region_name") + mapChecksContainer := map[string]string{ + "atlas_cidr_block": "192.168.208.0/21", + "provider_name": providerName, + "region_name": containerRegion, + } + checks = acc.AddAttrChecks(resourceNameContainer, checks, mapChecksContainer) - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { - acc.PreCheck(t) + acc.PreCheckBasic(t) acc.PreCheckPeeringEnvAWS(t) func() { if strings.EqualFold(containerRegion, peerRegion) { @@ -176,42 +137,65 @@ func TestAccNetworkRSNetworkPeering_AWSDifferentRegionName(t *testing.T) { CheckDestroy: acc.CheckDestroyNetworkPeering, Steps: []resource.TestStep{ { - Config: testAccMongoDBAtlasNetworkPeeringConfigAWSWithDifferentRegionName(projectID, providerName, containerRegion, peerRegion, vpcCIDRBlock, vpcID, awsAccountID), - Check: resource.ComposeTestCheckFunc( - // Peering test - testAccCheckMongoDBAtlasNetworkPeeringExists(resourcePeerName, &peer), - resource.TestCheckResourceAttrSet(resourcePeerName, "accepter_region_name"), - resource.TestCheckResourceAttrSet(resourcePeerName, "project_id"), - resource.TestCheckResourceAttrSet(resourcePeerName, "container_id"), - resource.TestCheckResourceAttrSet(resourcePeerName, "provider_name"), - resource.TestCheckResourceAttrSet(resourcePeerName, "route_table_cidr_block"), - resource.TestCheckResourceAttrSet(resourcePeerName, "vpc_id"), - resource.TestCheckResourceAttrSet(resourcePeerName, "aws_account_id"), - - resource.TestCheckResourceAttr(resourcePeerName, "accepter_region_name", peerRegion), - resource.TestCheckResourceAttr(resourcePeerName, "project_id", projectID), - resource.TestCheckResourceAttr(resourcePeerName, "provider_name", providerName), - resource.TestCheckResourceAttr(resourcePeerName, "route_table_cidr_block", vpcCIDRBlock), - resource.TestCheckResourceAttr(resourcePeerName, "vpc_id", vpcID), - resource.TestCheckResourceAttr(resourcePeerName, "aws_account_id", awsAccountID), + Config: configAWS(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, containerRegion, peerRegion), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} - // Container test - resource.TestCheckResourceAttrSet(resourceContainerName, "project_id"), - resource.TestCheckResourceAttrSet(resourceContainerName, "atlas_cidr_block"), - resource.TestCheckResourceAttrSet(resourceContainerName, "provider_name"), - resource.TestCheckResourceAttrSet(resourceContainerName, "region_name"), +func basicAWSTestCase(tb testing.TB) *resource.TestCase { + tb.Helper() + var ( + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + vpcID = os.Getenv("AWS_VPC_ID") + vpcCIDRBlock = os.Getenv("AWS_VPC_CIDR_BLOCK") + awsAccountID = os.Getenv("AWS_ACCOUNT_ID") + containerRegion = os.Getenv("AWS_REGION") + peerRegion = conversion.MongoDBRegionToAWSRegion(containerRegion) + providerName = "AWS" + projectName = acc.RandomProjectName() + ) + checks := commonChecksAWS(vpcID, providerName, awsAccountID, vpcCIDRBlock, peerRegion) - resource.TestCheckResourceAttr(resourceContainerName, "project_id", projectID), - resource.TestCheckResourceAttr(resourceContainerName, "atlas_cidr_block", "192.168.200.0/21"), - resource.TestCheckResourceAttr(resourceContainerName, "provider_name", providerName), - resource.TestCheckResourceAttr(resourceContainerName, "region_name", containerRegion), - ), + return &resource.TestCase{ + PreCheck: func() { acc.PreCheckBasic(tb); acc.PreCheckPeeringEnvAWS(tb) }, + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + CheckDestroy: acc.CheckDestroyNetworkPeering, + Steps: []resource.TestStep{ + { + Config: configAWS(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, containerRegion, peerRegion), + Check: resource.ComposeTestCheckFunc(checks...), + }, + { + ResourceName: resourceName, + ImportStateIdFunc: importStateIDFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"accepter_region_name", "container_id"}, }, }, - }) + } +} + +func commonChecksAWS(vpcID, providerName, awsAccountID, vpcCIDRBlock, regionPeer string) []resource.TestCheckFunc { + attributes := map[string]string{ + "vpc_id": vpcID, + "provider_name": providerName, + "aws_account_id": awsAccountID, + "route_table_cidr_block": vpcCIDRBlock, + } + checks := []resource.TestCheckFunc{checkExists(resourceName, &matlas.Peer{})} + checks = acc.AddAttrChecks(resourceName, checks, attributes) + checks = append(checks, resource.TestCheckResourceAttr(resourceName, "accepter_region_name", regionPeer)) + checks = acc.AddAttrChecks(dataSourceName, checks, attributes) + checks = acc.AddAttrSetChecks(resourceName, checks, "project_id", "container_id", "accepter_region_name") + checks = acc.AddAttrSetChecks(dataSourceName, checks, "project_id", "container_id") + checks = acc.AddAttrSetChecks(pluralDataSourceName, checks, "results.#", "results.0.provider_name", "results.0.vpc_id", "results.0.aws_account_id") + return checks } -func testAccCheckMongoDBAtlasNetworkPeeringImportStateIDFunc(resourceName string) resource.ImportStateIdFunc { +func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { return func(s *terraform.State) (string, error) { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -224,7 +208,7 @@ func testAccCheckMongoDBAtlasNetworkPeeringImportStateIDFunc(resourceName string } } -func testAccCheckMongoDBAtlasNetworkPeeringExists(resourceName string, peer *matlas.Peer) resource.TestCheckFunc { +func checkExists(resourceName string, peer *matlas.Peer) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -244,41 +228,41 @@ func testAccCheckMongoDBAtlasNetworkPeeringExists(resourceName string, peer *mat } } -func testAccMongoDBAtlasNetworkPeeringConfigAWS(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion string) string { +func configAWS(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegionContainer, awsRegionPeer string) string { return fmt.Sprintf(` - resource "mongodbatlas_project" "my_project" { - name = %[2]q - org_id = %[1]q - } - resource "mongodbatlas_network_container" "test" { - project_id = mongodbatlas_project.my_project.id - atlas_cidr_block = "192.168.208.0/21" - provider_name = "%[3]s" - region_name = "%[7]s" - } + resource "mongodbatlas_project" "my_project" { + name = %[2]q + org_id = %[1]q + } + resource "mongodbatlas_network_container" "test" { + project_id = mongodbatlas_project.my_project.id + atlas_cidr_block = "192.168.208.0/21" + provider_name = %[3]q + region_name = %[7]q + } - resource "mongodbatlas_network_peering" "test" { - accepter_region_name = lower(replace("%[7]s", "_", "-")) - project_id = mongodbatlas_project.my_project.id - container_id = mongodbatlas_network_container.test.id - provider_name = "%[3]s" - route_table_cidr_block = "%[6]s" - vpc_id = "%[4]s" - aws_account_id = "%[5]s" - } + resource "mongodbatlas_network_peering" "test" { + accepter_region_name = %[8]q + project_id = mongodbatlas_project.my_project.id + container_id = mongodbatlas_network_container.test.id + provider_name = %[3]q + route_table_cidr_block = %[6]q + vpc_id = %[4]q + aws_account_id = %[5]q + } - data "mongodbatlas_network_peering" "test" { - project_id = mongodbatlas_project.my_project.id - peering_id = mongodbatlas_network_peering.test.peer_id - } + data "mongodbatlas_network_peering" "test" { + project_id = mongodbatlas_project.my_project.id + peering_id = mongodbatlas_network_peering.test.peer_id + } - data "mongodbatlas_network_peerings" "test" { - project_id = mongodbatlas_network_peering.test.project_id - } - `, orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion) + data "mongodbatlas_network_peerings" "test" { + project_id = mongodbatlas_network_peering.test.project_id + } +`, orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegionContainer, awsRegionPeer) } -func testAccMongoDBAtlasNetworkPeeringConfigAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName string) string { +func configAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName string) string { return fmt.Sprintf(` resource "mongodbatlas_network_container" "test" { project_id = "%[1]s" @@ -299,7 +283,7 @@ func testAccMongoDBAtlasNetworkPeeringConfigAzure(projectID, providerName, direc `, projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName) } -func testAccMongoDBAtlasNetworkPeeringConfigGCP(projectID, providerName, gcpProjectID, networkName string) string { +func configGCP(projectID, providerName, gcpProjectID, networkName string) string { return fmt.Sprintf(` resource "mongodbatlas_network_container" "test" { project_id = "%[1]s" @@ -316,24 +300,3 @@ func testAccMongoDBAtlasNetworkPeeringConfigGCP(projectID, providerName, gcpProj } `, projectID, providerName, gcpProjectID, networkName) } - -func testAccMongoDBAtlasNetworkPeeringConfigAWSWithDifferentRegionName(projectID, providerName, containerRegion, peerRegion, vpcCIDRBlock, vpcID, awsAccountID string) string { - return fmt.Sprintf(` - resource "mongodbatlas_network_container" "test" { - project_id = "%[1]s" - atlas_cidr_block = "192.168.200.0/21" - provider_name = "%[2]s" - region_name = "%[3]s" - } - - resource "mongodbatlas_network_peering" "diff_region" { - accepter_region_name = "%[4]s" - project_id = "%[1]s" - container_id = mongodbatlas_network_container.test.container_id - provider_name = "%[2]s" - route_table_cidr_block = "%[5]s" - vpc_id = "%[6]s" - aws_account_id = "%[7]s" - } - `, projectID, providerName, containerRegion, peerRegion, vpcCIDRBlock, vpcID, awsAccountID) -} diff --git a/internal/testutil/mig/test_case.go b/internal/testutil/mig/test_case.go index cbe8bda5cb..e08a92d6f3 100644 --- a/internal/testutil/mig/test_case.go +++ b/internal/testutil/mig/test_case.go @@ -10,12 +10,13 @@ import ( func CreateAndRunTest(t *testing.T, test *resource.TestCase) { t.Helper() - acc.SkipInUnitTest(t) + acc.SkipInUnitTest(t) // Migration tests create external resources and use MONGODB_ATLAS_LAST_VERSION env-var. resource.ParallelTest(t, CreateTest(t, test)) } func CreateTestAndRunUseExternalProvider(t *testing.T, test *resource.TestCase, externalProviders, additionalProviders map[string]resource.ExternalProvider) { t.Helper() + acc.SkipInUnitTest(t) // Migration tests create external resources and use MONGODB_ATLAS_LAST_VERSION env-var. resource.ParallelTest(t, CreateTestUseExternalProvider(t, test, externalProviders, additionalProviders)) }