azure-libraries-for-net: [BUG] Managed disk in unmanaged disks collection

In case that disk has filled in ‘Image.Uri’ property it is considered unmanaged although it is managed. This causes issues when listing disk via ‘vm.UnmanagedDataDisks’.

StackTrace: Object reference not set to an instance of an object. System.NullReferenceException at Microsoft.Azure.Management.Compute.Fluent.UnmanagedDataDiskImpl.VhdUri() at Microsoft.Azure.Management.Compute.Fluent.UnmanagedDataDiskImpl.Microsoft.Azure.Management.Compute.Fluent.IVirtualMachineUnmanagedDataDisk.get_VhdUri()

To Reproduce: Have managed disk with filled in ‘Image.Uri’ property, list unmanaged disks from vm and try to get VhdUri

Code snipped:

      public async Task ManagedDiskGetsListedAsUnmanagedExample(Microsoft.Azure.Management.Fluent.IAzure azure, CancellationToken ct)
       {
           var virtualMachines = await azure.VirtualMachines.ListAsync(true, ct);
           foreach (var vm in virtualMachines)
           {
               try
               {
                   foreach (var disk in vm.UnmanagedDataDisks.Values)
                   {
                       var uri = disk.VhdUri; // this throws null reference exception; the disk is managed
                   }
               }
               catch (Exception)
               {
                   bool allTrue; // it is true at any moment == all the conditions are true
                   allTrue = vm.Inner.StorageProfile.OsDisk.Vhd == null; // no vhd
                   allTrue = !string.IsNullOrEmpty(vm.Inner.StorageProfile.OsDisk.Image.Uri); // there is uri in Image
                   allTrue = !string.IsNullOrEmpty(vm.Inner.StorageProfile.OsDisk.ManagedDisk.Id); // there is this id and no vhd
               }
           }
       }

Expected behavior: Should be listed as managed disk

Setup:

  • OS: Win10
  • IDE : VS2019
  • Version: 1.35.0

Additional context: I was looking into library code and there is this function called IsManagedDiskEnabled

      public IReadOnlyDictionary<int, Microsoft.Azure.Management.Compute.Fluent.IVirtualMachineUnmanagedDataDisk> UnmanagedDataDisks()
       {
           Dictionary<int, IVirtualMachineUnmanagedDataDisk> dataDisks = new Dictionary<int, IVirtualMachineUnmanagedDataDisk>();
           if (!IsManagedDiskEnabled())
           {
               foreach (var dataDisk in this.unmanagedDataDisks)
               {
                   dataDisks.Add(dataDisk.Lun, dataDisk);
               }
           }
           return dataDisks;
       }

where there is IsOSDiskFromStoredImage if (IsOSDiskFromStoredImage(Inner.StorageProfile)) function. In my case this will result in managed disk to be considered unmanaged disk (causing problems described above). I Actually really don’t understand the code. From my perspective last line return Inner.StorageProfile.OsDisk.Vhd == null; should be enough. The logic seems to be unnecessary overcomplicated. Author probably knew better than me so it possible it has its reasons, but it would be nice to review (and maybe add some comments) that.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 27 (7 by maintainers)

Most upvoted comments

I am looking in to this and will provide an update shortly.