Skip to content

Commit 69eeb93

Browse files
fix: Sets all attributes of Azure mongodbatlas_network_peering as ForceNew, forcing recreation of the resource when updating (#2299)
* set all necessary attributes for update * changelog * force new when updating azure network peering * Update .changelog/2299.txt Co-authored-by: Agustin Bettati <[email protected]> * run tests in CI * %[]q instead of "%[]s" for parameters of configuration * PreCheckBasic * remove azure from update * mention force new in changelog --------- Co-authored-by: Agustin Bettati <[email protected]>
1 parent a01932e commit 69eeb93

File tree

5 files changed

+94
-34
lines changed

5 files changed

+94
-34
lines changed

.changelog/2299.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: Sets all attributes of Azure network peering as ForceNew, forcing recreation of the resource when updating
3+
```

.github/workflows/acceptance-tests-runner.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ on:
104104
required: true
105105
mongodb_atlas_private_endpoint_dns_name:
106106
required: true
107+
azure_directory_id:
108+
required: true
109+
azure_resource_group_name:
110+
required: true
111+
azure_subscription_id:
112+
required: true
113+
azure_vnet_name:
114+
required: true
115+
azure_vnet_name_updated:
116+
required: true
107117

108118
env:
109119
TF_ACC: 1
@@ -616,6 +626,11 @@ jobs:
616626
AWS_SECURITY_GROUP_2: ${{ vars.AWS_SECURITY_GROUP_2 }}
617627
AWS_VPC_CIDR_BLOCK: ${{ vars.AWS_VPC_CIDR_BLOCK }}
618628
AWS_VPC_ID: ${{ vars.AWS_VPC_ID }}
629+
AZURE_DIRECTORY_ID: ${{ secrets.azure_directory_id }}
630+
AZURE_RESOURCE_GROUP_NAME: ${{ secrets.azure_resource_group_name }}
631+
AZURE_SUBSCRIPTION_ID: ${{ secrets.azure_subscription_id }}
632+
AZURE_VNET_NAME: ${{ secrets.azure_vnet_name }}
633+
AZURE_VNET_NAME_UPDATED: ${{ secrets.azure_vnet_name_updated }}
619634
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
620635
ACCTEST_PACKAGES: |
621636
./internal/service/networkcontainer

.github/workflows/acceptance-tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ jobs:
6464
mongodb_atlas_ldap_ca_certificate: ${{ secrets.MONGODB_ATLAS_LDAP_CA_CERTIFICATE }}
6565
mongodb_atlas_private_endpoint_id: ${{ secrets.MONGODB_ATLAS_PRIVATE_ENDPOINT_ID }}
6666
mongodb_atlas_private_endpoint_dns_name: ${{ secrets.MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME }}
67+
azure_directory_id: ${{ secrets.AZURE_DIRECTORY_ID }}
68+
azure_resource_group_name: ${{ secrets.AZURE_RESOURCE_GROUP_NAME }}
69+
azure_subscription_id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
70+
azure_vnet_name: ${{ secrets.AZURE_VNET_NAME }}
71+
azure_vnet_name_updated: ${{ secrets.AZURE_VNET_NAME_UPDATED }}
6772

6873
with:
6974
terraform_version: ${{ inputs.terraform_version || vars.TF_VERSION_LATEST }}

internal/service/networkpeering/resource_network_peering.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,25 @@ func Resource() *schema.Resource {
103103
Type: schema.TypeString,
104104
Optional: true,
105105
Computed: true,
106+
ForceNew: true,
106107
},
107108
"azure_subscription_id": {
108109
Type: schema.TypeString,
109110
Optional: true,
110111
Computed: true,
112+
ForceNew: true,
111113
},
112114
"resource_group_name": {
113115
Type: schema.TypeString,
114116
Optional: true,
115117
Computed: true,
118+
ForceNew: true,
116119
},
117120
"vnet_name": {
118121
Type: schema.TypeString,
119122
Optional: true,
120123
Computed: true,
124+
ForceNew: true,
121125
},
122126
"error_state": {
123127
Type: schema.TypeString,
@@ -406,33 +410,26 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
406410
ContainerId: conversion.GetEncodedID(d.Get("container_id").(string), "container_id"),
407411
}
408412

413+
// Updating any of the attributes for Azure Network Peering forces a recreation of the network peering.
414+
// Need to check if GCP and AWS have the same behavior
409415
switch peer.GetProviderName() {
410416
case "GCP":
411417
peer.SetGcpProjectId(d.Get("gcp_project_id").(string))
412418
peer.SetNetworkName(d.Get("network_name").(string))
413-
case "AZURE":
414-
if d.HasChange("azure_directory_id") {
415-
peer.SetAzureDirectoryId(d.Get("azure_directory_id").(string))
416-
}
417-
418-
if d.HasChange("azure_subscription_id") {
419-
peer.SetAzureSubscriptionId(d.Get("azure_subscription_id").(string))
420-
}
421-
422-
if d.HasChange("resource_group_name") {
423-
peer.SetResourceGroupName(d.Get("resource_group_name").(string))
424-
}
425-
426-
if d.HasChange("vnet_name") {
427-
peer.SetVnetName(d.Get("vnet_name").(string))
428-
}
429419
default: // AWS by default
430420
region, _ := conversion.ValRegion(d.Get("accepter_region_name"), "network_peering")
431421
peer.SetAccepterRegionName(region)
432422
peer.SetAwsAccountId(d.Get("aws_account_id").(string))
433423
peer.SetRouteTableCidrBlock(d.Get("route_table_cidr_block").(string))
434424
peer.SetVpcId(d.Get("vpc_id").(string))
435425
}
426+
peerConn, resp, getErr := conn.NetworkPeeringApi.GetPeeringConnection(ctx, projectID, peerID).Execute()
427+
if getErr != nil {
428+
if resp != nil && resp.StatusCode == 404 {
429+
return nil
430+
}
431+
}
432+
fmt.Print(peerConn.GetStatus())
436433

437434
_, _, err := conn.NetworkPeeringApi.UpdatePeeringConnection(ctx, projectID, peerID, peer).Execute()
438435
if err != nil {

internal/service/networkpeering/resource_network_peering_test.go

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ func TestAccNetworkNetworkPeering_basicAWS(t *testing.T) {
2626
}
2727

2828
func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) {
29-
acc.SkipTestForCI(t) // needs Azure configuration
30-
3129
var (
32-
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
30+
projectID = acc.ProjectIDExecution(t)
3331
directoryID = os.Getenv("AZURE_DIRECTORY_ID")
3432
subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
3533
resourceGroupName = os.Getenv("AZURE_RESOURCE_GROUP_NAME")
@@ -38,7 +36,7 @@ func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) {
3836
)
3937

4038
resource.Test(t, resource.TestCase{
41-
PreCheck: func() { acc.PreCheck(t); acc.PreCheckPeeringEnvAzure(t) },
39+
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckPeeringEnvAzure(t) },
4240
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
4341
CheckDestroy: acc.CheckDestroyNetworkPeering,
4442
Steps: []resource.TestStep{
@@ -64,6 +62,48 @@ func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) {
6462
})
6563
}
6664

65+
func TestAccNetworkRSNetworkPeering_updateBasicAzure(t *testing.T) {
66+
var (
67+
projectID = acc.ProjectIDExecution(t)
68+
directoryID = os.Getenv("AZURE_DIRECTORY_ID")
69+
subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
70+
resourceGroupName = os.Getenv("AZURE_RESOURCE_GROUP_NAME")
71+
vNetName = os.Getenv("AZURE_VNET_NAME")
72+
updatedvNetName = os.Getenv("AZURE_VNET_NAME_UPDATED")
73+
providerName = "AZURE"
74+
)
75+
76+
resource.Test(t, resource.TestCase{
77+
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckPeeringEnvAzure(t) },
78+
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
79+
CheckDestroy: acc.CheckDestroyNetworkPeering,
80+
Steps: []resource.TestStep{
81+
{
82+
Config: configAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName),
83+
Check: resource.ComposeTestCheckFunc(
84+
checkExists(resourceName),
85+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
86+
resource.TestCheckResourceAttrSet(resourceName, "container_id"),
87+
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
88+
resource.TestCheckResourceAttr(resourceName, "vnet_name", vNetName),
89+
resource.TestCheckResourceAttr(resourceName, "azure_directory_id", directoryID),
90+
),
91+
},
92+
{
93+
Config: configAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, updatedvNetName),
94+
Check: resource.ComposeTestCheckFunc(
95+
checkExists(resourceName),
96+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
97+
resource.TestCheckResourceAttrSet(resourceName, "container_id"),
98+
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
99+
resource.TestCheckResourceAttr(resourceName, "vnet_name", updatedvNetName),
100+
resource.TestCheckResourceAttr(resourceName, "azure_directory_id", directoryID),
101+
),
102+
},
103+
},
104+
})
105+
}
106+
67107
func TestAccNetworkRSNetworkPeering_basicGCP(t *testing.T) {
68108
acc.SkipTestForCI(t) // needs GCP configuration
69109

@@ -262,38 +302,38 @@ func configAWS(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlo
262302
func configAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName string) string {
263303
return fmt.Sprintf(`
264304
resource "mongodbatlas_network_container" "test" {
265-
project_id = "%[1]s"
305+
project_id = %[1]q
266306
atlas_cidr_block = "192.168.208.0/21"
267-
provider_name = "%[2]s"
307+
provider_name = %[2]q
268308
region = "US_EAST_2"
269309
}
270310
271311
resource "mongodbatlas_network_peering" "test" {
272-
project_id = "%[1]s"
312+
project_id = %[1]q
273313
container_id = mongodbatlas_network_container.test.container_id
274-
provider_name = "%[2]s"
275-
azure_directory_id = "%[3]s"
276-
azure_subscription_id = "%[4]s"
277-
resource_group_name = "%[5]s"
278-
vnet_name = "%[6]s"
314+
provider_name = %[2]q
315+
azure_directory_id = %[3]q
316+
azure_subscription_id = %[4]q
317+
resource_group_name = %[5]q
318+
vnet_name = %[6]q
279319
}
280320
`, projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName)
281321
}
282322

283323
func configGCP(projectID, providerName, gcpProjectID, networkName string) string {
284324
return fmt.Sprintf(`
285325
resource "mongodbatlas_network_container" "test" {
286-
project_id = "%[1]s"
326+
project_id = %[1]q
287327
atlas_cidr_block = "192.168.192.0/18"
288-
provider_name = "%[2]s"
328+
provider_name = %[2]q
289329
}
290330
291331
resource "mongodbatlas_network_peering" "test" {
292-
project_id = "%[1]s"
332+
project_id = %[1]q
293333
container_id = mongodbatlas_network_container.test.container_id
294-
provider_name = "%[2]s"
295-
gcp_project_id = "%[3]s"
296-
network_name = "%[4]s"
334+
provider_name = %[2]q
335+
gcp_project_id = %[3]q
336+
network_name = %[4]q
297337
}
298338
`, projectID, providerName, gcpProjectID, networkName)
299339
}

0 commit comments

Comments
 (0)