From ad1ad088358dda8414d9f751c660a9f45b8f98d7 Mon Sep 17 00:00:00 2001 From: Owen Farrell Date: Wed, 13 Oct 2021 15:41:16 -0400 Subject: [PATCH] `azurerm_subnet_resource` - Lock virtual network and subnet on update (#13726) This pull request locks the subnet resource and associated virtual network when updating subnets to prevent conflicting, concurrent operations. Fixes: #13564 --- internal/services/network/subnet_resource.go | 6 +++++ .../services/network/subnet_resource_test.go | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/internal/services/network/subnet_resource.go b/internal/services/network/subnet_resource.go index 87824e25f966..a3c2343bc88c 100644 --- a/internal/services/network/subnet_resource.go +++ b/internal/services/network/subnet_resource.go @@ -290,6 +290,12 @@ func resourceSubnetUpdate(d *pluginsdk.ResourceData, meta interface{}) error { return err } + locks.ByName(id.VirtualNetworkName, VirtualNetworkResourceName) + defer locks.UnlockByName(id.VirtualNetworkName, VirtualNetworkResourceName) + + locks.ByName(id.Name, SubnetResourceName) + defer locks.UnlockByName(id.Name, SubnetResourceName) + existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name, "") if err != nil { return fmt.Errorf("retrieving %s: %+v", *id, err) diff --git a/internal/services/network/subnet_resource_test.go b/internal/services/network/subnet_resource_test.go index 38c960bb3a54..0a55b22fc049 100644 --- a/internal/services/network/subnet_resource_test.go +++ b/internal/services/network/subnet_resource_test.go @@ -422,6 +422,13 @@ resource "azurerm_subnet" "test" { virtual_network_name = azurerm_virtual_network.test.name address_prefix = "10.0.2.0/24" } + +resource "azurerm_subnet" "test2" { + name = "internal2" + resource_group_name = azurerm_resource_group.test.name + virtual_network_name = azurerm_virtual_network.test.name + address_prefix = "10.0.3.0/24" +} `, r.template(data)) } @@ -571,6 +578,14 @@ resource "azurerm_subnet" "test" { address_prefix = "10.0.2.0/24" service_endpoints = ["Microsoft.Sql"] } + +resource "azurerm_subnet" "test2" { + name = "internal2" + resource_group_name = azurerm_resource_group.test.name + virtual_network_name = azurerm_virtual_network.test.name + address_prefix = "10.0.3.0/24" + service_endpoints = ["Microsoft.Sql"] +} `, r.template(data)) } @@ -585,6 +600,14 @@ resource "azurerm_subnet" "test" { address_prefix = "10.0.2.0/24" service_endpoints = ["Microsoft.Sql", "Microsoft.Storage"] } + +resource "azurerm_subnet" "test2" { + name = "internal2" + resource_group_name = azurerm_resource_group.test.name + virtual_network_name = azurerm_virtual_network.test.name + address_prefix = "10.0.3.0/24" + service_endpoints = ["Microsoft.Sql", "Microsoft.Storage"] +} `, r.template(data)) }