Skip to content

Commit 40ae4f0

Browse files
committed
azurerm_subnet - add delete_on_destroy to optionally skip Azure resource deletion on destroy
1 parent 8d70fcd commit 40ae4f0

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

internal/services/network/subnet_resource.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ func resourceSubnet() *pluginsdk.Resource {
276276
Optional: true,
277277
},
278278

279+
"delete_on_destroy": {
280+
Type: pluginsdk.TypeBool,
281+
Optional: true,
282+
Default: true,
283+
},
284+
279285
"private_endpoint_network_policies": {
280286
Type: pluginsdk.TypeString,
281287
Optional: true,
@@ -639,6 +645,11 @@ func resourceSubnetDelete(d *pluginsdk.ResourceData, meta interface{}) error {
639645
locks.ByName(id.SubnetName, SubnetResourceName)
640646
defer locks.UnlockByName(id.SubnetName, SubnetResourceName)
641647

648+
if !d.Get("delete_on_destroy").(bool) {
649+
log.Printf("[DEBUG] Skipping deletion for %s because `delete_on_destroy` is false", *id)
650+
return nil
651+
}
652+
642653
if err := client.DeleteThenPoll(ctx, *id); err != nil {
643654
return fmt.Errorf("deleting %s: %+v", *id, err)
644655
}

internal/services/network/subnet_resource_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,30 @@ func TestAccSubnet_defaultOutbound(t *testing.T) {
149149
})
150150
}
151151

152+
func TestAccSubnet_deleteOnDestroy(t *testing.T) {
153+
data := acceptance.BuildTestData(t, "azurerm_subnet", "internal")
154+
r := SubnetResource{}
155+
156+
data.ResourceTest(t, r, []acceptance.TestStep{
157+
{
158+
Config: r.deleteOnDestroy(data, false),
159+
Check: acceptance.ComposeTestCheckFunc(
160+
check.That(data.ResourceName).ExistsInAzure(r),
161+
check.That(data.ResourceName).Key("delete_on_destroy").HasValue("false"),
162+
),
163+
},
164+
data.ImportStep("delete_on_destroy"),
165+
{
166+
Config: r.deleteOnDestroy(data, true),
167+
Check: acceptance.ComposeTestCheckFunc(
168+
check.That(data.ResourceName).ExistsInAzure(r),
169+
check.That(data.ResourceName).Key("delete_on_destroy").HasValue("true"),
170+
),
171+
},
172+
data.ImportStep("delete_on_destroy"),
173+
})
174+
}
175+
152176
func TestAccSubnet_delegation(t *testing.T) {
153177
data := acceptance.BuildTestData(t, "azurerm_subnet", "test")
154178
r := SubnetResource{}
@@ -705,6 +729,19 @@ resource "azurerm_subnet" "internal" {
705729
`, r.template(data), enabled)
706730
}
707731

732+
func (r SubnetResource) deleteOnDestroy(data acceptance.TestData, deleteOnDestroy bool) string {
733+
return fmt.Sprintf(`
734+
%s
735+
resource "azurerm_subnet" "internal" {
736+
name = "internal"
737+
resource_group_name = azurerm_resource_group.test.name
738+
virtual_network_name = azurerm_virtual_network.test.name
739+
address_prefixes = ["10.0.2.0/24"]
740+
delete_on_destroy = %t
741+
}
742+
`, r.template(data), deleteOnDestroy)
743+
}
744+
708745
func (r SubnetResource) delegationUpdated(data acceptance.TestData) string {
709746
return fmt.Sprintf(`
710747
%s

website/docs/r/subnet.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ The following arguments are supported:
6565

6666
* `delegation` - (Optional) One or more `delegation` blocks as defined below.
6767

68+
* `delete_on_destroy` - (Optional) Should the Subnet be deleted in Azure when this resource is destroyed? Defaults to `true`. When set to `false`, Terraform will remove this resource from state without deleting the Subnet in Azure.
69+
6870
* `default_outbound_access_enabled` - (Optional) Enable default outbound access to the internet for the subnet. Defaults to `true`.
6971

7072
* `ip_address_pool` - (Optional) An `ip_address_pool` block as defined below.

0 commit comments

Comments
 (0)