Skip to content

Commit d630b92

Browse files
{Compute} Migrate disk and snapshot commands to aaz-based (#32596)
1 parent 1aec666 commit d630b92

File tree

10 files changed

+855
-760
lines changed

10 files changed

+855
-760
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,36 @@ def _get_latest_image_version(cli_ctx, location, publisher, offer, sku, edge_zon
323323
if not top_one:
324324
raise InvalidArgumentValueError("Can't resolve the version of '{}:{}:{}'".format(publisher, offer, sku))
325325
return top_one[0].name
326+
327+
328+
def _get_latest_image_version_by_aaz(cli_ctx, location, publisher, offer, sku, edge_zone=None):
329+
from azure.cli.core.azclierror import InvalidArgumentValueError
330+
if edge_zone is not None:
331+
from .aaz.latest.vm.image.edge_zone import List as VmImageEdgeZoneList
332+
command_args = {
333+
'edge_zone': edge_zone,
334+
'location': location,
335+
'offer': offer,
336+
'publisher': publisher,
337+
'sku': sku,
338+
'orderby': 'name desc',
339+
'top': 1,
340+
}
341+
top_one = VmImageEdgeZoneList(cli_ctx=cli_ctx)(command_args=command_args)
342+
if not top_one:
343+
raise InvalidArgumentValueError("Can't resolve the version of '{}:{}:{}:{}'"
344+
.format(publisher, offer, sku, edge_zone))
345+
else:
346+
from .aaz.latest.vm.image import List as VmImageList
347+
command_args = {
348+
'location': location,
349+
'offer': offer,
350+
'publisher': publisher,
351+
'sku': sku,
352+
'orderby': 'name desc',
353+
'top': 1,
354+
}
355+
top_one = VmImageList(cli_ctx=cli_ctx)(command_args=command_args)
356+
if not top_one:
357+
raise InvalidArgumentValueError("Can't resolve the version of '{}:{}:{}'".format(publisher, offer, sku))
358+
return top_one[0].get('name')

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,11 +1952,8 @@ def validate_vmss_disk(cmd, namespace):
19521952
raise CLIError('usage error: --disk EXIST_DISK --instance-id ID')
19531953

19541954

1955-
def _validate_gallery_image_reference(cmd, namespace):
1956-
from azure.cli.core.profiles import ResourceType
1957-
is_validate = 'gallery_image_reference' in namespace and namespace.gallery_image_reference is not None \
1958-
and cmd.supported_api_version(resource_type=ResourceType.MGMT_COMPUTE,
1959-
operation_group='disks', min_api='2022-03-02')
1955+
def _validate_gallery_image_reference(namespace):
1956+
is_validate = 'gallery_image_reference' in namespace and namespace.gallery_image_reference is not None
19601957
if not is_validate:
19611958
return
19621959

@@ -1985,9 +1982,9 @@ def process_disk_create_namespace(cmd, namespace):
19851982
from azure.core.exceptions import HttpResponseError
19861983
validate_tags(namespace)
19871984
validate_edge_zone(cmd, namespace)
1988-
_validate_gallery_image_reference(cmd, namespace)
1985+
_validate_gallery_image_reference(namespace)
19891986
_validate_security_data_uri(namespace)
1990-
_validate_upload_type(cmd, namespace)
1987+
_validate_upload_type(namespace)
19911988
_validate_secure_vm_disk_encryption_set(namespace)
19921989
_validate_hyper_v_generation(namespace)
19931990
if namespace.source:
@@ -2020,20 +2017,14 @@ def _validate_security_data_uri(namespace):
20202017
'Please specify --source when using the --security-data-uri parameter')
20212018

20222019

2023-
def _validate_upload_type(cmd, namespace):
2020+
def _validate_upload_type(namespace):
20242021
if 'upload_type' not in namespace:
20252022
return
20262023

20272024
if not namespace.upload_type and namespace.for_upload:
20282025
namespace.upload_type = 'Upload'
20292026

20302027
if namespace.upload_type == 'UploadWithSecurityData':
2031-
2032-
if not cmd.supported_api_version(min_api='2021-08-01', operation_group='disks'):
2033-
raise ArgumentUsageError(
2034-
"'UploadWithSecurityData' is not supported in the current profile. "
2035-
"Please upgrade your profile with 'az cloud set --profile newerProfile' and try again")
2036-
20372028
if not namespace.security_type:
20382029
raise RequiredArgumentMissingError(
20392030
"Please specify --security-type when the value of --upload-type is 'UploadWithSecurityData'")
@@ -2069,7 +2060,7 @@ def process_snapshot_create_namespace(cmd, namespace):
20692060
from azure.core.exceptions import HttpResponseError
20702061
validate_tags(namespace)
20712062
validate_edge_zone(cmd, namespace)
2072-
_validate_gallery_image_reference(cmd, namespace)
2063+
_validate_gallery_image_reference(namespace)
20732064
if namespace.source:
20742065
usage_error = 'usage error: --source {SNAPSHOT | DISK} | --source VHD_BLOB_URI [--source-storage-account-id ID]'
20752066
try:

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from azure.cli.command_modules.vm._client_factory import (cf_vm,
77
cf_vm_ext, cf_vm_ext_image,
88
cf_vm_image, cf_vm_image_term, cf_usage,
9-
cf_vmss, cf_disks, cf_snapshots,
10-
cf_images,
9+
cf_vmss, cf_images,
1110
cf_galleries, cf_gallery_images, cf_gallery_image_versions,
1211
cf_proximity_placement_groups,
1312
cf_dedicated_hosts, cf_dedicated_host_groups,
@@ -63,22 +62,11 @@ def load_command_table(self, _):
6362
operation_group='availability_sets'
6463
)
6564

66-
compute_disk_sdk = CliCommandType(
67-
operations_tmpl='azure.mgmt.compute.operations#DisksOperations.{}',
68-
client_factory=cf_disks,
69-
operation_group='disks'
70-
)
71-
7265
compute_image_sdk = CliCommandType(
7366
operations_tmpl='azure.mgmt.compute.operations#ImagesOperations.{}',
7467
client_factory=cf_images
7568
)
7669

77-
compute_snapshot_sdk = CliCommandType(
78-
operations_tmpl='azure.mgmt.compute.operations#SnapshotsOperations.{}',
79-
client_factory=cf_snapshots
80-
)
81-
8270
compute_vm_sdk = CliCommandType(
8371
operations_tmpl='azure.mgmt.compute.operations#VirtualMachinesOperations.{}',
8472
client_factory=cf_vm
@@ -208,7 +196,7 @@ def load_command_table(self, _):
208196
from .operations.ppg import PPGShow
209197
self.command_table["ppg show"] = PPGShow(loader=self)
210198

211-
with self.command_group('disk', compute_disk_sdk, operation_group='disks') as g:
199+
with self.command_group('disk', operation_group='disks') as g:
212200
g.custom_command('create', 'create_managed_disk', supports_no_wait=True, table_transformer=transform_disk_create_table_output, validator=process_disk_create_namespace)
213201
from .operations.disk import DiskUpdate, DiskGrantAccess
214202
self.command_table["disk grant-access"] = DiskGrantAccess(loader=self)
@@ -283,7 +271,7 @@ def load_command_table(self, _):
283271
g.custom_command('remove', 'remove_template_error_handler', supports_local_cache=True)
284272
g.custom_show_command('show', 'show_template_error_handler', supports_local_cache=True)
285273

286-
with self.command_group('snapshot', compute_snapshot_sdk, operation_group='snapshots') as g:
274+
with self.command_group('snapshot', operation_group='snapshots') as g:
287275
g.custom_command('create', 'create_snapshot', validator=process_snapshot_create_namespace, supports_no_wait=True)
288276
from .operations.snapshot import SnapshotUpdate
289277
self.command_table['snapshot update'] = SnapshotUpdate(loader=self)

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from ._vm_diagnostics_templates import get_default_diag_config
4242

4343
from ._actions import (load_images_from_aliases_doc, load_extension_images_thru_services,
44-
load_images_thru_services, _get_latest_image_version)
44+
load_images_thru_services, _get_latest_image_version, _get_latest_image_version_by_aaz)
4545
from ._client_factory import (_compute_client_factory, cf_vm_image_term)
4646

4747
from .aaz.latest.vm.disk import AttachDetachDataDisk
@@ -484,8 +484,8 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p
484484
if len(terms) == 4: # URN
485485
disk_publisher, disk_offer, disk_sku, disk_version = terms[0], terms[1], terms[2], terms[3]
486486
if disk_version.lower() == 'latest':
487-
disk_version = _get_latest_image_version(cmd.cli_ctx, location, disk_publisher, disk_offer,
488-
disk_sku)
487+
disk_version = _get_latest_image_version_by_aaz(cmd.cli_ctx, location, disk_publisher, disk_offer,
488+
disk_sku)
489489
else: # error
490490
raise CLIError('usage error: --image-reference should be ID or URN (publisher:offer:sku:version).')
491491
else:
@@ -494,14 +494,20 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p
494494
disk_publisher, disk_offer, disk_sku, disk_version = \
495495
terms['child_name_1'], terms['child_name_3'], terms['child_name_4'], terms['child_name_5']
496496

497-
client = _compute_client_factory(cmd.cli_ctx)
498-
response = client.virtual_machine_images.get(location=location, publisher_name=disk_publisher,
499-
offer=disk_offer, skus=disk_sku, version=disk_version)
497+
from .aaz.latest.vm.image import Show as VmImageShow
498+
command_args = {
499+
'location': location,
500+
'offer': disk_offer,
501+
'publisher': disk_publisher,
502+
'sku': disk_sku,
503+
'version': disk_version,
504+
}
505+
response = VmImageShow(cli_ctx=cmd.cli_ctx)(command_args=command_args)
500506

501-
if hasattr(response, 'hyper_v_generation'):
502-
if response.hyper_v_generation == 'V1':
507+
if response.get('hyper_v_generation'):
508+
if response.get('hyper_v_generation') == 'V1':
503509
logger.warning(UPGRADE_SECURITY_HINT)
504-
elif response.hyper_v_generation == 'V2':
510+
elif response.get('hyper_v_generation') == 'V2':
505511
# set default value of hyper_v_generation
506512
if hyper_v_generation == 'V1':
507513
hyper_v_generation = 'V2'
@@ -512,7 +518,7 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p
512518
logger.warning(UPGRADE_SECURITY_HINT)
513519

514520
# image_reference is an ID now
515-
image_reference = {'id': response.id}
521+
image_reference = {'id': response.get('id')}
516522
if image_reference_lun is not None:
517523
image_reference['lun'] = image_reference_lun
518524

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_confidential_disk_encryption_set.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ interactions:
11251125
User-Agent:
11261126
- AZURECLI/2.76.0 azsdk-python-core/1.35.0 Python/3.11.9 (Windows-10-10.0.26100-SP0)
11271127
method: GET
1128-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/westus/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/windows-cvm/skus/2022-datacenter-cvm/versions?$top=1&$orderby=name%20desc&api-version=2024-11-01
1128+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/westus/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/windows-cvm/skus/2022-datacenter-cvm/versions?$top=1&$orderby=name%20desc&api-version=2024-07-01
11291129
response:
11301130
body:
11311131
string: "[\r\n {\r\n \"location\": \"westus\",\r\n \"name\": \"20348.469.220106\",\r\n
@@ -1178,7 +1178,7 @@ interactions:
11781178
User-Agent:
11791179
- AZURECLI/2.76.0 azsdk-python-core/1.35.0 Python/3.11.9 (Windows-10-10.0.26100-SP0)
11801180
method: GET
1181-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/westus/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/windows-cvm/skus/2022-datacenter-cvm/versions/20348.469.220106?api-version=2024-11-01
1181+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/westus/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/windows-cvm/skus/2022-datacenter-cvm/versions/20348.469.220106?api-version=2024-07-01
11821182
response:
11831183
body:
11841184
string: "{\r\n \"properties\": {\r\n \"hyperVGeneration\": \"V2\",\r\n \"architecture\":
@@ -1534,7 +1534,7 @@ interactions:
15341534
User-Agent:
15351535
- AZURECLI/2.76.0 azsdk-python-core/1.35.0 Python/3.11.9 (Windows-10-10.0.26100-SP0)
15361536
method: GET
1537-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/westus/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/windows-cvm/skus/2022-datacenter-cvm/versions?$top=1&$orderby=name%20desc&api-version=2024-11-01
1537+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/westus/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/windows-cvm/skus/2022-datacenter-cvm/versions?$top=1&$orderby=name%20desc&api-version=2024-07-01
15381538
response:
15391539
body:
15401540
string: "[\r\n {\r\n \"location\": \"westus\",\r\n \"name\": \"20348.469.220106\",\r\n
@@ -1587,7 +1587,7 @@ interactions:
15871587
User-Agent:
15881588
- AZURECLI/2.76.0 azsdk-python-core/1.35.0 Python/3.11.9 (Windows-10-10.0.26100-SP0)
15891589
method: GET
1590-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/westus/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/windows-cvm/skus/2022-datacenter-cvm/versions/20348.469.220106?api-version=2024-11-01
1590+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/westus/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/windows-cvm/skus/2022-datacenter-cvm/versions/20348.469.220106?api-version=2024-07-01
15911591
response:
15921592
body:
15931593
string: "{\r\n \"properties\": {\r\n \"hyperVGeneration\": \"V2\",\r\n \"architecture\":

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_create_image_version_with_region_cvm_encryption_pmk.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ interactions:
502502
User-Agent:
503503
- AZURECLI/2.77.0 azsdk-python-core/1.35.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
504504
method: GET
505-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/centraluseuap/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/WindowsServer/skus/2022-datacenter-smalldisk-g2/versions?$top=1&$orderby=name%20desc&api-version=2024-11-01
505+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/centraluseuap/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/WindowsServer/skus/2022-datacenter-smalldisk-g2/versions?$top=1&$orderby=name%20desc&api-version=2024-07-01
506506
response:
507507
body:
508508
string: "[\r\n {\r\n \"location\": \"CentralUSEUAP\",\r\n \"name\": \"20348.4171.250903\",\r\n
@@ -554,7 +554,7 @@ interactions:
554554
User-Agent:
555555
- AZURECLI/2.77.0 azsdk-python-core/1.35.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
556556
method: GET
557-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/centraluseuap/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/WindowsServer/skus/2022-datacenter-smalldisk-g2/versions/20348.4171.250903?api-version=2024-11-01
557+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/centraluseuap/publishers/MicrosoftWindowsServer/artifacttypes/vmimage/offers/WindowsServer/skus/2022-datacenter-smalldisk-g2/versions/20348.4171.250903?api-version=2024-07-01
558558
response:
559559
body:
560560
string: "{\r\n \"properties\": {\r\n \"hyperVGeneration\": \"V2\",\r\n \"architecture\":

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_disk_encryption_set_disk_update.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ interactions:
831831
User-Agent:
832832
- AZURECLI/2.77.0 azsdk-python-core/1.35.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
833833
method: GET
834-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/eastus/publishers/Debian/artifacttypes/vmimage/offers/debian-11/skus/11-backports-gen2/versions?$top=1&$orderby=name%20desc&api-version=2024-11-01
834+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/eastus/publishers/Debian/artifacttypes/vmimage/offers/debian-11/skus/11-backports-gen2/versions?$top=1&$orderby=name%20desc&api-version=2024-07-01
835835
response:
836836
body:
837837
string: "[\r\n {\r\n \"location\": \"eastus\",\r\n \"name\": \"0.20250703.2162\",\r\n
@@ -883,7 +883,7 @@ interactions:
883883
User-Agent:
884884
- AZURECLI/2.77.0 azsdk-python-core/1.35.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
885885
method: GET
886-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/eastus/publishers/Debian/artifacttypes/vmimage/offers/debian-11/skus/11-backports-gen2/versions/0.20250703.2162?api-version=2024-11-01
886+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Compute/locations/eastus/publishers/Debian/artifacttypes/vmimage/offers/debian-11/skus/11-backports-gen2/versions/0.20250703.2162?api-version=2024-07-01
887887
response:
888888
body:
889889
string: "{\r\n \"properties\": {\r\n \"hyperVGeneration\": \"V2\",\r\n \"architecture\":

0 commit comments

Comments
 (0)