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..ee36c72ace0 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -1981,6 +1981,33 @@ def _validate_gallery_image_reference(cmd, namespace): 'format, please refer to the help sample'.format(gallery_image_reference)) +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: + 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 validate_tags(namespace) @@ -2069,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(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/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) 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..74c0188c7a3 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -736,8 +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.cli.core.commands.client_factory import get_subscription_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: @@ -766,14 +765,36 @@ 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 + 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: + disk_encryption_set = None + except HttpResponseError: + disk_encryption_set = None 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 + 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: + disk_access = None + except HttpResponseError: + disk_access = None if disk_encryption_set is not None and encryption_type is None: raise CLIError('usage error: Please specify --encryption-type.')