Skip to content

Commit

Permalink
VM: ensuring we nil-check items prior to accessing (#2365)
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff authored Dec 4, 2018
1 parent e73f477 commit 5496380
Showing 1 changed file with 123 additions and 84 deletions.
207 changes: 123 additions & 84 deletions azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,91 +721,90 @@ func resourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) err
d.Set("location", azureRMNormalizeLocation(*location))
}

if resp.Plan != nil {
if err := d.Set("plan", flattenAzureRmVirtualMachinePlan(resp.Plan)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Plan error: %#v", err)
}
if err := d.Set("plan", flattenAzureRmVirtualMachinePlan(resp.Plan)); err != nil {
return fmt.Errorf("Error setting `plan`: %#v", err)
}

d.Set("identity", flattenAzureRmVirtualMachineIdentity(resp.Identity))

if resp.VirtualMachineProperties.AvailabilitySet != nil {
d.Set("availability_set_id", strings.ToLower(*resp.VirtualMachineProperties.AvailabilitySet.ID))
if err := d.Set("identity", flattenAzureRmVirtualMachineIdentity(resp.Identity)); err != nil {
return fmt.Errorf("Error setting `identity`: %+v", err)
}

d.Set("vm_size", resp.VirtualMachineProperties.HardwareProfile.VMSize)

if resp.VirtualMachineProperties.StorageProfile.ImageReference != nil {
if err := d.Set("storage_image_reference", schema.NewSet(resourceArmVirtualMachineStorageImageReferenceHash, flattenAzureRmVirtualMachineImageReference(resp.VirtualMachineProperties.StorageProfile.ImageReference))); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage Image Reference error: %#v", err)
if props := resp.VirtualMachineProperties; props != nil {
if availabilitySet := props.AvailabilitySet; availabilitySet != nil {
// TODO: why is this being lower-cased?
d.Set("availability_set_id", strings.ToLower(*availabilitySet.ID))
}
}

if osDisk := resp.VirtualMachineProperties.StorageProfile.OsDisk; osDisk != nil {
diskInfo, err := resourceArmVirtualMachineGetManagedDiskInfo(osDisk.ManagedDisk, meta)
if err != nil {
return fmt.Errorf("[DEBUG] Error getting managed OS disk detailed information: %#v", err)
}
if err := d.Set("storage_os_disk", flattenAzureRmVirtualMachineOsDisk(osDisk, diskInfo)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage OS Disk error: %#v", err)
if profile := props.HardwareProfile; profile != nil {
d.Set("vm_size", profile.VMSize)
}
}

if dataDisks := resp.VirtualMachineProperties.StorageProfile.DataDisks; dataDisks != nil {
disksInfo := make([]*compute.Disk, len(*dataDisks))
for i, dataDisk := range *dataDisks {
diskInfo, err := resourceArmVirtualMachineGetManagedDiskInfo(dataDisk.ManagedDisk, meta)
if err != nil {
return fmt.Errorf("[DEBUG] Error getting managed data disk detailed information: %#v", err)
if profile := props.StorageProfile; profile != nil {
if err := d.Set("storage_image_reference", schema.NewSet(resourceArmVirtualMachineStorageImageReferenceHash, flattenAzureRmVirtualMachineImageReference(profile.ImageReference))); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage Image Reference error: %#v", err)
}
disksInfo[i] = diskInfo
}
if err := d.Set("storage_data_disk", flattenAzureRmVirtualMachineDataDisk(dataDisks, disksInfo)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage Data Disks error: %#v", err)
}
}

if resp.VirtualMachineProperties.OsProfile != nil {
if err := d.Set("os_profile", schema.NewSet(resourceArmVirtualMachineStorageOsProfileHash, flattenAzureRmVirtualMachineOsProfile(resp.VirtualMachineProperties.OsProfile))); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage OS Profile: %#v", err)
}
if osDisk := profile.OsDisk; osDisk != nil {
diskInfo, err := resourceArmVirtualMachineGetManagedDiskInfo(osDisk.ManagedDisk, meta)
if err != nil {
return fmt.Errorf("Error flattening `storage_os_disk`: %#v", err)
}
if err := d.Set("storage_os_disk", flattenAzureRmVirtualMachineOsDisk(osDisk, diskInfo)); err != nil {
return fmt.Errorf("Error setting `storage_os_disk`: %#v", err)
}
}

if resp.VirtualMachineProperties.OsProfile.WindowsConfiguration != nil {
if err := d.Set("os_profile_windows_config", schema.NewSet(resourceArmVirtualMachineStorageOsProfileWindowsConfigHash, flattenAzureRmVirtualMachineOsProfileWindowsConfiguration(resp.VirtualMachineProperties.OsProfile.WindowsConfiguration))); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage OS Profile Windows Configuration: %#v", err)
if dataDisks := profile.DataDisks; dataDisks != nil {
disksInfo := make([]*compute.Disk, len(*dataDisks))
for i, dataDisk := range *dataDisks {
diskInfo, err := resourceArmVirtualMachineGetManagedDiskInfo(dataDisk.ManagedDisk, meta)
if err != nil {
return fmt.Errorf("[DEBUG] Error getting managed data disk detailed information: %#v", err)
}
disksInfo[i] = diskInfo
}
if err := d.Set("storage_data_disk", flattenAzureRmVirtualMachineDataDisk(dataDisks, disksInfo)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage Data Disks error: %#v", err)
}
}
}

if resp.VirtualMachineProperties.OsProfile.LinuxConfiguration != nil {
if err := d.Set("os_profile_linux_config", schema.NewSet(resourceArmVirtualMachineStorageOsProfileLinuxConfigHash, flattenAzureRmVirtualMachineOsProfileLinuxConfiguration(resp.VirtualMachineProperties.OsProfile.LinuxConfiguration))); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage OS Profile Linux Configuration: %#v", err)
if profile := props.OsProfile; profile != nil {
if err := d.Set("os_profile", schema.NewSet(resourceArmVirtualMachineStorageOsProfileHash, flattenAzureRmVirtualMachineOsProfile(profile))); err != nil {
return fmt.Errorf("Error setting `os_profile`: %#v", err)
}
}

if resp.VirtualMachineProperties.OsProfile.Secrets != nil {
if err := d.Set("os_profile_secrets", flattenAzureRmVirtualMachineOsProfileSecrets(resp.VirtualMachineProperties.OsProfile.Secrets)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage OS Profile Secrets: %#v", err)
if err := d.Set("os_profile_linux_config", schema.NewSet(resourceArmVirtualMachineStorageOsProfileLinuxConfigHash, flattenAzureRmVirtualMachineOsProfileLinuxConfiguration(profile.LinuxConfiguration))); err != nil {
return fmt.Errorf("Error setting `os_profile_linux_config`: %+v", err)
}

if err := d.Set("os_profile_windows_config", schema.NewSet(resourceArmVirtualMachineStorageOsProfileWindowsConfigHash, flattenAzureRmVirtualMachineOsProfileWindowsConfiguration(profile.WindowsConfiguration))); err != nil {
return fmt.Errorf("Error setting `os_profile_windows_config`: %+v", err)
}
}
}

if resp.VirtualMachineProperties.DiagnosticsProfile != nil && resp.VirtualMachineProperties.DiagnosticsProfile.BootDiagnostics != nil {
if err := d.Set("boot_diagnostics", flattenAzureRmVirtualMachineDiagnosticsProfile(resp.VirtualMachineProperties.DiagnosticsProfile.BootDiagnostics)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Diagnostics Profile: %#v", err)
if err := d.Set("os_profile_secrets", flattenAzureRmVirtualMachineOsProfileSecrets(profile.Secrets)); err != nil {
return fmt.Errorf("Error setting `os_profile_secrets`: %+v", err)
}
}
}

if resp.VirtualMachineProperties.NetworkProfile != nil {
if err := d.Set("network_interface_ids", flattenAzureRmVirtualMachineNetworkInterfaces(resp.VirtualMachineProperties.NetworkProfile)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage Network Interfaces: %#v", err)
if profile := props.DiagnosticsProfile; profile != nil {
if err := d.Set("boot_diagnostics", flattenAzureRmVirtualMachineDiagnosticsProfile(profile.BootDiagnostics)); err != nil {
return fmt.Errorf("Error setting `boot_diagnostics`: %#v", err)
}
}

if resp.VirtualMachineProperties.NetworkProfile.NetworkInterfaces != nil {
for _, nic := range *resp.VirtualMachineProperties.NetworkProfile.NetworkInterfaces {
if props := nic.NetworkInterfaceReferenceProperties; props != nil {
if props.Primary != nil && *props.Primary {
d.Set("primary_network_interface_id", nic.ID)
break
if profile := props.NetworkProfile; profile != nil {
if err := d.Set("network_interface_ids", flattenAzureRmVirtualMachineNetworkInterfaces(profile)); err != nil {
return fmt.Errorf("Error flattening `network_interface_ids`: %#v", err)
}

if profile.NetworkInterfaces != nil {
for _, nic := range *profile.NetworkInterfaces {
if props := nic.NetworkInterfaceReferenceProperties; props != nil {
if props.Primary != nil && *props.Primary {
d.Set("primary_network_interface_id", nic.ID)
break
}
}
}
}
Expand Down Expand Up @@ -967,15 +966,30 @@ func resourceArmVirtualMachineDeleteManagedDisk(managedDiskID string, meta inter
}

func flattenAzureRmVirtualMachinePlan(plan *compute.Plan) []interface{} {
if plan == nil {
return []interface{}{}
}

result := make(map[string]interface{})
result["name"] = *plan.Name
result["publisher"] = *plan.Publisher
result["product"] = *plan.Product

if plan.Name != nil {
result["name"] = *plan.Name
}
if plan.Publisher != nil {
result["publisher"] = *plan.Publisher
}
if plan.Product != nil {
result["product"] = *plan.Product
}

return []interface{}{result}
}

func flattenAzureRmVirtualMachineImageReference(image *compute.ImageReference) []interface{} {
if image == nil {
return []interface{}{}
}

result := make(map[string]interface{})
if image.Publisher != nil {
result["publisher"] = *image.Publisher
Expand Down Expand Up @@ -1027,9 +1041,16 @@ func flattenAzureRmVirtualMachineIdentity(identity *compute.VirtualMachineIdenti
}

func flattenAzureRmVirtualMachineDiagnosticsProfile(profile *compute.BootDiagnostics) []interface{} {
if profile == nil {
return []interface{}{}
}

result := make(map[string]interface{})

result["enabled"] = *profile.Enabled
if profile.Enabled != nil {
result["enabled"] = *profile.Enabled
}

if profile.StorageURI != nil {
result["storage_uri"] = *profile.StorageURI
}
Expand All @@ -1046,6 +1067,10 @@ func flattenAzureRmVirtualMachineNetworkInterfaces(profile *compute.NetworkProfi
}

func flattenAzureRmVirtualMachineOsProfileSecrets(secrets *[]compute.VaultSecretGroup) []interface{} {
if secrets == nil {
return []interface{}{}
}

result := make([]interface{}, 0)
for _, secret := range *secrets {
s := map[string]interface{}{
Expand Down Expand Up @@ -1117,6 +1142,10 @@ func flattenAzureRmVirtualMachineOsProfile(input *compute.OSProfile) []interface
}

func flattenAzureRmVirtualMachineOsProfileWindowsConfiguration(config *compute.WindowsConfiguration) []interface{} {
if config == nil {
return []interface{}{}
}

result := make(map[string]interface{})

if config.ProvisionVMAgent != nil {
Expand All @@ -1131,53 +1160,61 @@ func flattenAzureRmVirtualMachineOsProfileWindowsConfiguration(config *compute.W
result["timezone"] = *config.TimeZone
}

if config.WinRM != nil {
listeners := make([]map[string]interface{}, 0, len(*config.WinRM.Listeners))
listeners := make([]map[string]interface{}, 0)
if config.WinRM != nil && config.WinRM.Listeners != nil {
for _, i := range *config.WinRM.Listeners {
listener := make(map[string]interface{})
listener["protocol"] = i.Protocol
listener["protocol"] = string(i.Protocol)

if i.CertificateURL != nil {
listener["certificate_url"] = *i.CertificateURL
}

listeners = append(listeners, listener)
}

result["winrm"] = listeners
}

result["winrm"] = listeners

content := make([]map[string]interface{}, 0)
if config.AdditionalUnattendContent != nil {
content := make([]map[string]interface{}, 0, len(*config.AdditionalUnattendContent))
for _, i := range *config.AdditionalUnattendContent {
c := make(map[string]interface{})
c["pass"] = i.PassName
c["component"] = i.ComponentName
c["setting_name"] = i.SettingName
c["pass"] = string(i.PassName)
c["component"] = string(i.ComponentName)
c["setting_name"] = string(i.SettingName)

if i.Content != nil {
c["content"] = *i.Content
}

content = append(content, c)
}

result["additional_unattend_config"] = content
}
result["additional_unattend_config"] = content

return []interface{}{result}
}

func flattenAzureRmVirtualMachineOsProfileLinuxConfiguration(config *compute.LinuxConfiguration) []interface{} {
if config == nil {
return []interface{}{}
}

result := make(map[string]interface{})
result["disable_password_authentication"] = *config.DisablePasswordAuthentication

if config.SSH != nil && len(*config.SSH.PublicKeys) > 0 {
ssh_keys := make([]map[string]interface{}, 0, len(*config.SSH.PublicKeys))
if config.DisablePasswordAuthentication != nil {
result["disable_password_authentication"] = *config.DisablePasswordAuthentication
}

if config.SSH != nil && config.SSH.PublicKeys != nil && len(*config.SSH.PublicKeys) > 0 {
ssh_keys := make([]map[string]interface{}, 0)
for _, i := range *config.SSH.PublicKeys {
key := make(map[string]interface{})
key["path"] = *i.Path

if i.Path != nil {
key["path"] = *i.Path
}

if i.KeyData != nil {
key["key_data"] = *i.KeyData
Expand All @@ -1194,8 +1231,10 @@ func flattenAzureRmVirtualMachineOsProfileLinuxConfiguration(config *compute.Lin

func flattenAzureRmVirtualMachineOsDisk(disk *compute.OSDisk, diskInfo *compute.Disk) []interface{} {
result := make(map[string]interface{})
result["name"] = *disk.Name
if disk.Vhd != nil {
if disk.Name != nil {
result["name"] = *disk.Name
}
if disk.Vhd != nil && disk.Vhd.URI != nil {
result["vhd_uri"] = *disk.Vhd.URI
}
if disk.Image != nil && disk.Image.URI != nil {
Expand Down

0 comments on commit 5496380

Please sign in to comment.