Skip to content

Commit c92c01c

Browse files
{Compute} az vm unmanaged-disk: Migrate command group to aaz-based implementation (#32754)
1 parent 561dc98 commit c92c01c

File tree

10 files changed

+5679
-5544
lines changed

10 files changed

+5679
-5544
lines changed

src/azure-cli/azure/cli/command_modules/vm/_vm_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,14 @@ class IdentityType(Enum):
782782
NONE = 'None'
783783

784784

785+
class DiskCreateOptionTypes(Enum):
786+
ATTACH = 'Attach'
787+
COPY = 'Copy'
788+
EMPTY = 'Empty'
789+
FROM_IMAGE = 'FromImage'
790+
RESTORE = 'Restore'
791+
792+
785793
class UpgradeMode(Enum):
786794
AUTOMATIC = 'Automatic'
787795
MANUAL = 'Manual'

src/azure-cli/azure/cli/command_modules/vm/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def load_command_table(self, _):
374374
g.custom_command('list', 'list_vm_secrets')
375375
g.custom_command('remove', 'remove_vm_secret')
376376

377-
with self.command_group('vm unmanaged-disk', compute_vm_sdk) as g:
377+
with self.command_group('vm unmanaged-disk') as g:
378378
g.custom_command('attach', 'attach_unmanaged_data_disk')
379379
g.custom_command('detach', 'detach_unmanaged_data_disk')
380380
g.custom_command('list', 'list_unmanaged_disks')

src/azure-cli/azure/cli/command_modules/vm/custom.py

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ def _get_disk_lun(data_disks):
188188
return len(existing_luns)
189189

190190

191+
def _get_disk_lun_by_aaz(data_disks):
192+
# start from 0, search for unused int for lun
193+
if not data_disks:
194+
return 0
195+
196+
existing_luns = sorted([d['lun'] for d in data_disks])
197+
for i, current in enumerate(existing_luns):
198+
if current != i:
199+
return i
200+
return len(existing_luns)
201+
202+
191203
def _get_private_config(cli_ctx, resource_group_name, storage_account):
192204
storage_mgmt_client = _get_storage_management_client(cli_ctx)
193205
# pylint: disable=no-member
@@ -2433,14 +2445,18 @@ def attach_managed_data_disk(cmd, resource_group_name, vm_name, disk=None, ids=N
24332445

24342446

24352447
def detach_unmanaged_data_disk(cmd, resource_group_name, vm_name, disk_name):
2448+
from .operations.vm import convert_show_result_to_snake_case
24362449
# here we handle unmanaged disk
2437-
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
2438-
# pylint: disable=no-member
2439-
leftovers = [d for d in vm.storage_profile.data_disks if d.name.lower() != disk_name.lower()]
2440-
if len(vm.storage_profile.data_disks) == len(leftovers):
2450+
vm = get_vm_to_update_by_aaz(cmd, resource_group_name, vm_name)
2451+
vm = convert_show_result_to_snake_case(vm)
2452+
leftovers = [d for d in vm.get('storage_profile', {}).get('data_disks', []) if
2453+
d.get('name', '').lower() != disk_name.lower()]
2454+
if len(vm.get('storage_profile', {}).get('data_disks', [])) == len(leftovers):
24412455
raise CLIError("No disk with the name '{}' was found".format(disk_name))
2442-
vm.storage_profile.data_disks = leftovers
2443-
set_vm(cmd, vm)
2456+
2457+
vm['storage_profile']['data_disks'] = leftovers
2458+
2459+
set_vm_by_aaz(cmd, vm)
24442460
# endregion
24452461

24462462

@@ -3382,37 +3398,44 @@ def remove_vm_secret(cmd, resource_group_name, vm_name, keyvault, certificate=No
33823398
# region VirtualMachines UnmanagedDisks
33833399
def attach_unmanaged_data_disk(cmd, resource_group_name, vm_name, new=False, vhd_uri=None, lun=None,
33843400
disk_name=None, size_gb=1023, caching=None):
3385-
DataDisk, DiskCreateOptionTypes, VirtualHardDisk = cmd.get_models(
3386-
'DataDisk', 'DiskCreateOptionTypes', 'VirtualHardDisk')
3401+
from .operations.vm import convert_show_result_to_snake_case
3402+
from ._vm_utils import DiskCreateOptionTypes
33873403
if not new and not disk_name:
33883404
raise CLIError('Please provide the name of the existing disk to attach')
3389-
create_option = DiskCreateOptionTypes.empty if new else DiskCreateOptionTypes.attach
33903405

3391-
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
3406+
vm = get_vm_to_update_by_aaz(cmd, resource_group_name, vm_name)
3407+
vm = convert_show_result_to_snake_case(vm)
33923408
if disk_name is None:
33933409
import datetime
33943410
disk_name = vm_name + '-' + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
33953411
# pylint: disable=no-member
33963412
if vhd_uri is None:
3397-
if not hasattr(vm.storage_profile.os_disk, 'vhd') or not vm.storage_profile.os_disk.vhd:
3413+
if not vm.get('storage_profile', {}).get('os_disk', {}).get('vhd'):
33983414
raise CLIError('Adding unmanaged disks to a VM with managed disks is not supported')
3399-
blob_uri = vm.storage_profile.os_disk.vhd.uri
3415+
blob_uri = vm['storage_profile']['os_disk']['vhd']['uri']
34003416
vhd_uri = blob_uri[0:blob_uri.rindex('/') + 1] + disk_name + '.vhd'
34013417

34023418
if lun is None:
3403-
lun = _get_disk_lun(vm.storage_profile.data_disks)
3404-
disk = DataDisk(lun=lun, vhd=VirtualHardDisk(uri=vhd_uri), name=disk_name,
3405-
create_option=create_option,
3406-
caching=caching, disk_size_gb=size_gb if new else None)
3407-
if vm.storage_profile.data_disks is None:
3408-
vm.storage_profile.data_disks = []
3409-
vm.storage_profile.data_disks.append(disk)
3410-
return set_vm(cmd, vm)
3419+
lun = _get_disk_lun_by_aaz(vm.get('storage_profile', {}).get('data_disks'))
3420+
disk = {
3421+
'caching': caching,
3422+
'create_option': DiskCreateOptionTypes.EMPTY.value if new else DiskCreateOptionTypes.ATTACH.value,
3423+
'disk_size_gb': size_gb if new else None,
3424+
'lun': lun,
3425+
'name': disk_name,
3426+
'vhd': {
3427+
'uri': vhd_uri
3428+
}
3429+
}
3430+
if not vm.get('storage_profile', {}).get('data_disks'):
3431+
vm['storage_profile']['data_disks'] = []
3432+
vm['storage_profile']['data_disks'].append(disk)
3433+
return set_vm_by_aaz(cmd, vm)
34113434

34123435

34133436
def list_unmanaged_disks(cmd, resource_group_name, vm_name):
3414-
vm = get_vm(cmd, resource_group_name, vm_name)
3415-
return vm.storage_profile.data_disks # pylint: disable=no-member
3437+
vm = get_vm_by_aaz(cmd, resource_group_name, vm_name)
3438+
return vm.get('storageProfile', {}).get('dataDisks')
34163439
# endregion
34173440

34183441

0 commit comments

Comments
 (0)