From 5496380dc0eadc12fe2378d6c1c1282577af4149 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Tue, 4 Dec 2018 12:52:50 +0000 Subject: [PATCH] VM: ensuring we nil-check items prior to accessing (#2365) --- azurerm/resource_arm_virtual_machine.go | 207 ++++++++++++++---------- 1 file changed, 123 insertions(+), 84 deletions(-) diff --git a/azurerm/resource_arm_virtual_machine.go b/azurerm/resource_arm_virtual_machine.go index 815d14023220..d98de3a42ee3 100644 --- a/azurerm/resource_arm_virtual_machine.go +++ b/azurerm/resource_arm_virtual_machine.go @@ -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 + } } } } @@ -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 @@ -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 } @@ -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{}{ @@ -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 { @@ -1131,11 +1160,11 @@ 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 @@ -1143,17 +1172,17 @@ func flattenAzureRmVirtualMachineOsProfileWindowsConfiguration(config *compute.W 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 @@ -1161,23 +1190,31 @@ func flattenAzureRmVirtualMachineOsProfileWindowsConfiguration(config *compute.W 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 @@ -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 {