Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/azurerm: add option to delete VMs Data disks on termination #7793

Merged
merged 1 commit into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions builtin/providers/azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ func resourceArmVirtualMachine() *schema.Resource {
},
},

"delete_data_disks_on_termination": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"os_profile": {
Type: schema.TypeSet,
Required: true,
Expand Down Expand Up @@ -561,19 +567,43 @@ func resourceArmVirtualMachineDelete(d *schema.ResourceData, meta interface{}) e
return err
}

if deleteOsDisk := d.Get("delete_os_disk_on_termination").(bool); !deleteOsDisk {
log.Printf("[INFO] delete_os_disk_on_termination is false, skipping delete")
return nil
// delete OS Disk if opted in
if deleteOsDisk := d.Get("delete_os_disk_on_termination").(bool); deleteOsDisk {
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)
if err != nil {
return fmt.Errorf("Error expanding OS Disk")
// delete Data disks if opted in
if deleteDataDisks := d.Get("delete_data_disks_on_termination").(bool); deleteDataDisks {
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 {
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
Expand All @@ -582,12 +612,12 @@ func resourceArmVirtualMachineDelete(d *schema.ResourceData, meta interface{}) e
containerName := path[0]
blobName := path[1]

blobClient, storageAccountExists, err := meta.(*ArmClient).getBlobStorageClientForStorageAccount(id.ResourceGroup, storageAccountName)
blobClient, saExists, err := meta.(*ArmClient).getBlobStorageClientForStorageAccount(resGroup, storageAccountName)
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)
return nil
}
Expand Down
36 changes: 27 additions & 9 deletions builtin/providers/azurerm/resource_arm_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestAccAzureRMVirtualMachine_winRMConfig(t *testing.T) {
func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) {
var vm compute.VirtualMachine
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)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -196,7 +196,10 @@ func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) {
},
{
Config: postConfig,
Check: testCheckAzureRMVirtualMachineOSDiskVHDExistance(true),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineVHDExistance("myosdisk1.vhd", true),
testCheckAzureRMVirtualMachineVHDExistance("mydatadisk1.vhd", true),
),
},
},
})
Expand All @@ -205,7 +208,7 @@ func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) {
func TestAccAzureRMVirtualMachine_deleteVHDOptIn(t *testing.T) {
var vm compute.VirtualMachine
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)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -220,7 +223,10 @@ func TestAccAzureRMVirtualMachine_deleteVHDOptIn(t *testing.T) {
},
{
Config: postConfig,
Check: testCheckAzureRMVirtualMachineOSDiskVHDExistance(false),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineVHDExistance("myosdisk1.vhd", false),
testCheckAzureRMVirtualMachineVHDExistance("mydatadisk1.vhd", false),
),
},
},
})
Expand Down Expand Up @@ -322,7 +328,7 @@ func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error {
return nil
}

func testCheckAzureRMVirtualMachineOSDiskVHDExistance(shouldExist bool) resource.TestCheckFunc {
func testCheckAzureRMVirtualMachineVHDExistance(name string, shouldExist bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_storage_container" {
Expand All @@ -338,13 +344,15 @@ func testCheckAzureRMVirtualMachineOSDiskVHDExistance(shouldExist bool) resource
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 {
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 {
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")
}
}

Expand Down Expand Up @@ -529,7 +537,7 @@ resource "azurerm_virtual_machine" "test" {
}
`

var testAccAzureRMVirtualMachine_basicLinuxMachineDestroyOSDisk = `
var testAccAzureRMVirtualMachine_basicLinuxMachineDestroyDisks = `
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "West US"
Expand Down Expand Up @@ -602,6 +610,16 @@ resource "azurerm_virtual_machine" "test" {

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 {
computer_name = "hostname%d"
admin_username = "testadmin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ The following arguments are supported:
* `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`
* `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_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.
Expand Down