From 74d466574d7df4b47e512420503966c48721c574 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Thu, 23 May 2024 10:15:44 +0200 Subject: [PATCH 1/9] set all necessary attributes for update --- .../resource_network_peering.go | 19 ++++--------------- .../resource_network_peering_test.go | 2 +- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/internal/service/networkpeering/resource_network_peering.go b/internal/service/networkpeering/resource_network_peering.go index d9a665bd57..bf47a638ce 100644 --- a/internal/service/networkpeering/resource_network_peering.go +++ b/internal/service/networkpeering/resource_network_peering.go @@ -411,21 +411,10 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. peer.SetGcpProjectId(d.Get("gcp_project_id").(string)) peer.SetNetworkName(d.Get("network_name").(string)) case "AZURE": - if d.HasChange("azure_directory_id") { - peer.SetAzureDirectoryId(d.Get("azure_directory_id").(string)) - } - - if d.HasChange("azure_subscription_id") { - peer.SetAzureSubscriptionId(d.Get("azure_subscription_id").(string)) - } - - if d.HasChange("resource_group_name") { - peer.SetResourceGroupName(d.Get("resource_group_name").(string)) - } - - if d.HasChange("vnet_name") { - peer.SetVnetName(d.Get("vnet_name").(string)) - } + peer.SetAzureDirectoryId(d.Get("azure_directory_id").(string)) + peer.SetAzureSubscriptionId(d.Get("azure_subscription_id").(string)) + peer.SetResourceGroupName(d.Get("resource_group_name").(string)) + peer.SetVnetName(d.Get("vnet_name").(string)) default: // AWS by default region, _ := conversion.ValRegion(d.Get("accepter_region_name"), "network_peering") peer.SetAccepterRegionName(region) diff --git a/internal/service/networkpeering/resource_network_peering_test.go b/internal/service/networkpeering/resource_network_peering_test.go index 21f3c37633..313197f251 100644 --- a/internal/service/networkpeering/resource_network_peering_test.go +++ b/internal/service/networkpeering/resource_network_peering_test.go @@ -275,7 +275,7 @@ func configAzure(projectID, providerName, directoryID, subscriptionID, resourceG azure_directory_id = "%[3]s" azure_subscription_id = "%[4]s" resource_group_name = "%[5]s" - vnet_name = "%[6]s" + vnet_name = "%[6]s" } `, projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName) } From b8fca6cf608f4412af755c56869fe329377de695 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Thu, 23 May 2024 10:46:30 +0200 Subject: [PATCH 2/9] changelog --- .changelog/2299.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2299.txt diff --git a/.changelog/2299.txt b/.changelog/2299.txt new file mode 100644 index 0000000000..b3cdf013f8 --- /dev/null +++ b/.changelog/2299.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/mongodbatlas_network_peering: sets all necessary attributes for update of Azure network peering +``` From 864a354621e218bae8eef0060b942bd112df8727 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Thu, 23 May 2024 15:05:34 +0200 Subject: [PATCH 3/9] force new when updating azure network peering --- .../resource_network_peering.go | 11 +++++ .../resource_network_peering_test.go | 44 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/internal/service/networkpeering/resource_network_peering.go b/internal/service/networkpeering/resource_network_peering.go index bf47a638ce..cb2a636240 100644 --- a/internal/service/networkpeering/resource_network_peering.go +++ b/internal/service/networkpeering/resource_network_peering.go @@ -103,21 +103,25 @@ func Resource() *schema.Resource { Type: schema.TypeString, Optional: true, Computed: true, + ForceNew: true, }, "azure_subscription_id": { Type: schema.TypeString, Optional: true, Computed: true, + ForceNew: true, }, "resource_group_name": { Type: schema.TypeString, Optional: true, Computed: true, + ForceNew: true, }, "vnet_name": { Type: schema.TypeString, Optional: true, Computed: true, + ForceNew: true, }, "error_state": { Type: schema.TypeString, @@ -422,6 +426,13 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. peer.SetRouteTableCidrBlock(d.Get("route_table_cidr_block").(string)) peer.SetVpcId(d.Get("vpc_id").(string)) } + peerConn, resp, getErr := conn.NetworkPeeringApi.GetPeeringConnection(ctx, projectID, peerID).Execute() + if getErr != nil { + if resp != nil && resp.StatusCode == 404 { + return nil + } + } + fmt.Print(peerConn.GetStatus()) _, _, err := conn.NetworkPeeringApi.UpdatePeeringConnection(ctx, projectID, peerID, peer).Execute() if err != nil { diff --git a/internal/service/networkpeering/resource_network_peering_test.go b/internal/service/networkpeering/resource_network_peering_test.go index 313197f251..6a8d0bc287 100644 --- a/internal/service/networkpeering/resource_network_peering_test.go +++ b/internal/service/networkpeering/resource_network_peering_test.go @@ -64,6 +64,50 @@ func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) { }) } +func TestAccNetworkRSNetworkPeering_updateBasicAzure(t *testing.T) { + acc.SkipTestForCI(t) // needs Azure configuration + + var ( + projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID") + directoryID = os.Getenv("AZURE_DIRECTORY_ID") + subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID") + resourceGroupName = os.Getenv("AZURE_RESOURCE_GROUP_NAME") + vNetName = os.Getenv("AZURE_VNET_NAME") + updatedvNetName = os.Getenv("AZURE_VNET_NAME_UPDATED") + providerName = "AZURE" + ) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.PreCheck(t); acc.PreCheckPeeringEnvAzure(t) }, + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + CheckDestroy: acc.CheckDestroyNetworkPeering, + Steps: []resource.TestStep{ + { + Config: configAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName), + Check: resource.ComposeTestCheckFunc( + checkExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "project_id"), + resource.TestCheckResourceAttrSet(resourceName, "container_id"), + resource.TestCheckResourceAttr(resourceName, "provider_name", providerName), + resource.TestCheckResourceAttr(resourceName, "vnet_name", vNetName), + resource.TestCheckResourceAttr(resourceName, "azure_directory_id", directoryID), + ), + }, + { + Config: configAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, updatedvNetName), + Check: resource.ComposeTestCheckFunc( + checkExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "project_id"), + resource.TestCheckResourceAttrSet(resourceName, "container_id"), + resource.TestCheckResourceAttr(resourceName, "provider_name", providerName), + resource.TestCheckResourceAttr(resourceName, "vnet_name", updatedvNetName), + resource.TestCheckResourceAttr(resourceName, "azure_directory_id", directoryID), + ), + }, + }, + }) +} + func TestAccNetworkRSNetworkPeering_basicGCP(t *testing.T) { acc.SkipTestForCI(t) // needs GCP configuration From 0e84e2740a65fdb22b1f1858538b7ab4db9a1c14 Mon Sep 17 00:00:00 2001 From: Oriol Date: Fri, 24 May 2024 09:59:44 +0200 Subject: [PATCH 4/9] Update .changelog/2299.txt Co-authored-by: Agustin Bettati --- .changelog/2299.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/2299.txt b/.changelog/2299.txt index b3cdf013f8..f9c501fcf8 100644 --- a/.changelog/2299.txt +++ b/.changelog/2299.txt @@ -1,3 +1,3 @@ ```release-note:bug -resource/mongodbatlas_network_peering: sets all necessary attributes for update of Azure network peering +resource/mongodbatlas_network_peering: Sets all necessary attributes for update of Azure network peering ``` From 0733f539ce711b86140d89debc3339629d5108ca Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 24 May 2024 10:35:26 +0200 Subject: [PATCH 5/9] run tests in CI --- .github/workflows/acceptance-tests-runner.yml | 15 +++++++++++++++ .github/workflows/acceptance-tests.yml | 5 +++++ .../resource_network_peering_test.go | 8 ++------ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/.github/workflows/acceptance-tests-runner.yml b/.github/workflows/acceptance-tests-runner.yml index fd50f51b0a..5162583025 100644 --- a/.github/workflows/acceptance-tests-runner.yml +++ b/.github/workflows/acceptance-tests-runner.yml @@ -104,6 +104,16 @@ on: required: true mongodb_atlas_private_endpoint_dns_name: required: true + azure_directory_id: + required: true + azure_resource_group_name: + required: true + azure_subscription_id: + required: true + azure_vnet_name: + required: true + azure_vnet_name_updated: + required: true env: TF_ACC: 1 @@ -616,6 +626,11 @@ jobs: AWS_SECURITY_GROUP_2: ${{ vars.AWS_SECURITY_GROUP_2 }} AWS_VPC_CIDR_BLOCK: ${{ vars.AWS_VPC_CIDR_BLOCK }} AWS_VPC_ID: ${{ vars.AWS_VPC_ID }} + AZURE_DIRECTORY_ID: ${{ secrets.azure_directory_id }} + AZURE_RESOURCE_GROUP_NAME: ${{ secrets.azure_resource_group_name }} + AZURE_SUBSCRIPTION_ID: ${{ secrets.azure_subscription_id }} + AZURE_VNET_NAME: ${{ secrets.azure_vnet_name }} + AZURE_VNET_NAME_UPDATED: ${{ secrets.azure_vnet_name_updated }} MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }} ACCTEST_PACKAGES: | ./internal/service/networkcontainer diff --git a/.github/workflows/acceptance-tests.yml b/.github/workflows/acceptance-tests.yml index 28272bedf4..56d8d1bb91 100644 --- a/.github/workflows/acceptance-tests.yml +++ b/.github/workflows/acceptance-tests.yml @@ -64,6 +64,11 @@ jobs: mongodb_atlas_ldap_ca_certificate: ${{ secrets.MONGODB_ATLAS_LDAP_CA_CERTIFICATE }} mongodb_atlas_private_endpoint_id: ${{ secrets.MONGODB_ATLAS_PRIVATE_ENDPOINT_ID }} mongodb_atlas_private_endpoint_dns_name: ${{ secrets.MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME }} + azure_directory_id: ${{ secrets.AZURE_DIRECTORY_ID }} + azure_resource_group_name: ${{ secrets.AZURE_RESOURCE_GROUP_NAME }} + azure_subscription_id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} + azure_vnet_name: ${{ secrets.AZURE_VNET_NAME }} + azure_vnet_name_updated: ${{ secrets.AZURE_VNET_NAME_UPDATED }} with: terraform_version: ${{ inputs.terraform_version || vars.TF_VERSION_LATEST }} diff --git a/internal/service/networkpeering/resource_network_peering_test.go b/internal/service/networkpeering/resource_network_peering_test.go index 6a8d0bc287..53ea0c4752 100644 --- a/internal/service/networkpeering/resource_network_peering_test.go +++ b/internal/service/networkpeering/resource_network_peering_test.go @@ -26,10 +26,8 @@ func TestAccNetworkNetworkPeering_basicAWS(t *testing.T) { } func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) { - acc.SkipTestForCI(t) // needs Azure configuration - var ( - projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID") + projectID = acc.ProjectIDExecution(t) directoryID = os.Getenv("AZURE_DIRECTORY_ID") subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID") resourceGroupName = os.Getenv("AZURE_RESOURCE_GROUP_NAME") @@ -65,10 +63,8 @@ func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) { } func TestAccNetworkRSNetworkPeering_updateBasicAzure(t *testing.T) { - acc.SkipTestForCI(t) // needs Azure configuration - var ( - projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID") + projectID = acc.ProjectIDExecution(t) directoryID = os.Getenv("AZURE_DIRECTORY_ID") subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID") resourceGroupName = os.Getenv("AZURE_RESOURCE_GROUP_NAME") From b25818db3913f52360b576d635fecaf718fe7ff5 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 24 May 2024 10:39:11 +0200 Subject: [PATCH 6/9] %[]q instead of "%[]s" for parameters of configuration --- .../resource_network_peering_test.go | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/service/networkpeering/resource_network_peering_test.go b/internal/service/networkpeering/resource_network_peering_test.go index 53ea0c4752..9c675f02a6 100644 --- a/internal/service/networkpeering/resource_network_peering_test.go +++ b/internal/service/networkpeering/resource_network_peering_test.go @@ -302,20 +302,20 @@ func configAWS(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlo func configAzure(projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName string) string { return fmt.Sprintf(` resource "mongodbatlas_network_container" "test" { - project_id = "%[1]s" + project_id = %[1]q atlas_cidr_block = "192.168.208.0/21" - provider_name = "%[2]s" + provider_name = %[2]q region = "US_EAST_2" } resource "mongodbatlas_network_peering" "test" { - project_id = "%[1]s" + project_id = %[1]q container_id = mongodbatlas_network_container.test.container_id - provider_name = "%[2]s" - azure_directory_id = "%[3]s" - azure_subscription_id = "%[4]s" - resource_group_name = "%[5]s" - vnet_name = "%[6]s" + provider_name = %[2]q + azure_directory_id = %[3]q + azure_subscription_id = %[4]q + resource_group_name = %[5]q + vnet_name = %[6]q } `, projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName) } @@ -323,17 +323,17 @@ func configAzure(projectID, providerName, directoryID, subscriptionID, resourceG func configGCP(projectID, providerName, gcpProjectID, networkName string) string { return fmt.Sprintf(` resource "mongodbatlas_network_container" "test" { - project_id = "%[1]s" + project_id = %[1]q atlas_cidr_block = "192.168.192.0/18" - provider_name = "%[2]s" + provider_name = %[2]q } resource "mongodbatlas_network_peering" "test" { - project_id = "%[1]s" + project_id = %[1]q container_id = mongodbatlas_network_container.test.container_id - provider_name = "%[2]s" - gcp_project_id = "%[3]s" - network_name = "%[4]s" + provider_name = %[2]q + gcp_project_id = %[3]q + network_name = %[4]q } `, projectID, providerName, gcpProjectID, networkName) } From 728bb6d581d9afc6f5be31ae79bce9e5fcc16005 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 24 May 2024 11:07:31 +0200 Subject: [PATCH 7/9] PreCheckBasic --- .../service/networkpeering/resource_network_peering_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/networkpeering/resource_network_peering_test.go b/internal/service/networkpeering/resource_network_peering_test.go index 9c675f02a6..4d5d3cbc7e 100644 --- a/internal/service/networkpeering/resource_network_peering_test.go +++ b/internal/service/networkpeering/resource_network_peering_test.go @@ -36,7 +36,7 @@ func TestAccNetworkRSNetworkPeering_basicAzure(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { acc.PreCheck(t); acc.PreCheckPeeringEnvAzure(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckPeeringEnvAzure(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyNetworkPeering, Steps: []resource.TestStep{ @@ -74,7 +74,7 @@ func TestAccNetworkRSNetworkPeering_updateBasicAzure(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { acc.PreCheck(t); acc.PreCheckPeeringEnvAzure(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckPeeringEnvAzure(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyNetworkPeering, Steps: []resource.TestStep{ From 83c50d67d79ffc7daa81df86cb1e915b149faf79 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 24 May 2024 12:16:59 +0200 Subject: [PATCH 8/9] remove azure from update --- .../service/networkpeering/resource_network_peering.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/internal/service/networkpeering/resource_network_peering.go b/internal/service/networkpeering/resource_network_peering.go index cb2a636240..f2612aabff 100644 --- a/internal/service/networkpeering/resource_network_peering.go +++ b/internal/service/networkpeering/resource_network_peering.go @@ -410,15 +410,12 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. ContainerId: conversion.GetEncodedID(d.Get("container_id").(string), "container_id"), } + // Updating any of the attributes for Azure Network Peering forces a recreation of the network peering. + // Need to check if GCP and AWS have the same behavior switch peer.GetProviderName() { case "GCP": peer.SetGcpProjectId(d.Get("gcp_project_id").(string)) peer.SetNetworkName(d.Get("network_name").(string)) - case "AZURE": - peer.SetAzureDirectoryId(d.Get("azure_directory_id").(string)) - peer.SetAzureSubscriptionId(d.Get("azure_subscription_id").(string)) - peer.SetResourceGroupName(d.Get("resource_group_name").(string)) - peer.SetVnetName(d.Get("vnet_name").(string)) default: // AWS by default region, _ := conversion.ValRegion(d.Get("accepter_region_name"), "network_peering") peer.SetAccepterRegionName(region) From efabd9b397dd0fc8f05b6e65a0750812f31911ad Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 24 May 2024 15:16:07 +0200 Subject: [PATCH 9/9] mention force new in changelog --- .changelog/2299.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/2299.txt b/.changelog/2299.txt index f9c501fcf8..9146464a50 100644 --- a/.changelog/2299.txt +++ b/.changelog/2299.txt @@ -1,3 +1,3 @@ ```release-note:bug -resource/mongodbatlas_network_peering: Sets all necessary attributes for update of Azure network peering +resource/mongodbatlas_network_peering: Sets all attributes of Azure network peering as ForceNew, forcing recreation of the resource when updating ```