From c272e35df610838b49f853e18a4bcc58dd586fc2 Mon Sep 17 00:00:00 2001 From: william051200 Date: Fri, 6 Mar 2026 16:11:06 +0800 Subject: [PATCH 1/7] Clean up mgmt.compute --- .../cli/command_modules/vm/_client_factory.py | 64 ------- .../azure/cli/command_modules/vm/commands.py | 158 ++++-------------- .../azure/cli/command_modules/vm/custom.py | 90 +--------- 3 files changed, 39 insertions(+), 273 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py b/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py index ee6329e9129..d2982242df8 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py @@ -16,14 +16,6 @@ def cf_vm(cli_ctx, _): return _compute_client_factory(cli_ctx).virtual_machines -def cf_vm_ext(cli_ctx, _): - return _compute_client_factory(cli_ctx).virtual_machine_extensions - - -def cf_vm_ext_image(cli_ctx, _): - return _compute_client_factory(cli_ctx).virtual_machine_extension_images - - def cf_vm_image(cli_ctx, _): return _compute_client_factory(cli_ctx).virtual_machine_images @@ -43,50 +35,10 @@ def cf_vmss(cli_ctx, _): return _compute_client_factory(cli_ctx).virtual_machine_scale_sets -def cf_vmss_vm(cli_ctx, _): - return _compute_client_factory(cli_ctx).virtual_machine_scale_set_vms - - -def cf_vm_sizes(cli_ctx, _): - return _compute_client_factory(cli_ctx).virtual_machine_sizes - - -def cf_disks(cli_ctx, _): - return _compute_client_factory(cli_ctx).disks - - -def cf_snapshots(cli_ctx, _): - return _compute_client_factory(cli_ctx).snapshots - - -def cf_disk_accesses(cli_ctx, _): - return _compute_client_factory(cli_ctx).disk_accesses - - def cf_images(cli_ctx, _): return _compute_client_factory(cli_ctx).images -def cf_rolling_upgrade_commands(cli_ctx, _): - return _compute_client_factory(cli_ctx).virtual_machine_scale_set_rolling_upgrades - - -def cf_galleries(cli_ctx, _): - return _compute_client_factory(cli_ctx).galleries - - -def cf_gallery_images(cli_ctx, _): - return _compute_client_factory(cli_ctx).gallery_images - - -def cf_gallery_image_versions(cli_ctx, _): - return _compute_client_factory(cli_ctx).gallery_image_versions - - -def cf_proximity_placement_groups(cli_ctx, _): - return _compute_client_factory(cli_ctx).proximity_placement_groups - - def cf_dedicated_hosts(cli_ctx, _): return _compute_client_factory(cli_ctx).dedicated_hosts @@ -127,18 +79,10 @@ def cf_vm_cl(cli_ctx, *_): ComputeManagementClient) -def cf_shared_galleries(cli_ctx, *_): - return cf_vm_cl(cli_ctx).shared_galleries - - def cf_shared_gallery_image(cli_ctx, *_): return cf_vm_cl(cli_ctx).shared_gallery_images -def cf_shared_gallery_image_version(cli_ctx, *_): - return cf_vm_cl(cli_ctx).shared_gallery_image_versions - - def cf_community_gallery(cli_ctx, *_): return cf_vm_cl(cli_ctx).community_galleries @@ -147,13 +91,5 @@ def cf_community_gallery_image(cli_ctx, *_): return cf_vm_cl(cli_ctx).community_gallery_images -def cf_community_gallery_image_version(cli_ctx, *_): - return cf_vm_cl(cli_ctx).community_gallery_image_versions - - def cf_capacity_reservation_groups(cli_ctx, *_): return cf_vm_cl(cli_ctx).capacity_reservation_groups - - -def cf_capacity_reservations(cli_ctx, *_): - return cf_vm_cl(cli_ctx).capacity_reservations 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 165e8664a6c..0c57c9c7ae9 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -4,15 +4,11 @@ # -------------------------------------------------------------------------------------------- from azure.cli.command_modules.vm._client_factory import (cf_vm, - cf_vm_ext, cf_vm_ext_image, cf_vm_image, cf_vm_image_term, cf_usage, cf_vmss, cf_images, - cf_galleries, cf_gallery_images, cf_gallery_image_versions, - cf_proximity_placement_groups, cf_dedicated_hosts, cf_dedicated_host_groups, cf_log_analytics_data_plane, - cf_capacity_reservation_groups, cf_capacity_reservations, - cf_community_gallery) + cf_capacity_reservation_groups) from azure.cli.command_modules.vm._format import ( transform_ip_addresses, transform_vm, transform_vm_create_output, transform_vm_usage_list, transform_vm_list, transform_disk_create_table_output, transform_sku_for_table_output, transform_disk_show_table_output, @@ -57,11 +53,6 @@ def load_command_table(self, _): client_factory=image_builder_client_factory ) - compute_availset_profile = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#AvailabilitySetsOperations.{}', - operation_group='availability_sets' - ) - compute_image_sdk = CliCommandType( operations_tmpl='azure.mgmt.compute.operations#ImagesOperations.{}', client_factory=cf_images @@ -72,16 +63,6 @@ def load_command_table(self, _): client_factory=cf_vm ) - compute_vm_extension_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#VirtualMachineExtensionsOperations.{}', - client_factory=cf_vm_ext - ) - - compute_vm_extension_image_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#VirtualMachineExtensionImagesOperations.{}', - client_factory=cf_vm_ext_image - ) - compute_vm_image_sdk = CliCommandType( operations_tmpl='azure.mgmt.compute.operations#VirtualMachineImagesOperations.{}', client_factory=cf_vm_image @@ -97,47 +78,12 @@ def load_command_table(self, _): client_factory=cf_usage ) - compute_vm_run_profile = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#VirtualMachineRunCommandsOperations.{}' - ) - - compute_vmss_run_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#VirtualMachineScaleSetVmRunCommandsOperations.{}' - ) - compute_vmss_sdk = CliCommandType( operations_tmpl='azure.mgmt.compute.operations#VirtualMachineScaleSetsOperations.{}', client_factory=cf_vmss, operation_group='virtual_machine_scale_sets' ) - compute_galleries_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#GalleriesOperations.{}', - client_factory=cf_galleries, - ) - - compute_gallery_images_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#GalleryImagesOperations.{}', - client_factory=cf_gallery_images, - ) - - compute_gallery_image_versions_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#GalleryImageVersionsOperations.{}', - client_factory=cf_gallery_image_versions, - ) - - compute_gallery_application_profile = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#GalleryApplicationsOperations.{}', - ) - - compute_gallery_application_version_profile = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#GalleryApplicationVersionsOperations.{}', - ) - - compute_proximity_placement_groups_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#ProximityPlacementGroupsOperations.{}', - ) - compute_dedicated_host_sdk = CliCommandType( operations_tmpl="azure.mgmt.compute.operations#DedicatedHostsOperations.{}", client_factory=cf_dedicated_hosts, @@ -153,10 +99,6 @@ def load_command_table(self, _): client_factory=cf_img_bldr_image_templates, ) - compute_disk_encryption_set_profile = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#DiskEncryptionSetsOperations.{}' - ) - monitor_custom = CliCommandType( operations_tmpl='azure.cli.command_modules.monitor.custom#{}', exception_handler=monitor_exception_handler @@ -175,23 +117,6 @@ def load_command_table(self, _): client_factory=cf_capacity_reservation_groups ) - capacity_reservations_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#CapacityReservationsOperations.{}', - client_factory=cf_capacity_reservations - ) - - restore_point = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#RestorePointsOperations.{}', - ) - - restore_point_collection = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#RestorePointCollectionsOperations.{}' - ) - - community_gallery_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#CommunityGalleriesOperations.{}', - client_factory=cf_community_gallery) - with self.command_group("ppg"): from .operations.ppg import PPGShow self.command_table["ppg show"] = PPGShow(loader=self) @@ -210,12 +135,12 @@ def load_command_table(self, _): from .operations.disk import DiskConfigUpdate self.command_table["disk config update"] = DiskConfigUpdate(loader=self) - with self.command_group('disk-encryption-set', compute_disk_encryption_set_profile, operation_group='disk_encryption_sets'): + with self.command_group('disk-encryption-set', operation_group='disk_encryption_sets'): from .operations.disk_encryption_set import DiskEncryptionSetCreate, DiskEncryptionSetUpdate self.command_table['disk-encryption-set create'] = DiskEncryptionSetCreate(loader=self) self.command_table['disk-encryption-set update'] = DiskEncryptionSetUpdate(loader=self) - with self.command_group('disk-encryption-set identity', compute_disk_encryption_set_profile, operation_group='disk_encryption_sets') as g: + with self.command_group('disk-encryption-set identity', operation_group='disk_encryption_sets') as g: from .operations.disk_encryption_set_identity import DiskEncryptionSetIdentityAssign, DiskEncryptionSetIdentityRemove self.command_table['disk-encryption-set identity assign'] = DiskEncryptionSetIdentityAssign(loader=self) self.command_table['disk-encryption-set identity remove'] = DiskEncryptionSetIdentityRemove(loader=self) @@ -282,34 +207,31 @@ def load_command_table(self, _): g.custom_show_command('show', 'show_vm_identity') with self.command_group('vm') as g: + g.custom_command('application set', 'set_vm_applications', validator=process_set_applications_namespace) + g.custom_command('application list', 'list_vm_applications') + g.custom_command('auto-shutdown', 'auto_shutdown_vm') g.custom_command('create', 'create_vm', transform=transform_vm_create_output, supports_no_wait=True, table_transformer=deployment_validate_table_format, validator=process_vm_create_namespace, exception_handler=handle_template_based_exception) - g.custom_command('list', 'list_vm', table_transformer=transform_vm_list) - g.custom_show_command('show', 'show_vm', table_transformer=transform_vm) - g.generic_update_command('update', getter_name='get_vm_to_update_by_aaz', setter_name='update_vm', setter_type=compute_custom, command_type=compute_custom, supports_no_wait=True, validator=process_vm_update_namespace) - g.custom_command('open-port', 'open_vm_port') - - with self.command_group('vm', compute_vm_sdk) as g: - g.custom_command('application set', 'set_vm_applications', validator=process_set_applications_namespace, min_api='2021-07-01') - g.custom_command('application list', 'list_vm_applications', min_api='2021-07-01') - g.custom_command('get-instance-view', 'get_instance_view', table_transformer='{Name:name, ResourceGroup:resourceGroup, Location:location, ProvisioningState:provisioningState, PowerState:instanceView.statuses[1].displayStatus}') + g.custom_command('install-patches', 'install_vm_patches', supports_no_wait=True) + g.custom_command('list', 'list_vm', table_transformer=transform_vm_list) g.custom_command('list-ip-addresses', 'list_vm_ip_addresses', table_transformer=transform_ip_addresses) - g.custom_command('list-skus', 'list_skus', table_transformer=transform_sku_for_table_output, min_api='2017-03-30') - g.command('list-usage', 'list', command_type=compute_vm_usage_sdk, transform=transform_vm_usage_list, table_transformer='[].{Name:localName, CurrentValue:currentValue, Limit:limit}') + g.custom_command('list-sizes', 'list_vm_sizes', deprecate_info=g.deprecate(redirect='az vm list-skus')) + g.custom_command('list-skus', 'list_skus', table_transformer=transform_sku_for_table_output) + g.custom_command('open-port', 'open_vm_port') g.custom_command('resize', 'resize_vm', supports_no_wait=True) g.custom_command('restart', 'restart_vm', supports_no_wait=True) - g.command('stop', 'begin_power_off', supports_no_wait=True, validator=process_vm_vmss_stop) + g.custom_show_command('show', 'show_vm', table_transformer=transform_vm) + g.generic_update_command('update', getter_name='get_vm_to_update_by_aaz', setter_name='update_vm', setter_type=compute_custom, command_type=compute_custom, supports_no_wait=True, validator=process_vm_update_namespace) g.wait_command('wait', getter_name='get_instance_view', getter_type=compute_custom) - g.custom_command('auto-shutdown', 'auto_shutdown_vm') - g.custom_command('list-sizes', 'list_vm_sizes', deprecate_info=g.deprecate(redirect='az vm list-skus')) from .operations.vm import VMCapture self.command_table['vm capture'] = VMCapture(loader=self) - with self.command_group('vm') as g: - g.custom_command('install-patches', 'install_vm_patches', supports_no_wait=True, min_api='2020-12-01') + with self.command_group('vm', compute_vm_sdk) as g: + g.command('list-usage', 'list', command_type=compute_vm_usage_sdk, transform=transform_vm_usage_list, table_transformer='[].{Name:localName, CurrentValue:currentValue, Limit:limit}') + g.command('stop', 'begin_power_off', supports_no_wait=True, validator=process_vm_vmss_stop) - with self.command_group('vm availability-set', compute_availset_profile) as g: + with self.command_group('vm availability-set') as g: g.custom_command('create', 'create_av_set', table_transformer=deployment_validate_table_format, supports_no_wait=True, exception_handler=handle_template_based_exception) from .operations.vm_availability_set import AvailabilitySetUpdate, AvailabilitySetConvert self.command_table['vm availability-set update'] = AvailabilitySetUpdate(loader=self) @@ -333,12 +255,12 @@ def load_command_table(self, _): g.custom_command('disable', 'decrypt_vm') g.custom_show_command('show', 'show_vm_encryption_status', table_transformer=transform_vm_encryption_show_table_output) - with self.command_group('vm extension', compute_vm_extension_sdk) as g: + with self.command_group('vm extension') as g: g.custom_show_command('show', 'show_extensions', table_transformer=transform_extension_show_table_output) g.custom_command('set', 'set_extension', supports_no_wait=True) g.custom_command('list', 'list_extensions', table_transformer='[].' + transform_extension_show_table_output) - with self.command_group('vm extension image', compute_vm_extension_image_sdk) as g: + with self.command_group('vm extension image') as g: g.custom_command('list', 'list_vm_extension_images') with self.command_group('vm image', compute_vm_image_sdk) as g: @@ -355,14 +277,14 @@ def load_command_table(self, _): g.custom_command('cancel', 'cancel_terms') g.custom_show_command('show', 'get_terms') - with self.command_group('vm nic', compute_vm_sdk) as g: + with self.command_group('vm nic') as g: g.custom_command('add', 'add_vm_nic') g.custom_command('remove', 'remove_vm_nic') g.custom_command('set', 'set_vm_nic') g.custom_show_command('show', 'show_vm_nic') g.custom_command('list', 'list_vm_nics') - with self.command_group('vm run-command', compute_vm_run_profile, operation_group='virtual_machine_run_commands') as g: + with self.command_group('vm run-command', operation_group='virtual_machine_run_commands') as g: g.custom_command('invoke', 'vm_run_command_invoke', supports_no_wait=True) g.custom_command('list', 'vm_run_command_list') g.custom_show_command('show', 'vm_run_command_show') @@ -370,7 +292,7 @@ def load_command_table(self, _): g.custom_command('update', 'vm_run_command_update', supports_no_wait=True) g.custom_wait_command('wait', 'vm_run_command_show') - with self.command_group('vm secret', compute_vm_sdk) as g: + with self.command_group('vm secret') as g: g.custom_command('format', 'get_vm_format_secret', validator=process_vm_secret_format) g.custom_command('add', 'add_vm_secret') g.custom_command('list', 'list_vm_secrets') @@ -381,7 +303,7 @@ def load_command_table(self, _): g.custom_command('detach', 'detach_unmanaged_data_disk') g.custom_command('list', 'list_unmanaged_disks') - with self.command_group('vm user', compute_vm_sdk, supports_no_wait=True) as g: + with self.command_group('vm user', supports_no_wait=True) as g: g.custom_command('update', 'set_user') g.custom_command('delete', 'delete_user') g.custom_command('reset-ssh', 'reset_linux_ssh') @@ -451,25 +373,25 @@ def load_command_table(self, _): g.custom_command('list', 'list_vmss_extensions') g.custom_command('upgrade', 'upgrade_vmss_extension', min_api='2020-06-01', supports_no_wait=True) - with self.command_group('vmss extension image', compute_vm_extension_image_sdk) as g: + with self.command_group('vmss extension image') as g: g.custom_command('list', 'list_vm_extension_images') - with self.command_group('vmss run-command', compute_vmss_run_sdk) as g: + with self.command_group('vmss run-command') as g: g.custom_command('invoke', 'vmss_run_command_invoke') g.custom_show_command('show', 'vmss_run_command_show') g.custom_command('create', 'vmss_run_command_create', supports_no_wait=True) g.custom_command('update', 'vmss_run_command_update', supports_no_wait=True) - with self.command_group('sig', compute_galleries_sdk, operation_group='galleries') as g: + with self.command_group('sig', operation_group='galleries') as g: from .operations.sig import SigCreate, SigUpdate, SigShow self.command_table['sig create'] = SigCreate(loader=self) self.command_table['sig update'] = SigUpdate(loader=self) self.command_table['sig show'] = SigShow(loader=self) - with self.command_group('sig', community_gallery_sdk, client_factory=cf_community_gallery, operation_group='shared_galleries', min_api='2022-01-03') as g: + with self.command_group('sig', operation_group='shared_galleries', min_api='2022-01-03') as g: g.custom_command('list-community', 'sig_community_gallery_list') - with self.command_group('sig image-definition', compute_gallery_images_sdk, operation_group='gallery_images', min_api='2018-06-01') as g: + with self.command_group('sig image-definition', operation_group='gallery_images') as g: g.custom_command('create', 'create_gallery_image') from .operations.sig_image_definition import SigImageDefinitionUpdate self.command_table['sig image-definition update'] = SigImageDefinitionUpdate(loader=self) @@ -478,24 +400,19 @@ def load_command_table(self, _): from .operations.sig_image_definition import SigImageDefinitionListShared self.command_table['sig image-definition list-shared'] = SigImageDefinitionListShared(loader=self) - with self.command_group('sig image-version', compute_gallery_image_versions_sdk, operation_group='gallery_image_versions', min_api='2018-06-01') as g: + with self.command_group('sig image-version', operation_group='gallery_image_versions') as g: g.custom_command('create', 'create_image_version', supports_no_wait=True, validator=process_image_version_create_namespace) g.custom_command('undelete', 'undelete_image_version', supports_no_wait=True, validator=process_image_version_undelete_namespace, is_preview=True) g.generic_update_command('update', getter_name='get_image_version_to_update', setter_arg_name='gallery_image_version', setter_name='update_image_version', setter_type=compute_custom, command_type=compute_custom, supports_no_wait=True, validator=process_image_version_update_namespace) from .aaz.latest.sig.image_version import Show as SigImageVersionShow self.command_table['sig image-version show'] = SigImageVersionShow(loader=self, table_transformer='{Name:name, ResourceGroup:resourceGroup, ProvisioningState:provisioningState, TargetRegions: publishingProfile.targetRegions && join(`, `, publishingProfile.targetRegions[*].name), EdgeZones: publishingProfile.targetExtendedLocations && join(`, `, publishingProfile.targetExtendedLocations[*].name), ReplicationState:replicationStatus.aggregatedState}') + with self.command_group('sig image-version'): from .operations.sig_image_version import SigImageVersionListShared self.command_table['sig image-version list-shared'] = SigImageVersionListShared(loader=self) - vm_gallery_sharing_profile = CliCommandType( - operations_tmpl=( - 'azure.mgmt.compute.operations._gallery_sharing_profile_operations#GallerySharingProfileOperations.{}' - ), - operation_group='shared_galleries' - ) - with self.command_group('sig share', vm_gallery_sharing_profile, operation_group='shared_galleries'): + with self.command_group('sig share', operation_group='shared_galleries'): from .operations.sig_share import SigShareAdd, SigShareRemove, SigShareReset, SigShareEnableCommunity, SigShareWait self.command_table['sig share add'] = SigShareAdd(loader=self) self.command_table['sig share remove'] = SigShareRemove(loader=self) @@ -503,11 +420,11 @@ def load_command_table(self, _): self.command_table['sig share enable-community'] = SigShareEnableCommunity(loader=self) self.command_table['sig share wait'] = SigShareWait(loader=self) - with self.command_group('sig gallery-application', compute_gallery_application_profile, operation_group='gallery_applications') as g: + with self.command_group('sig gallery-application', operation_group='gallery_applications') as g: from .operations.sig_gallery_application import SigGalleryApplicationCreate self.command_table['sig gallery-application create'] = SigGalleryApplicationCreate(loader=self) - with self.command_group('sig gallery-application version', compute_gallery_application_version_profile, operation_group='gallery_application_versions'): + with self.command_group('sig gallery-application version', operation_group='gallery_application_versions'): from .operations.sig_gallery_application_version import SigGalleryApplicationVersionCreate, SiggalleryApplicationversionUpdate self.command_table['sig gallery-application version create'] = SigGalleryApplicationVersionCreate(loader=self) self.command_table['sig gallery-application version update'] = SiggalleryApplicationversionUpdate(loader=self) @@ -517,7 +434,7 @@ def load_command_table(self, _): self.command_table['sig in-vm-access-control-profile-version create'] = SigInVMAccessControlProfileVersionCreate(loader=self) self.command_table['sig in-vm-access-control-profile-version update'] = SigInVMAccessControlProfileVersionUpdate(loader=self) - with self.command_group('ppg', compute_proximity_placement_groups_sdk, min_api='2018-04-01', client_factory=cf_proximity_placement_groups) as g: + with self.command_group('ppg') as g: from .operations.ppg import PPGCreate, PPGUpdate self.command_table['ppg create'] = PPGCreate(loader=self) self.command_table['ppg update'] = PPGUpdate(loader=self) @@ -544,16 +461,15 @@ def load_command_table(self, _): from .operations.capacity_reservation_group import CapacityReservationGroupList self.command_table['capacity reservation group list'] = CapacityReservationGroupList(loader=self) - with self.command_group('capacity reservation', capacity_reservations_sdk, min_api='2021-04-01', - client_factory=cf_capacity_reservations) as g: + with self.command_group('capacity reservation') as g: from .operations.capacity_reservation import CapacityReservationUpdate, CapacityReservationShow self.command_table['capacity reservation update'] = CapacityReservationUpdate(loader=self) self.command_table['capacity reservation show'] = CapacityReservationShow(loader=self) - with self.command_group('restore-point', restore_point) as g: + with self.command_group('restore-point') as g: g.custom_show_command('show', 'restore_point_show') g.custom_command('create', 'restore_point_create', supports_no_wait=True) g.wait_command('wait') - with self.command_group('restore-point collection', restore_point_collection) as g: + with self.command_group('restore-point collection') as g: g.custom_show_command('show', 'restore_point_collection_show') 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 6c6f602148b..5a93566c435 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -226,53 +226,6 @@ def _get_sku_object(cmd, sku): return sku -def get_hyper_v_generation_from_vmss(cli_ctx, image_ref, location): # pylint: disable=too-many-return-statements - from ._vm_utils import (is_valid_image_version_id, parse_gallery_image_id, is_valid_vm_image_id, parse_vm_image_id, - parse_shared_gallery_image_id, parse_community_gallery_image_id) - if image_ref is None: - return None - if image_ref.id: - from ._client_factory import _compute_client_factory - if is_valid_image_version_id(image_ref.id): - image_info = parse_gallery_image_id(image_ref.id) - client = _compute_client_factory(cli_ctx, subscription_id=image_info[0]).gallery_images - gallery_image_info = client.get( - resource_group_name=image_info[1], gallery_name=image_info[2], gallery_image_name=image_info[3]) - return gallery_image_info.hyper_v_generation if hasattr(gallery_image_info, 'hyper_v_generation') else None - if is_valid_vm_image_id(image_ref.id): - sub, rg, image_name = parse_vm_image_id(image_ref.id) - client = _compute_client_factory(cli_ctx, subscription_id=sub).images - image_info = client.get(rg, image_name) - return image_info.hyper_v_generation if hasattr(image_info, 'hyper_v_generation') else None - - if image_ref.shared_gallery_image_id is not None: - from ._client_factory import cf_shared_gallery_image - image_info = parse_shared_gallery_image_id(image_ref.shared_gallery_image_id) - gallery_image_info = cf_shared_gallery_image(cli_ctx).get( - location=location, gallery_unique_name=image_info[0], gallery_image_name=image_info[1]) - return gallery_image_info.hyper_v_generation if hasattr(gallery_image_info, 'hyper_v_generation') else None - - if image_ref.community_gallery_image_id is not None: - from ._client_factory import cf_community_gallery_image - image_info = parse_community_gallery_image_id(image_ref.community_gallery_image_id) - gallery_image_info = cf_community_gallery_image(cli_ctx).get( - location=location, public_gallery_name=image_info[0], gallery_image_name=image_info[1]) - return gallery_image_info.hyper_v_generation if hasattr(gallery_image_info, 'hyper_v_generation') else None - - if image_ref.offer and image_ref.publisher and image_ref.sku and image_ref.version: - from ._client_factory import cf_vm_image - version = image_ref.version - if version.lower() == 'latest': - from ._actions import _get_latest_image_version - version = _get_latest_image_version(cli_ctx, location, image_ref.publisher, image_ref.offer, - image_ref.sku) - vm_image_info = cf_vm_image(cli_ctx, '').get( - location, image_ref.publisher, image_ref.offer, image_ref.sku, version) - return vm_image_info.hyper_v_generation if hasattr(vm_image_info, 'hyper_v_generation') else None - - return None - - def get_hyper_v_generation_from_vmss_by_aaz(cli_ctx, image_ref, location): # pylint: disable=too-many-return-statements from ._vm_utils import (is_valid_image_version_id, parse_gallery_image_id, is_valid_vm_image_id, parse_vm_image_id, parse_shared_gallery_image_id, parse_community_gallery_image_id) @@ -2617,10 +2570,8 @@ def set_extension(cmd, resource_group_name, vm_name, vm_extension_name, publishe # region VirtualMachines Extension Images -def list_vm_extension_images( - cmd, image_location=None, publisher_name=None, name=None, version=None, latest=False): - return load_extension_images_thru_services( - cmd.cli_ctx, publisher_name, name, version, image_location, latest) +def list_vm_extension_images(cmd, image_location=None, publisher_name=None, name=None, version=None, latest=False): + return load_extension_images_thru_services(cmd.cli_ctx, publisher_name, name, version, image_location, latest) # endregion @@ -4275,23 +4226,6 @@ def get_vmss_by_aaz(cmd, resource_group_name, name, instance_id=None, include_us return VMSSShow(cli_ctx=cmd.cli_ctx)(command_args=command_args) -def _check_vmss_hyper_v_generation(cli_ctx, vmss): - hyper_v_generation = get_hyper_v_generation_from_vmss( - cli_ctx, vmss.virtual_machine_profile.storage_profile.image_reference, vmss.location) - security_profile = vmss.virtual_machine_profile.security_profile - security_type = security_profile.security_type if security_profile else None - - if hyper_v_generation == "V1" or (hyper_v_generation == "V2" and security_type is None): - logger.warning("Trusted Launch security type is supported on Hyper-V Generation 2 OS Images. " - "To know more please visit " - "https://learn.microsoft.com/en-us/azure/virtual-machines/trusted-launch") - elif hyper_v_generation == "V2" and security_type == "ConfidentialVM": - from azure.cli.core.azclierror import InvalidArgumentValueError - raise InvalidArgumentValueError("{} is already configured with {}. " - "Security Configuration cannot be updated from ConfidentialVM to " - "TrustedLaunch.".format(vmss.name, security_type)) - - def _check_vmss_hyper_v_generation_by_aaz(cli_ctx, vmss): hyper_v_generation = get_hyper_v_generation_from_vmss_by_aaz( cli_ctx, vmss.get("virtualMachineProfile", {}).get("storageProfile", {}).get("imageReference", {}), vmss["location"]) # pylint: disable=line-too-long @@ -4309,26 +4243,6 @@ def _check_vmss_hyper_v_generation_by_aaz(cli_ctx, vmss): "TrustedLaunch.".format(vmss["name"], security_type)) -def get_vmss_modified(cmd, resource_group_name, name, instance_id=None, security_type=None): - client = _compute_client_factory(cmd.cli_ctx) - if instance_id is not None: - vms = client.virtual_machine_scale_set_vms.get(resource_group_name=resource_group_name, - vm_scale_set_name=name, instance_id=instance_id) - # To avoid unnecessary permission check of image - if hasattr(vms, "storage_profile") and vms.storage_profile: - vms.storage_profile.image_reference = None - return vms - - vmss = client.virtual_machine_scale_sets.get(resource_group_name, name) - if security_type == 'TrustedLaunch': - _check_vmss_hyper_v_generation(cmd.cli_ctx, vmss) - # To avoid unnecessary permission check of image - if hasattr(vmss, "virtual_machine_profile") and vmss.virtual_machine_profile \ - and vmss.virtual_machine_profile.storage_profile: - vmss.virtual_machine_profile.storage_profile.image_reference = None - return vmss - - def get_vmss_modified_by_aaz(cmd, resource_group_name, name, instance_id=None, security_type=None): if instance_id is not None: from .operations.vmss_vms import VMSSVMSShow From 258d3bf35ab779bcb64e5825d38ef212acd8d017 Mon Sep 17 00:00:00 2001 From: william051200 Date: Mon, 9 Mar 2026 08:51:47 +0800 Subject: [PATCH 2/7] Update code --- 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 0c57c9c7ae9..d9dbe5364f0 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -325,6 +325,7 @@ def load_command_table(self, _): g.custom_command('identity remove', 'remove_vmss_identity', validator=process_remove_identity_namespace, is_preview=True) g.custom_show_command('identity show', 'show_vmss_identity') g.custom_command('deallocate', 'deallocate_vmss', supports_no_wait=True) + g.custom_command('get-instance-view', 'get_vmss_instance_view', table_transformer='{ProvisioningState:statuses[0].displayStatus, PowerState:statuses[1].displayStatus}') g.custom_command('list-instance-connection-info', 'list_vmss_instance_connection_info') g.custom_command('list-instance-public-ips', 'list_vmss_instance_public_ips') g.custom_command('list-instances', 'get_instances_list') @@ -338,7 +339,6 @@ def load_command_table(self, _): with self.command_group('vmss', compute_vmss_sdk, operation_group='virtual_machine_scale_sets') as g: g.custom_command('create', 'create_vmss', transform=DeploymentOutputLongRunningOperation(self.cli_ctx, 'Starting vmss create'), supports_no_wait=True, table_transformer=deployment_validate_table_format, validator=process_vmss_create_namespace, exception_handler=handle_template_based_exception) - g.custom_command('get-instance-view', 'get_vmss_instance_view', table_transformer='{ProvisioningState:statuses[0].displayStatus, PowerState:statuses[1].displayStatus}') g.custom_show_command('show', 'get_vmss', table_transformer=get_vmss_table_output_transformer(self, False)) g.custom_command('stop', 'stop_vmss', supports_no_wait=True, validator=process_vm_vmss_stop) g.generic_update_command('update', getter_name='get_vmss_modified_by_aaz', setter_name='update_vmss', supports_no_wait=True, command_type=compute_custom, validator=validate_vmss_update_namespace) From 39177245a1cefd6fdd97fa78e3efcdcb977b58bd Mon Sep 17 00:00:00 2001 From: william051200 Date: Thu, 30 Apr 2026 11:58:24 +0800 Subject: [PATCH 3/7] Update code --- .../azure/cli/command_modules/vm/commands.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) 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 417099cd133..8727ed93afb 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -3,16 +3,8 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- -from azure.cli.command_modules.vm._client_factory import (cf_vm, - cf_vm_ext, cf_vm_ext_image, - cf_vm_image_term, cf_usage, - cf_vmss, - cf_gallery_images, cf_gallery_image_versions, - cf_proximity_placement_groups, - cf_dedicated_hosts, - cf_vm_image, cf_vm_image_term, cf_usage, - cf_vmss, cf_images, - cf_dedicated_hosts, cf_dedicated_host_groups, +from azure.cli.command_modules.vm._client_factory import (cf_vm, cf_vm_image_term, cf_usage, + cf_vmss, cf_dedicated_hosts, cf_log_analytics_data_plane, cf_capacity_reservation_groups) from azure.cli.command_modules.vm._format import ( From a1fd9f785d5ae51e3a81623bca211d4cccf098c0 Mon Sep 17 00:00:00 2001 From: william051200 Date: Thu, 30 Apr 2026 12:01:37 +0800 Subject: [PATCH 4/7] Update code --- .../azure/cli/command_modules/vm/commands.py | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) 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 8727ed93afb..f7c1402a65e 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -309,14 +309,11 @@ def load_command_table(self, _): transform=DeploymentOutputLongRunningOperation(self.cli_ctx, 'Starting vmss create'), supports_no_wait=True, table_transformer=deployment_validate_table_format, validator=process_vmss_create_namespace, exception_handler=handle_template_based_exception) - g.custom_show_command('show', 'get_vmss_by_aaz', table_transformer=get_vmss_table_output_transformer(self, False)) - g.generic_update_command('update', getter_name='get_vmss_modified_by_aaz', setter_name='update_vmss', supports_no_wait=True, command_type=compute_custom, validator=validate_vmss_update_namespace) - g.wait_command('wait', getter_name='get_vmss_by_aaz', getter_type=compute_custom) + g.custom_command('deallocate', 'deallocate_vmss', supports_no_wait=True) + g.custom_command('get-instance-view', 'get_vmss_instance_view', table_transformer='{ProvisioningState:statuses[0].displayStatus, PowerState:statuses[1].displayStatus}') g.custom_command('identity assign', 'assign_vmss_identity', validator=process_assign_identity_namespace) g.custom_command('identity remove', 'remove_vmss_identity', validator=process_remove_identity_namespace, is_preview=True) g.custom_show_command('identity show', 'show_vmss_identity') - g.custom_command('deallocate', 'deallocate_vmss', supports_no_wait=True) - g.custom_command('get-instance-view', 'get_vmss_instance_view', table_transformer='{ProvisioningState:statuses[0].displayStatus, PowerState:statuses[1].displayStatus}') g.custom_command('list-instance-connection-info', 'list_vmss_instance_connection_info') g.custom_command('list-instance-public-ips', 'list_vmss_instance_public_ips') g.custom_command('list-instances', 'get_instances_list') @@ -324,19 +321,11 @@ def load_command_table(self, _): g.custom_command('restart', 'restart_vmss', supports_no_wait=True) g.custom_command('scale', 'scale_vmss', supports_no_wait=True) g.custom_command('set-orchestration-service-state', 'set_orchestration_service_state', supports_no_wait=True) - g.custom_command('stop', 'stop_vmss', supports_no_wait=True, validator=process_vm_vmss_stop) - - with self.command_group('vmss application', operation_group='virtual_machine_scale_sets') as g: - g.custom_command('set', 'set_vmss_applications', validator=process_set_applications_namespace) - g.custom_command('list', 'list_vmss_applications') - - with self.command_group('vmss', compute_vmss_sdk, operation_group='virtual_machine_scale_sets') as g: - g.custom_command('get-instance-view', 'get_vmss_instance_view', table_transformer='{ProvisioningState:statuses[0].displayStatus, PowerState:statuses[1].displayStatus}') - g.custom_command('create', 'create_vmss', transform=DeploymentOutputLongRunningOperation(self.cli_ctx, 'Starting vmss create'), supports_no_wait=True, table_transformer=deployment_validate_table_format, validator=process_vmss_create_namespace, exception_handler=handle_template_based_exception) - g.custom_show_command('show', 'get_vmss', table_transformer=get_vmss_table_output_transformer(self, False)) + g.custom_show_command('show', 'get_vmss_by_aaz', table_transformer=get_vmss_table_output_transformer(self, False)) g.custom_command('stop', 'stop_vmss', supports_no_wait=True, validator=process_vm_vmss_stop) g.generic_update_command('update', getter_name='get_vmss_modified_by_aaz', setter_name='update_vmss', supports_no_wait=True, command_type=compute_custom, validator=validate_vmss_update_namespace) g.custom_command('update-instances', 'update_vmss_instances', supports_no_wait=True) + g.wait_command('wait', getter_name='get_vmss_by_aaz', getter_type=compute_custom) from .aaz.latest.vmss import List as VMSSList self.command_table['vmss list'] = VMSSList(loader=self, @@ -345,6 +334,10 @@ def load_command_table(self, _): from .operations.vmss_vms import VMSSGetResiliencyView self.command_table['vmss get-resiliency-view'] = VMSSGetResiliencyView(loader=self) + with self.command_group('vmss application', operation_group='virtual_machine_scale_sets') as g: + g.custom_command('set', 'set_vmss_applications', validator=process_set_applications_namespace) + g.custom_command('list', 'list_vmss_applications') + with self.command_group('vmss diagnostics') as g: g.custom_command('set', 'set_vmss_diagnostics_extension') g.custom_command('get-default-config', 'show_default_diagnostics_configuration') From 3d20a9854a042776f80d9a53e5c5fac5612b172c Mon Sep 17 00:00:00 2001 From: william051200 Date: Fri, 8 May 2026 15:27:32 +0800 Subject: [PATCH 5/7] Clean up code --- .../cli/command_modules/vm/_client_factory.py | 22 -------------- .../azure/cli/command_modules/vm/commands.py | 29 ++----------------- 2 files changed, 2 insertions(+), 49 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py b/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py index 67363c6ec7b..d888521caf9 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py @@ -12,14 +12,6 @@ def _compute_client_factory(cli_ctx, **kwargs): aux_subscriptions=kwargs.get('aux_subscriptions')) -def cf_vm(cli_ctx, _): - return _compute_client_factory(cli_ctx).virtual_machines - - -def cf_vm_image(cli_ctx, _): - return _compute_client_factory(cli_ctx).virtual_machine_images - - def cf_vm_image_term(cli_ctx, _): from azure.cli.core.commands.client_factory import get_mgmt_service_client from azure.mgmt.marketplaceordering import MarketplaceOrderingAgreements @@ -31,10 +23,6 @@ def cf_vmss(cli_ctx, _): return _compute_client_factory(cli_ctx).virtual_machine_scale_sets -def cf_dedicated_hosts(cli_ctx, _): - return _compute_client_factory(cli_ctx).dedicated_hosts - - def _log_analytics_client_factory(cli_ctx, subscription_id, *_): from azure.mgmt.loganalytics import LogAnalyticsManagementClient from azure.cli.core.commands.client_factory import get_mgmt_service_client @@ -67,15 +55,5 @@ def cf_vm_cl(cli_ctx, *_): ComputeManagementClient) -def cf_shared_gallery_image(cli_ctx, *_): - return cf_vm_cl(cli_ctx).shared_gallery_images - - def cf_community_gallery(cli_ctx, *_): return cf_vm_cl(cli_ctx).community_galleries - - -def cf_community_gallery_image(cli_ctx, *_): - return cf_vm_cl(cli_ctx).community_gallery_images - - 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 2d5b2f4b74d..a7b371bc09a 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -3,12 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- -from azure.cli.command_modules.vm._client_factory import (cf_vm, - cf_vm_image_term, - cf_vmss, - cf_dedicated_hosts, - cf_log_analytics_data_plane, - cf_community_gallery) +from azure.cli.command_modules.vm._client_factory import (cf_vm_image_term, cf_vmss, cf_log_analytics_data_plane) from azure.cli.command_modules.vm._format import ( transform_ip_addresses, transform_vm, transform_vm_create_output, transform_vm_list, transform_disk_create_table_output, transform_sku_for_table_output, transform_disk_show_table_output, @@ -53,32 +48,17 @@ def load_command_table(self, _): client_factory=image_builder_client_factory ) - compute_vm_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#VirtualMachinesOperations.{}', - client_factory=cf_vm - ) - compute_vm_image_term_sdk = CliCommandType( operations_tmpl='azure.mgmt.marketplaceordering.operations#MarketplaceAgreementsOperations.{}', client_factory=cf_vm_image_term ) - compute_vm_usage_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#UsageOperations.{}', - client_factory=cf_usage - ) - compute_vmss_sdk = CliCommandType( operations_tmpl='azure.mgmt.compute.operations#VirtualMachineScaleSetsOperations.{}', client_factory=cf_vmss, operation_group='virtual_machine_scale_sets' ) - compute_dedicated_host_sdk = CliCommandType( - operations_tmpl="azure.mgmt.compute.operations#DedicatedHostsOperations.{}", - client_factory=cf_dedicated_hosts, - ) - image_builder_image_templates_sdk = CliCommandType( operations_tmpl="azure.mgmt.imagebuilder.operations#VirtualMachineImageTemplatesOperations.{}", client_factory=cf_img_bldr_image_templates, @@ -97,11 +77,6 @@ def load_command_table(self, _): exception_handler=monitor_exception_handler ) - capacity_reservation_groups_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#CapacityReservationGroupsOperations.{}', - client_factory=cf_capacity_reservation_groups - ) - with self.command_group("ppg"): from .operations.ppg import PPGShow self.command_table["ppg show"] = PPGShow(loader=self) @@ -230,7 +205,7 @@ def load_command_table(self, _): g.custom_command('set', 'set_diagnostics_extension') g.custom_command('get-default-config', 'show_default_diagnostics_configuration') - with self.command_group('vm disk', compute_vm_sdk, min_api='2017-03-30') as g: + with self.command_group('vm disk', min_api='2017-03-30') as g: g.custom_command('attach', 'attach_managed_data_disk', validator=process_vm_disk_attach_namespace) g.custom_command('detach', 'detach_managed_data_disk', validator=process_vm_disk_detach_namespace) From b94da509fe6732463568f3d6cebd3bea7cf62b7e Mon Sep 17 00:00:00 2001 From: william051200 Date: Wed, 13 May 2026 11:42:16 +0800 Subject: [PATCH 6/7] Clean up code --- .../azure/cli/command_modules/vm/commands.py | 3 +- .../azure/cli/command_modules/vm/custom.py | 77 +------------------ 2 files changed, 3 insertions(+), 77 deletions(-) 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 a7b371bc09a..e6bcab8b23f 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -3,7 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- -from azure.cli.command_modules.vm._client_factory import (cf_vm_image_term, cf_vmss, cf_log_analytics_data_plane) +from azure.cli.command_modules.vm._client_factory import cf_vm_image_term, cf_vmss, cf_log_analytics_data_plane from azure.cli.command_modules.vm._format import ( transform_ip_addresses, transform_vm, transform_vm_create_output, transform_vm_list, transform_disk_create_table_output, transform_sku_for_table_output, transform_disk_show_table_output, @@ -432,7 +432,6 @@ def load_command_table(self, _): with self.command_group('restore-point') as g: g.custom_show_command('show', 'restore_point_show') g.custom_command('create', 'restore_point_create', supports_no_wait=True) - g.wait_command('wait') with self.command_group('restore-point collection') as g: g.custom_show_command('show', 'restore_point_collection_show') 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 753c487dbab..0bd6e024e0d 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -42,7 +42,7 @@ from ._actions import (load_images_from_aliases_doc, load_extension_images_thru_services, load_images_thru_services, _get_latest_image_version_by_aaz) -from ._client_factory import (_compute_client_factory, cf_vm_image_term) +from ._client_factory import _compute_client_factory, cf_vm_image_term from .aaz.latest.vm.disk import AttachDetachDataDisk from .aaz.latest.vm import Update as UpdateVM @@ -890,19 +890,6 @@ def setter(vm, external_identities=external_identities): # region VirtualMachines -def capture_vm(cmd, resource_group_name, vm_name, vhd_name_prefix, - storage_container='vhds', overwrite=True): - VirtualMachineCaptureParameters = cmd.get_models('VirtualMachineCaptureParameters') - client = _compute_client_factory(cmd.cli_ctx) - parameter = VirtualMachineCaptureParameters(vhd_prefix=vhd_name_prefix, - destination_container_name=storage_container, - overwrite_vhds=overwrite) - poller = client.virtual_machines.begin_capture(resource_group_name, vm_name, parameter) - result = LongRunningOperation(cmd.cli_ctx)(poller) - output = getattr(result, 'output', None) or result.resources[0] - print(json.dumps(output, indent=2)) # pylint: disable=no-member - - # pylint: disable=too-many-locals, unused-argument, too-many-statements, too-many-branches, broad-except def create_vm(cmd, vm_name, resource_group_name, image=None, size='Standard_D2s_v5', location=None, tags=None, no_wait=False, authentication_type=None, admin_password=None, computer_name=None, @@ -1392,19 +1379,6 @@ def get_vm_by_aaz(cmd, resource_group_name, vm_name, expand=None): return VMShow(cli_ctx=cmd.cli_ctx)(command_args=command_args) -def get_vm(cmd, resource_group_name, vm_name, expand=None): - client = _compute_client_factory(cmd.cli_ctx) - return client.virtual_machines.get(resource_group_name, vm_name, expand=expand) - - -def get_vm_to_update(cmd, resource_group_name, vm_name): - client = _compute_client_factory(cmd.cli_ctx) - vm = client.virtual_machines.get(resource_group_name, vm_name) - # To avoid unnecessary permission check of image - vm.storage_profile.image_reference = None - return vm - - def get_vm_to_update_by_aaz(cmd, resource_group_name, vm_name): from .operations.vm import VMShow @@ -1742,20 +1716,6 @@ def stop_vm(cmd, resource_group_name, vm_name, no_wait=False, skip_shutdown=Fals return VMStop(cli_ctx=cmd.cli_ctx)(command_args=command_args) -def set_vm(cmd, instance, lro_operation=None, no_wait=False): - instance.resources = None # Issue: https://github.com/Azure/autorest/issues/934 - client = _compute_client_factory(cmd.cli_ctx) - parsed_id = _parse_rg_name(instance.id) - poller = sdk_no_wait(no_wait, client.virtual_machines.begin_create_or_update, - resource_group_name=parsed_id[0], - vm_name=parsed_id[1], - parameters=instance) - if lro_operation: - return lro_operation(poller) - - return LongRunningOperation(cmd.cli_ctx)(poller) - - # Notes: vm format is in snake_case def set_vm_by_aaz(cmd, vm, no_wait=False): from .aaz.latest.vm import Create as _VMCreate @@ -1784,19 +1744,6 @@ def _output(self, *args, **kwargs): return vm -def patch_vm(cmd, resource_group_name, vm_name, vm): - client = _compute_client_factory(cmd.cli_ctx) - poller = client.virtual_machines.begin_update(resource_group_name, vm_name, vm) - return LongRunningOperation(cmd.cli_ctx)(poller) - - -def patch_disk_encryption_set(cmd, resource_group_name, disk_encryption_set_name, disk_encryption_set_update): - client = _compute_client_factory(cmd.cli_ctx) - poller = client.disk_encryption_sets.begin_update(resource_group_name, disk_encryption_set_name, - disk_encryption_set_update) - return LongRunningOperation(cmd.cli_ctx)(poller) - - def show_vm(cmd, resource_group_name, vm_name, show_details=False, include_user_data=False): if show_details: return get_vm_details(cmd, resource_group_name, vm_name, include_user_data) @@ -4280,26 +4227,6 @@ def deallocate_vmss(cmd, resource_group_name, vm_scale_set_name, instance_ids=No return VmssDeallocate(cli_ctx=cmd.cli_ctx)(command_args=command_args) -def get_vmss(cmd, resource_group_name, name, instance_id=None, include_user_data=False): - client = _compute_client_factory(cmd.cli_ctx) - - expand = None - if include_user_data: - expand = 'userData' - - if instance_id is not None: - if cmd.supported_api_version(min_api='2020-12-01', operation_group='virtual_machine_scale_sets'): - return client.virtual_machine_scale_set_vms.get(resource_group_name=resource_group_name, - vm_scale_set_name=name, instance_id=instance_id, - expand=expand) - return client.virtual_machine_scale_set_vms.get(resource_group_name=resource_group_name, - vm_scale_set_name=name, instance_id=instance_id) - - if cmd.supported_api_version(min_api='2021-03-01', operation_group='virtual_machine_scale_sets'): - return client.virtual_machine_scale_sets.get(resource_group_name, name, expand=expand) - return client.virtual_machine_scale_sets.get(resource_group_name, name) - - def get_vmss_by_aaz(cmd, resource_group_name, name, instance_id=None, include_user_data=False): from .operations.vmss import VMSSShow from .operations.vmss_vms import VMSSVMSShow @@ -5917,7 +5844,7 @@ def undelete_image_version(cmd, resource_group_name, gallery_name, gallery_image def fix_gallery_image_date_info(date_info): - # here we add needed time, if only date is provided, so the setting can be accepted by servie end + # here we add needed time, if only date is provided, so the setting can be accepted by service end if date_info and 't' not in date_info.lower(): date_info += 'T12:59:59Z' return date_info From e381f267371925d7e804c6ba136582df742caa4e Mon Sep 17 00:00:00 2001 From: william051200 Date: Fri, 15 May 2026 09:37:06 +0800 Subject: [PATCH 7/7] Clean up code --- .../azure/cli/command_modules/vm/_client_factory.py | 4 ---- .../azure/cli/command_modules/vm/commands.py | 12 +++--------- src/azure-cli/azure/cli/command_modules/vm/custom.py | 7 ++----- 3 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py b/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py index d888521caf9..ab8afe696f0 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_client_factory.py @@ -19,10 +19,6 @@ def cf_vm_image_term(cli_ctx, _): return market_place_client.marketplace_agreements -def cf_vmss(cli_ctx, _): - return _compute_client_factory(cli_ctx).virtual_machine_scale_sets - - def _log_analytics_client_factory(cli_ctx, subscription_id, *_): from azure.mgmt.loganalytics import LogAnalyticsManagementClient from azure.cli.core.commands.client_factory import get_mgmt_service_client 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 e6bcab8b23f..5253c3d39bf 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -3,7 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- -from azure.cli.command_modules.vm._client_factory import cf_vm_image_term, cf_vmss, cf_log_analytics_data_plane +from azure.cli.command_modules.vm._client_factory import cf_vm_image_term, cf_log_analytics_data_plane from azure.cli.command_modules.vm._format import ( transform_ip_addresses, transform_vm, transform_vm_create_output, transform_vm_list, transform_disk_create_table_output, transform_sku_for_table_output, transform_disk_show_table_output, @@ -53,12 +53,6 @@ def load_command_table(self, _): client_factory=cf_vm_image_term ) - compute_vmss_sdk = CliCommandType( - operations_tmpl='azure.mgmt.compute.operations#VirtualMachineScaleSetsOperations.{}', - client_factory=cf_vmss, - operation_group='virtual_machine_scale_sets' - ) - image_builder_image_templates_sdk = CliCommandType( operations_tmpl="azure.mgmt.imagebuilder.operations#VirtualMachineImageTemplatesOperations.{}", client_factory=cf_img_bldr_image_templates, @@ -309,7 +303,7 @@ def load_command_table(self, _): from .operations.vmss_vms import VMSSGetResiliencyView self.command_table['vmss get-resiliency-view'] = VMSSGetResiliencyView(loader=self) - with self.command_group('vmss application', operation_group='virtual_machine_scale_sets') as g: + with self.command_group('vmss application') as g: g.custom_command('set', 'set_vmss_applications', validator=process_set_applications_namespace) g.custom_command('list', 'list_vmss_applications') @@ -326,7 +320,7 @@ def load_command_table(self, _): g.custom_command('disable', 'decrypt_vmss') g.custom_show_command('show', 'show_vmss_encryption_status') - with self.command_group('vmss extension', compute_vmss_sdk) as g: + with self.command_group('vmss extension') as g: g.custom_command('delete', 'delete_vmss_extension', supports_no_wait=True) g.custom_show_command('show', 'get_vmss_extension') g.custom_command('set', 'set_vmss_extension', supports_no_wait=True) 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 0bd6e024e0d..ff4e8c7e4e4 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -33,7 +33,7 @@ from azure.cli.core.commands.validators import validate_file_or_dict from azure.cli.core.commands import LongRunningOperation, DeploymentOutputLongRunningOperation -from azure.cli.core.commands.client_factory import get_mgmt_service_client +from azure.cli.core.commands.client_factory import get_mgmt_service_client, get_subscription_id from azure.cli.core.profiles import ResourceType from azure.cli.core.util import sdk_no_wait @@ -42,7 +42,7 @@ from ._actions import (load_images_from_aliases_doc, load_extension_images_thru_services, load_images_thru_services, _get_latest_image_version_by_aaz) -from ._client_factory import _compute_client_factory, cf_vm_image_term +from ._client_factory import _compute_client_factory, cf_log_analytics, cf_log_analytics_data_sources, cf_vm_image_term from .aaz.latest.vm.disk import AttachDetachDataDisk from .aaz.latest.vm import Update as UpdateVM @@ -434,7 +434,6 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p supported_security_option=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 security_data_uri: @@ -6008,7 +6007,6 @@ def update_dedicated_host(cmd, host_group_name, host_name, resource_group_name, # region VMMonitor def _get_log_analytics_client(cmd): - from ._client_factory import cf_log_analytics from azure.cli.core.commands.client_factory import get_subscription_id subscription_id = get_subscription_id(cmd.cli_ctx) return cf_log_analytics(cmd.cli_ctx, subscription_id) @@ -6045,7 +6043,6 @@ def _prepare_workspace(cmd, resource_group_name, workspace): def _set_data_source_for_workspace(cmd, os_type, resource_group_name, workspace_name): - from ._client_factory import cf_log_analytics_data_sources from azure.cli.core.commands.client_factory import get_subscription_id from azure.mgmt.loganalytics.models import DataSource from azure.core.exceptions import HttpResponseError