provider/azurerm: add option to delete VMs Data disks on termination (#7793)
``` TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMVirtualMachine_deleteVHD -timeout 120m === RUN TestAccAzureRMVirtualMachine_deleteVHDOptOut --- PASS: TestAccAzureRMVirtualMachine_deleteVHDOptOut (621.84s) === RUN TestAccAzureRMVirtualMachine_deleteVHDOptIn --- PASS: TestAccAzureRMVirtualMachine_deleteVHDOptIn (623.95s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 1245.930s ```
This commit is contained in:
parent
31b8cde45c
commit
1c13cc994b
|
@ -206,6 +206,12 @@ func resourceArmVirtualMachine() *schema.Resource {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"delete_data_disks_on_termination": {
|
||||||
|
Type: schema.TypeBool,
|
||||||
|
Optional: true,
|
||||||
|
Default: false,
|
||||||
|
},
|
||||||
|
|
||||||
"os_profile": {
|
"os_profile": {
|
||||||
Type: schema.TypeSet,
|
Type: schema.TypeSet,
|
||||||
Required: true,
|
Required: true,
|
||||||
|
@ -561,19 +567,43 @@ func resourceArmVirtualMachineDelete(d *schema.ResourceData, meta interface{}) e
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if deleteOsDisk := d.Get("delete_os_disk_on_termination").(bool); !deleteOsDisk {
|
// delete OS Disk if opted in
|
||||||
log.Printf("[INFO] delete_os_disk_on_termination is false, skipping delete")
|
if deleteOsDisk := d.Get("delete_os_disk_on_termination").(bool); deleteOsDisk {
|
||||||
return nil
|
log.Printf("[INFO] delete_os_disk_on_termination is enabled, deleting")
|
||||||
|
|
||||||
|
osDisk, err := expandAzureRmVirtualMachineOsDisk(d)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error expanding OS Disk: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = resourceArmVirtualMachineDeleteVhd(*osDisk.Vhd.URI, resGroup, meta); err != nil {
|
||||||
|
return fmt.Errorf("Error deleting OS Disk VHD: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
osDisk, err := expandAzureRmVirtualMachineOsDisk(d)
|
// delete Data disks if opted in
|
||||||
if err != nil {
|
if deleteDataDisks := d.Get("delete_data_disks_on_termination").(bool); deleteDataDisks {
|
||||||
return fmt.Errorf("Error expanding OS Disk")
|
log.Printf("[INFO] delete_data_disks_on_termination is enabled, deleting each data disk")
|
||||||
|
|
||||||
|
disks, err := expandAzureRmVirtualMachineDataDisk(d)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error expanding Data Disks: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, disk := range disks {
|
||||||
|
if err = resourceArmVirtualMachineDeleteVhd(*disk.Vhd.URI, resGroup, meta); err != nil {
|
||||||
|
return fmt.Errorf("Error deleting Data Disk VHD: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vhdURL, err := url.Parse(*osDisk.Vhd.URI)
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceArmVirtualMachineDeleteVhd(uri, resGroup string, meta interface{}) error {
|
||||||
|
vhdURL, err := url.Parse(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Cannot parse OS Disk VHD URI: %s", err)
|
return fmt.Errorf("Cannot parse Disk VHD URI: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// VHD URI is in the form: https://storageAccountName.blob.core.windows.net/containerName/blobName
|
// VHD URI is in the form: https://storageAccountName.blob.core.windows.net/containerName/blobName
|
||||||
|
@ -582,12 +612,12 @@ func resourceArmVirtualMachineDelete(d *schema.ResourceData, meta interface{}) e
|
||||||
containerName := path[0]
|
containerName := path[0]
|
||||||
blobName := path[1]
|
blobName := path[1]
|
||||||
|
|
||||||
blobClient, storageAccountExists, err := meta.(*ArmClient).getBlobStorageClientForStorageAccount(id.ResourceGroup, storageAccountName)
|
blobClient, saExists, err := meta.(*ArmClient).getBlobStorageClientForStorageAccount(resGroup, storageAccountName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error creating blob store account for VHD deletion: %s", err)
|
return fmt.Errorf("Error creating blob store client for VHD deletion: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !storageAccountExists {
|
if !saExists {
|
||||||
log.Printf("[INFO] Storage Account %q doesn't exist so the VHD blob won't exist", storageAccountName)
|
log.Printf("[INFO] Storage Account %q doesn't exist so the VHD blob won't exist", storageAccountName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -181,7 +181,7 @@ func TestAccAzureRMVirtualMachine_winRMConfig(t *testing.T) {
|
||||||
func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) {
|
func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) {
|
||||||
var vm compute.VirtualMachine
|
var vm compute.VirtualMachine
|
||||||
ri := acctest.RandInt()
|
ri := acctest.RandInt()
|
||||||
preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri)
|
preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_withDataDisk, ri, ri, ri, ri, ri, ri, ri)
|
||||||
postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM, ri, ri, ri, ri, ri)
|
postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM, ri, ri, ri, ri, ri)
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
@ -196,7 +196,10 @@ func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Config: postConfig,
|
Config: postConfig,
|
||||||
Check: testCheckAzureRMVirtualMachineOSDiskVHDExistance(true),
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
testCheckAzureRMVirtualMachineVHDExistance("myosdisk1.vhd", true),
|
||||||
|
testCheckAzureRMVirtualMachineVHDExistance("mydatadisk1.vhd", true),
|
||||||
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -205,7 +208,7 @@ func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) {
|
||||||
func TestAccAzureRMVirtualMachine_deleteVHDOptIn(t *testing.T) {
|
func TestAccAzureRMVirtualMachine_deleteVHDOptIn(t *testing.T) {
|
||||||
var vm compute.VirtualMachine
|
var vm compute.VirtualMachine
|
||||||
ri := acctest.RandInt()
|
ri := acctest.RandInt()
|
||||||
preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDestroyOSDisk, ri, ri, ri, ri, ri, ri, ri)
|
preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDestroyDisks, ri, ri, ri, ri, ri, ri, ri)
|
||||||
postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM, ri, ri, ri, ri, ri)
|
postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM, ri, ri, ri, ri, ri)
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
@ -220,7 +223,10 @@ func TestAccAzureRMVirtualMachine_deleteVHDOptIn(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Config: postConfig,
|
Config: postConfig,
|
||||||
Check: testCheckAzureRMVirtualMachineOSDiskVHDExistance(false),
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
testCheckAzureRMVirtualMachineVHDExistance("myosdisk1.vhd", false),
|
||||||
|
testCheckAzureRMVirtualMachineVHDExistance("mydatadisk1.vhd", false),
|
||||||
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -322,7 +328,7 @@ func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func testCheckAzureRMVirtualMachineOSDiskVHDExistance(shouldExist bool) resource.TestCheckFunc {
|
func testCheckAzureRMVirtualMachineVHDExistance(name string, shouldExist bool) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
for _, rs := range s.RootModule().Resources {
|
for _, rs := range s.RootModule().Resources {
|
||||||
if rs.Type != "azurerm_storage_container" {
|
if rs.Type != "azurerm_storage_container" {
|
||||||
|
@ -338,13 +344,15 @@ func testCheckAzureRMVirtualMachineOSDiskVHDExistance(shouldExist bool) resource
|
||||||
return fmt.Errorf("Error creating Blob storage client: %s", err)
|
return fmt.Errorf("Error creating Blob storage client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
exists, err := storageClient.BlobExists(containerName, "myosdisk1.vhd")
|
exists, err := storageClient.BlobExists(containerName, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error checking if OS Disk VHD Blob exists: %s", err)
|
return fmt.Errorf("Error checking if Disk VHD Blob exists: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if exists && !shouldExist {
|
if exists && !shouldExist {
|
||||||
return fmt.Errorf("OS Disk VHD Blob still exists")
|
return fmt.Errorf("Disk VHD Blob still exists")
|
||||||
|
} else if !exists && shouldExist {
|
||||||
|
return fmt.Errorf("Disk VHD Blob should exist")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -529,7 +537,7 @@ resource "azurerm_virtual_machine" "test" {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
var testAccAzureRMVirtualMachine_basicLinuxMachineDestroyOSDisk = `
|
var testAccAzureRMVirtualMachine_basicLinuxMachineDestroyDisks = `
|
||||||
resource "azurerm_resource_group" "test" {
|
resource "azurerm_resource_group" "test" {
|
||||||
name = "acctestrg-%d"
|
name = "acctestrg-%d"
|
||||||
location = "West US"
|
location = "West US"
|
||||||
|
@ -602,6 +610,16 @@ resource "azurerm_virtual_machine" "test" {
|
||||||
|
|
||||||
delete_os_disk_on_termination = true
|
delete_os_disk_on_termination = true
|
||||||
|
|
||||||
|
storage_data_disk {
|
||||||
|
name = "mydatadisk1"
|
||||||
|
vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/mydatadisk1.vhd"
|
||||||
|
disk_size_gb = "1023"
|
||||||
|
create_option = "Empty"
|
||||||
|
lun = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
delete_data_disks_on_termination = true
|
||||||
|
|
||||||
os_profile {
|
os_profile {
|
||||||
computer_name = "hostname%d"
|
computer_name = "hostname%d"
|
||||||
admin_username = "testadmin"
|
admin_username = "testadmin"
|
||||||
|
|
|
@ -212,6 +212,7 @@ The following arguments are supported:
|
||||||
* `storage_os_disk` - (Required) A Storage OS Disk block as referenced below.
|
* `storage_os_disk` - (Required) A Storage OS Disk block as referenced below.
|
||||||
* `delete_os_disk_on_termination` - (Optional) Flag to enable deletion of the OS Disk VHD blob when the VM is deleted, defaults to `false`
|
* `delete_os_disk_on_termination` - (Optional) Flag to enable deletion of the OS Disk VHD blob when the VM is deleted, defaults to `false`
|
||||||
* `storage_data_disk` - (Optional) A list of Storage Data disk blocks as referenced below.
|
* `storage_data_disk` - (Optional) A list of Storage Data disk blocks as referenced below.
|
||||||
|
* `delete_data_disks_on_termination` - (Optional) Flag to enable deletion of Storage Disk VHD blobs when the VM is deleted, defaults to `false`
|
||||||
* `os_profile` - (Required) An OS Profile block as documented below.
|
* `os_profile` - (Required) An OS Profile block as documented below.
|
||||||
* `os_profile_windows_config` - (Required, when a windows machine) A Windows config block as documented below.
|
* `os_profile_windows_config` - (Required, when a windows machine) A Windows config block as documented below.
|
||||||
* `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below.
|
* `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below.
|
||||||
|
|
Loading…
Reference in New Issue