From 74c4ccc32721357fe3f65cb9c504f0fa0b39cb48 Mon Sep 17 00:00:00 2001 From: william051200 Date: Mon, 29 Dec 2025 11:49:30 +0800 Subject: [PATCH 1/8] Migrated disk_access in create_snapshot --- src/azure-cli/azure/cli/command_modules/vm/custom.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/custom.py b/src/azure-cli/azure/cli/command_modules/vm/custom.py index 28ed17b1169..52c75404fcc 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -20,6 +20,7 @@ # the urlopen is imported for automation purpose from urllib.request import urlopen # noqa, pylint: disable=import-error,unused-import,ungrouped-imports +from jinja2.filters import do_slice from knack.log import get_logger from knack.util import CLIError from azure.cli.core.azclierror import ( @@ -771,9 +772,11 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size namespace='Microsoft.Compute', type='diskEncryptionSets', name=disk_encryption_set) if disk_access is not None and not is_valid_resource_id(disk_access): - disk_access = resource_id( - subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name, - namespace='Microsoft.Compute', type='diskAccesses', name=disk_access) + from .aaz.latest.disk_access import Show as DiskAccessShow + disk_access = DiskAccessShow(cli_ctx=cmd.cli_ctx)(command_args={ + 'resource_group': resource_group_name, + 'disk_access_name': disk_access + })['id'] if disk_encryption_set is not None and encryption_type is None: raise CLIError('usage error: Please specify --encryption-type.') From 08ac2fcd99db7c7407bf57d406cbd51dd54999ba Mon Sep 17 00:00:00 2001 From: william051200 Date: Mon, 29 Dec 2025 15:37:38 +0800 Subject: [PATCH 2/8] Migrated disk_encryption_set in create_snapshot --- src/azure-cli/azure/cli/command_modules/vm/custom.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/custom.py b/src/azure-cli/azure/cli/command_modules/vm/custom.py index 52c75404fcc..5142f7274d2 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -767,9 +767,11 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size raise CLIError('Please supply size for the snapshots') if disk_encryption_set is not None and not is_valid_resource_id(disk_encryption_set): - disk_encryption_set = resource_id( - subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name, - namespace='Microsoft.Compute', type='diskEncryptionSets', name=disk_encryption_set) + from .aaz.latest.disk_encryption_set import Show as DiskEncryptionSetShow + disk_encryption_set = DiskEncryptionSetShow(cli_ctx=cmd.cli_ctx)(command_args={ + 'resource_group': resource_group_name, + 'disk_encryption_set_name': disk_encryption_set + })['id'] if disk_access is not None and not is_valid_resource_id(disk_access): from .aaz.latest.disk_access import Show as DiskAccessShow From 33c70609ac8293728cfad3ce5e79539a52fc8f5d Mon Sep 17 00:00:00 2001 From: william051200 Date: Mon, 29 Dec 2025 15:43:27 +0800 Subject: [PATCH 3/8] Removed unused import --- src/azure-cli/azure/cli/command_modules/vm/custom.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/custom.py b/src/azure-cli/azure/cli/command_modules/vm/custom.py index 5142f7274d2..e1f91d445d1 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -20,7 +20,6 @@ # the urlopen is imported for automation purpose from urllib.request import urlopen # noqa, pylint: disable=import-error,unused-import,ungrouped-imports -from jinja2.filters import do_slice from knack.log import get_logger from knack.util import CLIError from azure.cli.core.azclierror import ( @@ -738,7 +737,6 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size public_network_access=None, accelerated_network=None, architecture=None, elastic_san_resource_id=None, bandwidth_copy_speed=None, instant_access_duration_minutes=None): from azure.mgmt.core.tools import resource_id, is_valid_resource_id - from azure.cli.core.commands.client_factory import get_subscription_id location = location or _get_resource_group_location(cmd.cli_ctx, resource_group_name) if source_blob_uri: From 5338eac28d1cc97757de701b49173dec3e8887e1 Mon Sep 17 00:00:00 2001 From: william051200 Date: Mon, 29 Dec 2025 15:52:48 +0800 Subject: [PATCH 4/8] Migrated create_snapshot validator --- .../cli/command_modules/vm/_validators.py | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index eac634252f6..26afe29541e 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -1980,6 +1980,32 @@ def _validate_gallery_image_reference(cmd, namespace): 'compute, shared or community gallery image version. For details about valid ' 'format, please refer to the help sample'.format(gallery_image_reference)) +def _validate_gallery_image_reference_by_aaz(cmd, namespace): + is_validate = 'gallery_image_reference' in namespace and namespace.gallery_image_reference is not None + + if not is_validate: + return + + from azure.cli.command_modules.vm._image_builder import GalleryImageReferenceType + from ._vm_utils import is_compute_gallery_image_id, is_community_gallery_image_id, \ + is_shared_gallery_image_id + + gallery_image_reference = namespace.gallery_image_reference + if is_compute_gallery_image_id(gallery_image_reference): + namespace.gallery_image_reference_type = GalleryImageReferenceType.COMPUTE.backend_key + return + if is_community_gallery_image_id(gallery_image_reference): + namespace.gallery_image_reference_type = GalleryImageReferenceType.COMMUNITY.backend_key + return + if is_shared_gallery_image_id(gallery_image_reference): + namespace.gallery_image_reference_type = GalleryImageReferenceType.SHARED.backend_key + return + + from azure.cli.core.parser import InvalidArgumentValueError + raise InvalidArgumentValueError('usage error: {} is an invalid gallery image reference, please provide valid ' + 'compute, shared or community gallery image version. For details about valid ' + 'format, please refer to the help sample'.format(gallery_image_reference)) + def process_disk_create_namespace(cmd, namespace): from azure.core.exceptions import HttpResponseError @@ -2069,7 +2095,7 @@ def process_snapshot_create_namespace(cmd, namespace): from azure.core.exceptions import HttpResponseError validate_tags(namespace) validate_edge_zone(cmd, namespace) - _validate_gallery_image_reference(cmd, namespace) + _validate_gallery_image_reference_by_aaz(cmd, namespace) if namespace.source: usage_error = 'usage error: --source {SNAPSHOT | DISK} | --source VHD_BLOB_URI [--source-storage-account-id ID]' try: From 04e0d046e4f4a52fd006a0c59cfc3acf33253898 Mon Sep 17 00:00:00 2001 From: william051200 Date: Mon, 29 Dec 2025 16:21:20 +0800 Subject: [PATCH 5/8] Added handling to create_snapshot --- .../azure/cli/command_modules/vm/custom.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/custom.py b/src/azure-cli/azure/cli/command_modules/vm/custom.py index e1f91d445d1..b01e077ac62 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -769,14 +769,24 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size disk_encryption_set = DiskEncryptionSetShow(cli_ctx=cmd.cli_ctx)(command_args={ 'resource_group': resource_group_name, 'disk_encryption_set_name': disk_encryption_set - })['id'] + }) + + if disk_encryption_set: + disk_encryption_set = disk_encryption_set['id'] + else: + disk_encryption_set = None if disk_access is not None and not is_valid_resource_id(disk_access): from .aaz.latest.disk_access import Show as DiskAccessShow disk_access = DiskAccessShow(cli_ctx=cmd.cli_ctx)(command_args={ 'resource_group': resource_group_name, 'disk_access_name': disk_access - })['id'] + }) + + if disk_access: + disk_access = disk_access['id'] + else: + disk_access = None if disk_encryption_set is not None and encryption_type is None: raise CLIError('usage error: Please specify --encryption-type.') From aa1c132042fb7d547bacae19276ad5e14cd1aa1f Mon Sep 17 00:00:00 2001 From: william051200 Date: Tue, 30 Dec 2025 09:54:04 +0800 Subject: [PATCH 6/8] Removed unused import --- src/azure-cli/azure/cli/command_modules/vm/_validators.py | 5 +++-- src/azure-cli/azure/cli/command_modules/vm/custom.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index 26afe29541e..ee36c72ace0 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -1980,7 +1980,8 @@ def _validate_gallery_image_reference(cmd, namespace): 'compute, shared or community gallery image version. For details about valid ' 'format, please refer to the help sample'.format(gallery_image_reference)) -def _validate_gallery_image_reference_by_aaz(cmd, namespace): + +def _validate_gallery_image_reference_by_aaz(namespace): is_validate = 'gallery_image_reference' in namespace and namespace.gallery_image_reference is not None if not is_validate: @@ -2095,7 +2096,7 @@ def process_snapshot_create_namespace(cmd, namespace): from azure.core.exceptions import HttpResponseError validate_tags(namespace) validate_edge_zone(cmd, namespace) - _validate_gallery_image_reference_by_aaz(cmd, namespace) + _validate_gallery_image_reference_by_aaz(namespace) if namespace.source: usage_error = 'usage error: --source {SNAPSHOT | DISK} | --source VHD_BLOB_URI [--source-storage-account-id ID]' try: diff --git a/src/azure-cli/azure/cli/command_modules/vm/custom.py b/src/azure-cli/azure/cli/command_modules/vm/custom.py index b01e077ac62..49162faa78e 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -736,7 +736,7 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size encryption_type=None, network_access_policy=None, disk_access=None, edge_zone=None, public_network_access=None, accelerated_network=None, architecture=None, elastic_san_resource_id=None, bandwidth_copy_speed=None, instant_access_duration_minutes=None): - from azure.mgmt.core.tools import resource_id, is_valid_resource_id + from azure.mgmt.core.tools import is_valid_resource_id location = location or _get_resource_group_location(cmd.cli_ctx, resource_group_name) if source_blob_uri: From d67c3c341ca5fbe43da53ac7f4c9602d54f94a2a Mon Sep 17 00:00:00 2001 From: william051200 Date: Tue, 30 Dec 2025 14:35:42 +0800 Subject: [PATCH 7/8] Removed unused parameter in commands.py --- src/azure-cli/azure/cli/command_modules/vm/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/commands.py b/src/azure-cli/azure/cli/command_modules/vm/commands.py index 00b5f2900d1..9aeb031c3c2 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -283,7 +283,7 @@ def load_command_table(self, _): g.custom_command('remove', 'remove_template_error_handler', supports_local_cache=True) g.custom_show_command('show', 'show_template_error_handler', supports_local_cache=True) - with self.command_group('snapshot', compute_snapshot_sdk, operation_group='snapshots') as g: + with self.command_group('snapshot', operation_group='snapshots') as g: g.custom_command('create', 'create_snapshot', validator=process_snapshot_create_namespace, supports_no_wait=True) from .operations.snapshot import SnapshotUpdate self.command_table['snapshot update'] = SnapshotUpdate(loader=self) From bf2d3e793ecffefec2a354836b23a6c00e7e35fb Mon Sep 17 00:00:00 2001 From: william051200 Date: Tue, 30 Dec 2025 15:17:07 +0800 Subject: [PATCH 8/8] Added exception handling --- .../azure/cli/command_modules/vm/custom.py | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/custom.py b/src/azure-cli/azure/cli/command_modules/vm/custom.py index 49162faa78e..74c0188c7a3 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -766,26 +766,34 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size if disk_encryption_set is not None and not is_valid_resource_id(disk_encryption_set): from .aaz.latest.disk_encryption_set import Show as DiskEncryptionSetShow - disk_encryption_set = DiskEncryptionSetShow(cli_ctx=cmd.cli_ctx)(command_args={ - 'resource_group': resource_group_name, - 'disk_encryption_set_name': disk_encryption_set - }) + from azure.core.exceptions import HttpResponseError + try: + disk_encryption_set = DiskEncryptionSetShow(cli_ctx=cmd.cli_ctx)(command_args={ + 'resource_group': resource_group_name, + 'disk_encryption_set_name': disk_encryption_set + }) - if disk_encryption_set: - disk_encryption_set = disk_encryption_set['id'] - else: + if disk_encryption_set: + disk_encryption_set = disk_encryption_set['id'] + else: + disk_encryption_set = None + except HttpResponseError: disk_encryption_set = None if disk_access is not None and not is_valid_resource_id(disk_access): from .aaz.latest.disk_access import Show as DiskAccessShow - disk_access = DiskAccessShow(cli_ctx=cmd.cli_ctx)(command_args={ - 'resource_group': resource_group_name, - 'disk_access_name': disk_access - }) + from azure.core.exceptions import HttpResponseError + try: + disk_access = DiskAccessShow(cli_ctx=cmd.cli_ctx)(command_args={ + 'resource_group': resource_group_name, + 'disk_access_name': disk_access + }) - if disk_access: - disk_access = disk_access['id'] - else: + if disk_access: + disk_access = disk_access['id'] + else: + disk_access = None + except HttpResponseError: disk_access = None if disk_encryption_set is not None and encryption_type is None: