Detach disk from virtual machine using the new Azure Resource Manager API

66 Views Asked by At

I struggle to port my automated Azure VM management from the old .NET fluent API to the new ARM API (the one in the Azure.ResourceManager.* namespaces).

The most important point is detaching a disk from a VM.

The VirtualMachineResource has only one UpdateAsync method, and that is taking a VirtualMachinePatch. I could successfully attach a disk that was previously unattached by using this patch object:

        var vmPatch = new VirtualMachinePatch
        {
            StorageProfile = new VirtualMachineStorageProfile
            {
                DataDisks =
                {
                    new VirtualMachineDataDisk(0, DiskCreateOptionType.Attach)
                    {
                        ManagedDisk = new VirtualMachineManagedDisk
                        {
                            Id = disc.Id
                        },
                        DeleteOption = DiskDeleteOptionType.Detach
                    }
                }
            }
        };

It's not obvious how detaching is supposed to work though, and the documentation is sparse. Simply leaving the DataDisks property empty does nothing, as one would expect.

The old fluent API had specialized methods, allowing to write something like

    vm.Update().WithoutDataDisk(entry.Lun);

The new one, is far less obvious to use. Any ideas?

1

There are 1 best solutions below

0
John On

I found the correct way of doing this by looking at the .NET SDK test suite:

        var vmPatch = new VirtualMachinePatch
        {
            StorageProfile = new VirtualMachineStorageProfile
            {
                DataDisks =
                {
                    new VirtualMachineDataDisk(0, DiskCreateOptionType.Attach)
                    {
                        ToBeDetached = true
                    }
                }
            }
        };

A bit obvious in hindsight, but I missed it several time.