Skip to content

Commit

Permalink
New Resource: azurerm_virtual_machine_data_disk_attachment (#1207)
Browse files Browse the repository at this point in the history
* New Resource: `azurerm_virtual_machine_data_disk_attachment`

* Disclaimers about not using both resources at the same time

* Fixing an incorrect assignment

* Documentation fixes

* Fixing some broken tests

* Removing the `create_option` field

* Moving to support only managed disks

* Refactoring to switch back to how it was

* Explaining why we're deliberately ignoring an error

* Adding missing validation for the create_option

* Required -> Optional

* Fixing issues identified in PR Review
  • Loading branch information
tombuildsstuff authored Jun 27, 2018
1 parent ad765f4 commit 10af28f
Show file tree
Hide file tree
Showing 9 changed files with 979 additions and 125 deletions.
58 changes: 58 additions & 0 deletions azurerm/import_arm_virtual_machine_data_disk_attachment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMVirtualMachineDataDiskAttachment_importBasic(t *testing.T) {
resourceName := "azurerm_virtual_machine_data_disk_attachment.test"
ri := acctest.RandInt()
location := testLocation()
config := testAccAzureRMVirtualMachineDataDiskAttachment_basic(ri, location)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualMachineDataDiskAttachmentDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMVirtualMachineDataDiskAttachment_importMultipleDisks(t *testing.T) {
ri := acctest.RandInt()
location := testLocation()
config := testAccAzureRMVirtualMachineDataDiskAttachment_multipleDisks(ri, location)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualMachineDataDiskAttachmentDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: "azurerm_virtual_machine_data_disk_attachment.first",
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "azurerm_virtual_machine_data_disk_attachment.second",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
231 changes: 116 additions & 115 deletions azurerm/provider.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion azurerm/resource_arm_managed_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) erro
OsType: compute.OperatingSystemTypes(osType),
},
Sku: &compute.DiskSku{
Name: (skuName),
Name: skuName,
},
Tags: expandedTags,
Zones: zones,
Expand Down
27 changes: 21 additions & 6 deletions azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

var virtualMachineResourceName = "azurerm_virtual_machine"

func resourceArmVirtualMachine() *schema.Resource {
return &schema.Resource{
Create: resourceArmVirtualMachineCreate,
Expand Down Expand Up @@ -248,6 +250,7 @@ func resourceArmVirtualMachine() *schema.Resource {
"storage_data_disk": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -617,6 +620,9 @@ func resourceArmVirtualMachineCreate(d *schema.ResourceData, meta interface{}) e
vm.Plan = plan
}

azureRMLockByName(name, virtualMachineResourceName)
defer azureRMUnlockByName(name, virtualMachineResourceName)

future, err := client.CreateOrUpdate(ctx, resGroup, name, vm)
if err != nil {
return err
Expand Down Expand Up @@ -774,6 +780,9 @@ func resourceArmVirtualMachineDelete(d *schema.ResourceData, meta interface{}) e
resGroup := id.ResourceGroup
name := id.Path["virtualMachines"]

azureRMLockByName(name, virtualMachineResourceName)
defer azureRMUnlockByName(name, virtualMachineResourceName)

future, err := client.Delete(ctx, resGroup, name)
if err != nil {
return err
Expand Down Expand Up @@ -834,32 +843,38 @@ func resourceArmVirtualMachineDelete(d *schema.ResourceData, meta interface{}) e
}

func resourceArmVirtualMachineDeleteVhd(uri string, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
environment := armClient.environment

vhdURL, err := url.Parse(uri)
if err != nil {
return fmt.Errorf("Cannot parse Disk VHD URI: %s", err)
}

blobDomainSuffix := environment.StorageEndpointSuffix
if !strings.HasSuffix(strings.ToLower(vhdURL.Host), strings.ToLower(blobDomainSuffix)) {
return fmt.Errorf("Error: Disk VHD URI %q doesn't appear to be a Blob Storage URI (%q) - expected a suffix of %q)", uri, vhdURL.Host, blobDomainSuffix)
}

// VHD URI is in the form: https://storageAccountName.blob.core.windows.net/containerName/blobName
storageAccountName := strings.Split(vhdURL.Host, ".")[0]
path := strings.Split(strings.TrimPrefix(vhdURL.Path, "/"), "/")
containerName := path[0]
blobName := path[1]

storageAccountResourceGroupName, err := findStorageAccountResourceGroup(meta, storageAccountName)
resourceGroupName, err := findStorageAccountResourceGroup(meta, storageAccountName)
if err != nil {
return fmt.Errorf("Error finding resource group for storage account %s: %+v", storageAccountName, err)
}

armClient := meta.(*ArmClient)
ctx := armClient.StopContext

blobClient, saExists, err := armClient.getBlobStorageClientForStorageAccount(ctx, storageAccountResourceGroupName, storageAccountName)
blobClient, saExists, err := armClient.getBlobStorageClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return fmt.Errorf("Error creating blob store client for VHD deletion: %+v", err)
}

if !saExists {
log.Printf("[INFO] Storage Account %q in resource group %q doesn't exist so the VHD blob won't exist", storageAccountName, storageAccountResourceGroupName)
log.Printf("[INFO] Storage Account %q in resource group %q doesn't exist so the VHD blob won't exist", storageAccountName, resourceGroupName)
return nil
}

Expand Down
Loading

0 comments on commit 10af28f

Please sign in to comment.