@@ -136,7 +136,7 @@ func GetVirtualMachineNicsContextE(ctx context.Context, vmName string, resGroupN
136136
137137// extractVMNics extracts the Network Interface names from a Virtual Machine object.
138138func extractVMNics (vm * armcompute.VirtualMachine ) ([]string , error ) {
139- if vm .Properties == nil || vm .Properties .NetworkProfile == nil {
139+ if vm == nil || vm .Properties == nil || vm .Properties .NetworkProfile == nil {
140140 return nil , nil
141141 }
142142
@@ -145,6 +145,10 @@ func extractVMNics(vm *armcompute.VirtualMachine) ([]string, error) {
145145 var nics []string
146146
147147 for _ , nic := range vmNICs {
148+ if nic == nil || nic .ID == nil {
149+ continue
150+ }
151+
148152 nicName , err := GetNameFromResourceIDE (* nic .ID )
149153 if err != nil {
150154 return nil , fmt .Errorf ("failed to parse NIC resource ID %q: %w" , * nic .ID , err )
@@ -198,11 +202,20 @@ func GetVirtualMachineManagedDisksContextE(ctx context.Context, vmName string, r
198202
199203// extractVMManagedDisks extracts the Managed Disk names from a Virtual Machine object.
200204func extractVMManagedDisks (vm * armcompute.VirtualMachine ) []string {
205+ if vm == nil || vm .Properties == nil || vm .Properties .StorageProfile == nil {
206+ return nil
207+ }
208+
201209 vmDisks := vm .Properties .StorageProfile .DataDisks
202210
203- diskNames := make ([]string , len (vmDisks ))
204- for i , v := range vmDisks {
205- diskNames [i ] = * v .Name
211+ diskNames := make ([]string , 0 , len (vmDisks ))
212+
213+ for _ , v := range vmDisks {
214+ if v == nil || v .Name == nil {
215+ continue
216+ }
217+
218+ diskNames = append (diskNames , * v .Name )
206219 }
207220
208221 return diskNames
@@ -250,6 +263,11 @@ func GetVirtualMachineOSDiskNameContextE(ctx context.Context, vmName string, res
250263
251264// extractVMOSDiskName extracts the OS Disk name from a Virtual Machine object.
252265func extractVMOSDiskName (vm * armcompute.VirtualMachine ) string {
266+ if vm == nil || vm .Properties == nil || vm .Properties .StorageProfile == nil ||
267+ vm .Properties .StorageProfile .OSDisk == nil || vm .Properties .StorageProfile .OSDisk .Name == nil {
268+ return ""
269+ }
270+
253271 return * vm .Properties .StorageProfile .OSDisk .Name
254272}
255273
@@ -295,7 +313,8 @@ func GetVirtualMachineAvailabilitySetIDContextE(ctx context.Context, vmName stri
295313
296314// extractVMAvailabilitySetID extracts the Availability Set ID from a Virtual Machine object.
297315func extractVMAvailabilitySetID (vm * armcompute.VirtualMachine ) (string , error ) {
298- if vm .Properties .AvailabilitySet == nil {
316+ if vm == nil || vm .Properties == nil || vm .Properties .AvailabilitySet == nil ||
317+ vm .Properties .AvailabilitySet .ID == nil {
299318 return "" , nil
300319 }
301320
@@ -358,10 +377,15 @@ func GetVirtualMachineImageContextE(ctx context.Context, vmName string, resGroup
358377// extractVMImage extracts the Image reference from a Virtual Machine object.
359378// For custom images where Publisher/Offer/SKU/Version may be nil, empty strings are returned.
360379func extractVMImage (vm * armcompute.VirtualMachine ) * VMImage {
361- ref := vm .Properties .StorageProfile .ImageReference
362-
363380 img := & VMImage {}
364381
382+ if vm == nil || vm .Properties == nil || vm .Properties .StorageProfile == nil ||
383+ vm .Properties .StorageProfile .ImageReference == nil {
384+ return img
385+ }
386+
387+ ref := vm .Properties .StorageProfile .ImageReference
388+
365389 if ref .Publisher != nil {
366390 img .Publisher = * ref .Publisher
367391 }
@@ -423,6 +447,11 @@ func GetSizeOfVirtualMachineContextE(ctx context.Context, vmName string, resGrou
423447
424448// extractVMSize extracts the VM size from a Virtual Machine object.
425449func extractVMSize (vm * armcompute.VirtualMachine ) armcompute.VirtualMachineSizeTypes {
450+ if vm == nil || vm .Properties == nil || vm .Properties .HardwareProfile == nil ||
451+ vm .Properties .HardwareProfile .VMSize == nil {
452+ return ""
453+ }
454+
426455 return * vm .Properties .HardwareProfile .VMSize
427456}
428457
@@ -470,11 +499,15 @@ func GetVirtualMachineTagsContextE(ctx context.Context, vmName string, resGroupN
470499func extractVMTags (vm * armcompute.VirtualMachine ) map [string ]string {
471500 tags := make (map [string ]string )
472501
473- if vm .Tags == nil {
502+ if vm == nil || vm .Tags == nil {
474503 return tags
475504 }
476505
477506 for k , v := range vm .Tags {
507+ if v == nil {
508+ continue
509+ }
510+
478511 tags [k ] = * v
479512 }
480513
@@ -544,6 +577,10 @@ func listVirtualMachineNames(ctx context.Context, client *armcompute.VirtualMach
544577 }
545578
546579 for _ , v := range page .Value {
580+ if v == nil || v .Name == nil {
581+ continue
582+ }
583+
547584 vmDetails = append (vmDetails , * v .Name )
548585 }
549586 }
@@ -615,6 +652,10 @@ func listVirtualMachineProperties(ctx context.Context, client *armcompute.Virtua
615652 }
616653
617654 for _ , v := range page .Value {
655+ if v == nil || v .Name == nil || v .Properties == nil {
656+ continue
657+ }
658+
618659 vmDetails [* v .Name ] = * v .Properties
619660 }
620661 }
@@ -633,6 +674,11 @@ type Instance struct {
633674
634675// GetVirtualMachineInstanceSize gets the size of the Virtual Machine.
635676func (vm * Instance ) GetVirtualMachineInstanceSize () armcompute.VirtualMachineSizeTypes {
677+ if vm == nil || vm .VirtualMachine == nil || vm .Properties == nil ||
678+ vm .Properties .HardwareProfile == nil || vm .Properties .HardwareProfile .VMSize == nil {
679+ return ""
680+ }
681+
636682 return * vm .Properties .HardwareProfile .VMSize
637683}
638684
0 commit comments