Skip to content

Commit

Permalink
test: Adds migration test to networkpeering for AWS (#2094)
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenAlbert authored Apr 2, 2024
1 parent 0b948ee commit e1f4534
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 170 deletions.
39 changes: 23 additions & 16 deletions internal/service/networkpeering/resource_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
Loading

0 comments on commit e1f4534

Please sign in to comment.