Azure-Samples / virtual-machines-python-manage

An example illustrating how to use Python to manage your Azure Virtual Machines
MIT License
104 stars 84 forks source link

Detach Data Disk on line 155 in examples.py is not doing anything #13

Closed nkv16786 closed 6 years ago

nkv16786 commented 6 years ago

Detach data disk

    print('\nDetach Data Disk')
    data_disks = virtual_machine.storage_profile.data_disks
    data_disks[:] = [disk for disk in data_disks if disk.name != 'mydatadisk1']
    async_vm_update = compute_client.virtual_machines.create_or_update(
        GROUP_NAME,
        VM_NAME,
        virtual_machine
    )
    virtual_machine = async_vm_update.result()
lmazuel commented 6 years ago

Yes it does detach the disk. Why do you think it doesn't?

nkv16786 commented 6 years ago

vm.storage_profile.data_disks.remove()

We have to use this along with your code to remove Please correct me if i am wrong

lmazuel commented 6 years ago

No, this line is doing it: data_disks[:] = [disk for disk in data_disks if disk.name != 'mydatadisk1']

This means:

If you want to use remove, you will have to find the element before:

for disk in vm.storage_profile.data_disks:
    if disk.name == 'mydatadisk1':
        break
else:
    disk = None
if disk:
    vm.storage_profile.data_disks.remove(disk)

That's longer than the one line version. And don't try to remove while iterating on the list :)

nkv16786 commented 6 years ago

Ok thanks for clarifying :)

lmazuel commented 6 years ago

No problem :)