Skip to content

Commit

Permalink
azurerm_subnet_resource - Lock virtual network and subnet on update (
Browse files Browse the repository at this point in the history
…#13726)

This pull request locks the subnet resource and associated virtual network when updating subnets to prevent conflicting, concurrent operations.

Fixes: #13564
  • Loading branch information
owenfarrell authored Oct 13, 2021
1 parent 2de21c7 commit ad1ad08
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/services/network/subnet_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions internal/services/network/subnet_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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))
}

Expand All @@ -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))
}

Expand Down

0 comments on commit ad1ad08

Please sign in to comment.