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

Azure quickstart samples #6

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
166 changes: 162 additions & 4 deletions builtin/providers/azurerm/resource_arm_virtual_machine_scale_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,19 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource {
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Optional: true,
},

"image": {
Type: schema.TypeString,
Optional: true,
},

"managed_disk_type": {
Type: schema.TypeString,
Optional: true,
},

"vhd_containers": {
Type: schema.TypeSet,
Optional: true,
Expand All @@ -312,6 +317,52 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource {
Set: resourceArmVirtualMachineScaleSetStorageProfileOsDiskHash,
},

"storage_profile_data_disk": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},

"lun": {
Type: schema.TypeInt,
Required: true,
},

"create_option": {
Type: schema.TypeString,
Required: true,
},

"caching": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validateDiskSizeGB,
},

"managed_disk_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
string(compute.PremiumLRS),
string(compute.StandardLRS),
}, true),
},
},
},
},

"storage_profile_image_reference": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -419,6 +470,15 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf
return err
}
storageProfile.OsDisk = osDisk

if _, ok := d.GetOk("storage_profile_data_disk"); ok {
dataDisks, err := expandAzureRMVirtualMachineScaleSetsStorageProfileDataDisk(d)
if err != nil {
return err
}
storageProfile.DataDisks = &dataDisks
}

if _, ok := d.GetOk("storage_profile_image_reference"); ok {
imageRef, err := expandAzureRmVirtualMachineScaleSetStorageProfileImageReference(d)
if err != nil {
Expand Down Expand Up @@ -545,6 +605,12 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Scale Set Storage Profile OS Disk error: %#v", err)
}

if resp.VirtualMachineProfile.StorageProfile.DataDisks != nil {
if err := d.Set("storage_profile_data_disk", flattenAzureRmVirtualMachineScaleSetStorageProfileDataDisk(properties.VirtualMachineProfile.StorageProfile.DataDisks)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Scale Set Storage Profile Data Disk error: %#v", err)
}
}

if properties.VirtualMachineProfile.ExtensionProfile != nil {
extension, err := flattenAzureRmVirtualMachineScaleSetExtensionProfile(properties.VirtualMachineProfile.ExtensionProfile)
if err != nil {
Expand Down Expand Up @@ -725,7 +791,11 @@ func flattenAzureRMVirtualMachineScaleSetOsProfile(profile *compute.VirtualMachi

func flattenAzureRmVirtualMachineScaleSetStorageProfileOSDisk(profile *compute.VirtualMachineScaleSetOSDisk) []interface{} {
result := make(map[string]interface{})
result["name"] = *profile.Name
//Name is not required with Managed Disks
if profile.Name != nil {
result["name"] = *profile.Name
}

if profile.Image != nil {
result["image"] = *profile.Image.URI
}
Expand All @@ -745,6 +815,31 @@ func flattenAzureRmVirtualMachineScaleSetStorageProfileOSDisk(profile *compute.V
return []interface{}{result}
}

func flattenAzureRmVirtualMachineScaleSetStorageProfileDataDisk(disks *[]compute.VirtualMachineScaleSetDataDisk) interface{} {
result := make([]interface{}, len(*disks))
for i, disk := range *disks {
l := make(map[string]interface{})
//Name is optional with Managed Disks
if disk.Name != nil {
l["name"] = *disk.Name
}

if disk.ManagedDisk != nil {
l["managed_disk_type"] = string(disk.ManagedDisk.StorageAccountType)
}

l["create_option"] = disk.CreateOption
l["caching"] = string(disk.Caching)
if disk.DiskSizeGB != nil {
l["disk_size_gb"] = *disk.DiskSizeGB
}
l["lun"] = *disk.Lun

result[i] = l
}
return result
}

func flattenAzureRmVirtualMachineScaleSetStorageProfileImageReference(profile *compute.ImageReference) []interface{} {
result := make(map[string]interface{})
result["publisher"] = *profile.Publisher
Expand Down Expand Up @@ -1026,6 +1121,8 @@ func expandAzureRMVirtualMachineScaleSetsStorageProfileOsDisk(d *schema.Resource
caching := osDiskConfig["caching"].(string)
osType := osDiskConfig["os_type"].(string)
createOption := osDiskConfig["create_option"].(string)
managedDiskType := osDiskConfig["managed_disk_type"].(string)
containers := osDiskConfig["vhd_containers"].(*schema.Set).List()

osDisk := &compute.VirtualMachineScaleSetOSDisk{
Name: &name,
Expand All @@ -1034,24 +1131,85 @@ func expandAzureRMVirtualMachineScaleSetsStorageProfileOsDisk(d *schema.Resource
CreateOption: compute.DiskCreateOptionTypes(createOption),
}

managedDiskVMSS := &compute.VirtualMachineScaleSetManagedDiskParameters{}

if managedDiskType != "" {
managedDiskVMSS.StorageAccountType = compute.StorageAccountTypes(managedDiskType)
} else {
managedDiskVMSS.StorageAccountType = compute.StorageAccountTypes(compute.StandardLRS)
}

if len(containers) > 0 && managedDiskType != "" {
return nil, fmt.Errorf("[ERROR] Conflict between `vhd_containers` and `managed_disk_type` (only one or the other can be used)")
}

//assume managed disks only if neither "image" nor "vhd_contaners" properties are referenced
if image != "" {
osDisk.Image = &compute.VirtualHardDisk{
URI: &image,
}
} else {
} else if len(containers) > 0 {
var vhdContainers []string
containers := osDiskConfig["vhd_containers"].(*schema.Set).List()
for _, v := range containers {
str := v.(string)
vhdContainers = append(vhdContainers, str)
}
osDisk.VhdContainers = &vhdContainers
} else {
osDisk.ManagedDisk = managedDiskVMSS
//Name is not allowed with Managed Disks when used with VMSS
osDisk.Name = nil
}

return osDisk, nil

}

func expandAzureRMVirtualMachineScaleSetsStorageProfileDataDisk(d *schema.ResourceData) ([]compute.VirtualMachineScaleSetDataDisk, error) {
disks := d.Get("storage_profile_data_disk").([]interface{})
dataDisks := make([]compute.VirtualMachineScaleSetDataDisk, 0, len(disks))
for _, diskConfig := range disks {
config := diskConfig.(map[string]interface{})

name := config["name"].(string)
createOption := config["create_option"].(string)
managedDiskType := config["managed_disk_type"].(string)
lun := int32(config["lun"].(int))

dataDisk := compute.VirtualMachineScaleSetDataDisk{
Name: &name,
Lun: &lun,
CreateOption: compute.DiskCreateOptionTypes(createOption),
}

managedDiskVMSS := &compute.VirtualMachineScaleSetManagedDiskParameters{}

if managedDiskType != "" {
managedDiskVMSS.StorageAccountType = compute.StorageAccountTypes(managedDiskType)
} else {
managedDiskVMSS.StorageAccountType = compute.StorageAccountTypes(compute.StandardLRS)
}

//assume that data disks in VMSS can only be Managed Disks
dataDisk.ManagedDisk = managedDiskVMSS
//Name is not allowed with Managed Disks when used with VMSS (API Error)
dataDisk.Name = nil

if v := config["caching"].(string); v != "" {
dataDisk.Caching = compute.CachingTypes(v)
}

if v := config["disk_size_gb"]; v != nil {
diskSize := int32(config["disk_size_gb"].(int))
dataDisk.DiskSizeGB = &diskSize
}

dataDisks = append(dataDisks, dataDisk)
}

return dataDisks, nil
}

func expandAzureRmVirtualMachineScaleSetStorageProfileImageReference(d *schema.ResourceData) (*compute.ImageReference, error) {
storageImageRefs := d.Get("storage_profile_image_reference").(*schema.Set).List()

Expand Down
Loading