Skip to content

Commit 83efed2

Browse files
fix: Define values of altas_gcp_project_id and atlas_vpc_name in network peering by fetching associated container (#2315)
* fix: Define values of altas_gcp_project_id and atlas_vpc_name in network peering by fetching associated container * add additional check in test add update documentation * add changelog entry * Update website/docs/r/network_peering.html.markdown Co-authored-by: Leo Antoli <[email protected]> * adjust changelog entry removing implementation detail --------- Co-authored-by: Leo Antoli <[email protected]>
1 parent d5013d4 commit 83efed2

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

.changelog/2315.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/mongodbatlas_network_peering: Fixes computed values of `altas_gcp_project_id` and `atlas_vpc_name` to provide GCP related values
3+
```

internal/service/networkpeering/data_source_network_peering.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,14 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
164164
}
165165

166166
func readAtlasCidrBlock(ctx context.Context, conn admin.NetworkPeeringApi, projectID, containerID string) (string, error) {
167-
container, _, err := conn.GetPeeringContainer(ctx, projectID, containerID).Execute()
167+
container, err := getContainer(ctx, conn, projectID, containerID)
168168
if err != nil {
169169
return "", err
170170
}
171171
return container.GetAtlasCidrBlock(), nil
172172
}
173+
174+
func getContainer(ctx context.Context, conn admin.NetworkPeeringApi, projectID, containerID string) (*admin.CloudProviderContainer, error) {
175+
container, _, err := conn.GetPeeringContainer(ctx, projectID, containerID).Execute()
176+
return container, err
177+
}

internal/service/networkpeering/resource_network_peering.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
273273
ids := conversion.DecodeStateID(d.Id())
274274
projectID := ids["project_id"]
275275
peerID := ids["peer_id"]
276+
providerName := d.Get("provider_name").(string)
276277

277278
peer, resp, err := conn.NetworkPeeringApi.GetPeeringConnection(ctx, projectID, peerID).Execute()
278279
if err != nil {
@@ -283,20 +284,25 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
283284

284285
return diag.FromErr(fmt.Errorf(errorPeersRead, peerID, err))
285286
}
286-
atlasCidrBlock, err := readAtlasCidrBlock(ctx, conn.NetworkPeeringApi, projectID, peer.GetContainerId())
287+
288+
container, err := getContainer(ctx, conn.NetworkPeeringApi, projectID, peer.GetContainerId())
287289
if err != nil {
288290
return diag.FromErr(err)
289291
}
290-
if err := d.Set("atlas_cidr_block", atlasCidrBlock); err != nil {
292+
293+
if err := d.Set("atlas_cidr_block", container.GetAtlasCidrBlock()); err != nil {
291294
return diag.Errorf("error setting `atlas_cidr_block` for Network Peering Connection (%s): %s", peerID, err)
292295
}
293296

294-
if err := d.Set("atlas_gcp_project_id", peer.GetGcpProjectId()); err != nil {
295-
return diag.FromErr(fmt.Errorf("error setting `atlas_gcp_project_id` for Network Peering Connection (%s): %s", peerID, err))
296-
}
297+
// If provider name is GCP we need to get the parameters to configure the the reciprocal connection between Mongo and Google
298+
if providerName == "GCP" {
299+
if err := d.Set("atlas_gcp_project_id", container.GetGcpProjectId()); err != nil {
300+
return diag.FromErr(fmt.Errorf("error setting `atlas_gcp_project_id` for Network Peering Connection (%s): %s", peerID, err))
301+
}
297302

298-
if err := d.Set("atlas_vpc_name", peer.GetNetworkName()); err != nil {
299-
return diag.FromErr(fmt.Errorf("error setting `atlas_vpc_name` for Network Peering Connection (%s): %s", peerID, err))
303+
if err := d.Set("atlas_vpc_name", container.GetNetworkName()); err != nil {
304+
return diag.FromErr(fmt.Errorf("error setting `atlas_vpc_name` for Network Peering Connection (%s): %s", peerID, err))
305+
}
300306
}
301307

302308
if err := d.Set("peer_id", peerID); err != nil {

internal/service/networkpeering/resource_network_peering_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,14 @@ func TestAccNetworkRSNetworkPeering_basicGCP(t *testing.T) {
125125
checkExists(resourceName),
126126
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
127127
resource.TestCheckResourceAttrSet(resourceName, "container_id"),
128-
resource.TestCheckResourceAttrSet(resourceName, "network_name"),
129128

130129
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
131130
resource.TestCheckResourceAttr(resourceName, "gcp_project_id", gcpProjectID),
132131
resource.TestCheckResourceAttr(resourceName, "network_name", networkName),
132+
133+
// computed values that are obtain from associated container, checks for existing prefix convention to ensure they are gcp related values
134+
resource.TestCheckResourceAttrWith(resourceName, "atlas_gcp_project_id", acc.MatchesExpression("p-.*")),
135+
resource.TestCheckResourceAttrWith(resourceName, "atlas_vpc_name", acc.MatchesExpression("nt-.*")),
133136
),
134137
},
135138
{

website/docs/r/network_peering.html.markdown

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ In addition to all arguments above, the following attributes are exported:
408408
* `gcp_project_id` - GCP project ID of the owner of the network peer.
409409
* `error_message` - When `"status" : "FAILED"`, Atlas provides a description of the error.
410410
* `network_name` - Name of the network peer to which Atlas connects.
411-
* `atlas_gcp_project_id` - The Atlas GCP Project ID for the GCP VPC used by your atlas cluster that it is need to set up the reciprocal connection.
411+
* `atlas_gcp_project_id` - The Atlas GCP Project ID for the GCP VPC used by your atlas cluster that is needed to set up the reciprocal connection.
412+
* `atlas_vpc_name` - Name of the GCP VPC used by your atlas cluster that is needed to set up the reciprocal connection.
412413

413414
**AZURE ONLY:**
414415

@@ -429,4 +430,4 @@ $ terraform import mongodbatlas_network_peering.my_peering 1112222b3bf99403840e8
429430

430431
See detailed information for arguments and attributes: [MongoDB API Network Peering Connection](https://docs.atlas.mongodb.com/reference/api/vpc-create-peering-connection/)
431432

432-
-> **NOTE:** If you need to get an existing container ID see the [How-To Guide](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/guides/howto-guide.html).
433+
-> **NOTE:** If you need to get an existing container ID see the [How-To Guide](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/guides/howto-guide.html).

0 commit comments

Comments
 (0)