Skip to content

Commit

Permalink
Merge branch 'brandontosch/hashicorpGH-11874' into vnext
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Rudduck committed Mar 3, 2017
2 parents bc78f5f + e3dbe47 commit f4162fd
Show file tree
Hide file tree
Showing 3 changed files with 598 additions and 117 deletions.
28 changes: 28 additions & 0 deletions builtin/providers/azurerm/import_arm_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,31 @@ func TestAccAzureRMVirtualMachine_importBasic(t *testing.T) {
},
})
}

func TestAccAzureRMVirtualMachine_importBasic_managedDisk(t *testing.T) {
resourceName := "azurerm_virtual_machine.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk, ri, ri, ri, ri, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"delete_data_disks_on_termination",
"delete_os_disk_on_termination",
},
},
},
})
}
110 changes: 96 additions & 14 deletions builtin/providers/azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
riviera "github.com/jen20/riviera/azure"
)

Expand Down Expand Up @@ -142,10 +143,27 @@ func resourceArmVirtualMachine() *schema.Resource {

"vhd_uri": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
},

"managed_disk": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"storage_account_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(compute.PremiumLRS),
string(compute.StandardLRS),
}, true),
},
},
},
},

"image_uri": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -190,7 +208,24 @@ func resourceArmVirtualMachine() *schema.Resource {

"vhd_uri": {
Type: schema.TypeString,
Required: true,
Optional: true,
},

"managed_disk": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"storage_account_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(compute.PremiumLRS),
string(compute.StandardLRS),
}, true),
},
},
},
},

"create_option": {
Expand Down Expand Up @@ -770,8 +805,15 @@ func resourceArmVirtualMachineStorageOsDiskHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["vhd_uri"].(string)))

if m["vhd_uri"] != nil {
buf.WriteString(fmt.Sprintf("%s-", m["vhd_uri"].(string)))
}
if m["managed_disk"] != nil {
managedDisk := m["managed_disk"].(map[string]interface{})
if managedDisk["storage_account_type"] != nil {
buf.WriteString(fmt.Sprintf("%s-", managedDisk["storage_account_type"].(string)))
}
}
return hashcode.String(buf.String())
}

Expand Down Expand Up @@ -869,7 +911,12 @@ func flattenAzureRmVirtualMachineDataDisk(disks *[]compute.DataDisk) interface{}
for i, disk := range *disks {
l := make(map[string]interface{})
l["name"] = *disk.Name
l["vhd_uri"] = *disk.Vhd.URI
if disk.Vhd != nil {
l["vhd_uri"] = *disk.Vhd.URI
}
if disk.ManagedDisk != nil {
l["managed_disk"] = flattenAzureRmVirtualMachineManagedDisk(disk.ManagedDisk)
}
l["create_option"] = disk.CreateOption
l["caching"] = string(disk.Caching)
if disk.DiskSizeGB != nil {
Expand Down Expand Up @@ -975,7 +1022,12 @@ func flattenAzureRmVirtualMachineOsProfileLinuxConfiguration(config *compute.Lin
func flattenAzureRmVirtualMachineOsDisk(disk *compute.OSDisk) []interface{} {
result := make(map[string]interface{})
result["name"] = *disk.Name
result["vhd_uri"] = *disk.Vhd.URI
if disk.Vhd != nil {
result["vhd_uri"] = *disk.Vhd.URI
}
if disk.ManagedDisk != nil {
result["managed_disk"] = flattenAzureRmVirtualMachineManagedDisk(disk.ManagedDisk)
}
result["create_option"] = disk.CreateOption
result["caching"] = disk.Caching
if disk.DiskSizeGB != nil {
Expand All @@ -985,6 +1037,12 @@ func flattenAzureRmVirtualMachineOsDisk(disk *compute.OSDisk) []interface{} {
return []interface{}{result}
}

func flattenAzureRmVirtualMachineManagedDisk(params *compute.ManagedDiskParameters) map[string]interface{} {
managedDisk := make(map[string]interface{})
managedDisk["storage_account_type"] = string(params.StorageAccountType)
return managedDisk
}

func expandAzureRmVirtualMachinePlan(d *schema.ResourceData) (*compute.Plan, error) {
planConfigs := d.Get("plan").(*schema.Set).List()

Expand Down Expand Up @@ -1208,17 +1266,26 @@ func expandAzureRmVirtualMachineDataDisk(d *schema.ResourceData) ([]compute.Data

name := config["name"].(string)
vhd := config["vhd_uri"].(string)
managedDisk := config["managed_disk"].(map[string]interface{})
createOption := config["create_option"].(string)
lun := int32(config["lun"].(int))

data_disk := compute.DataDisk{
Name: &name,
Vhd: &compute.VirtualHardDisk{
URI: &vhd,
},
Name: &name,
Lun: &lun,
CreateOption: compute.DiskCreateOptionTypes(createOption),
}
if vhd != "" && managedDisk != nil {
return nil, fmt.Errorf("[ERROR] Conflict between `vhd_uri` and `managed_disk` (only one or the other can be used)")
} else if vhd != "" {
data_disk.Vhd = &compute.VirtualHardDisk{
URI: &vhd,
}
} else if managedDisk != nil {
data_disk.ManagedDisk = expandAzureRmVirtualMachineManagedDisk(managedDisk)
} else {
return nil, fmt.Errorf("[ERROR] A value must be specified for either `vhd_uri` or `managed_disk`")
}

if v := config["caching"].(string); v != "" {
data_disk.Caching = compute.CachingTypes(v)
Expand Down Expand Up @@ -1299,17 +1366,25 @@ func expandAzureRmVirtualMachineOsDisk(d *schema.ResourceData) (*compute.OSDisk,

name := disk["name"].(string)
vhdURI := disk["vhd_uri"].(string)
managedDisk := disk["managed_disk"].(map[string]interface{})
imageURI := disk["image_uri"].(string)
createOption := disk["create_option"].(string)

osDisk := &compute.OSDisk{
Name: &name,
Vhd: &compute.VirtualHardDisk{
URI: &vhdURI,
},
Name: &name,
CreateOption: compute.DiskCreateOptionTypes(createOption),
}

if vhdURI != "" {
osDisk.Vhd = &compute.VirtualHardDisk{
URI: &vhdURI,
}
} else if managedDisk != nil {
osDisk.ManagedDisk = expandAzureRmVirtualMachineManagedDisk(managedDisk)
} else {
return nil, fmt.Errorf("[ERROR] must specify value for either vhd_uri or managed_disk")
}

if v := disk["image_uri"].(string); v != "" {
osDisk.Image = &compute.VirtualHardDisk{
URI: &imageURI,
Expand Down Expand Up @@ -1338,6 +1413,13 @@ func expandAzureRmVirtualMachineOsDisk(d *schema.ResourceData) (*compute.OSDisk,
return osDisk, nil
}

func expandAzureRmVirtualMachineManagedDisk(managedDisk map[string]interface{}) *compute.ManagedDiskParameters {
managedDiskParameters := &compute.ManagedDiskParameters{
StorageAccountType: compute.StorageAccountTypes(managedDisk["storage_account_type"].(string)),
}
return managedDiskParameters
}

func findStorageAccountResourceGroup(meta interface{}, storageAccountName string) (string, error) {
client := meta.(*ArmClient).resourceFindClient
filter := fmt.Sprintf("name eq '%s' and resourceType eq 'Microsoft.Storage/storageAccounts'", storageAccountName)
Expand Down
Loading

0 comments on commit f4162fd

Please sign in to comment.