From 21a7fd5158327ca8e0f3938f6fcc1305340c562d Mon Sep 17 00:00:00 2001 From: william051200 Date: Mon, 16 Feb 2026 08:27:04 +0800 Subject: [PATCH 1/2] Generate vmss stop code --- .../vm/aaz/latest/vmss/__init__.py | 1 + .../vm/aaz/latest/vmss/_stop.py | 187 ++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/_stop.py diff --git a/src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/__init__.py b/src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/__init__.py index 06ac4aa5ab4..7794bda7b1d 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/__init__.py +++ b/src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/__init__.py @@ -22,6 +22,7 @@ from ._show import * from ._simulate_eviction import * from ._start import * +from ._stop import * from ._update import * from ._update_domain_walk import * from ._wait import * diff --git a/src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/_stop.py b/src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/_stop.py new file mode 100644 index 00000000000..7650fe6dd37 --- /dev/null +++ b/src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/_stop.py @@ -0,0 +1,187 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# +# Code generated by aaz-dev-tools +# -------------------------------------------------------------------------------------------- + +# pylint: skip-file +# flake8: noqa + +from azure.cli.core.aaz import * + + +class Stop(AAZCommand): + """Power off (stop) one or more virtual machines in a VM scale set. Note that resources are still attached and you are getting charged for the resources. Instead, use deallocate to release resources and avoid charges. + """ + + _aaz_info = { + "version": "2024-11-01", + "resources": [ + ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.compute/virtualmachinescalesets/{}/poweroff", "2024-11-01"], + ] + } + + AZ_SUPPORT_NO_WAIT = True + + def _handler(self, command_args): + super()._handler(command_args) + return self.build_lro_poller(self._execute_operations, None) + + _args_schema = None + + @classmethod + def _build_arguments_schema(cls, *args, **kwargs): + if cls._args_schema is not None: + return cls._args_schema + cls._args_schema = super()._build_arguments_schema(*args, **kwargs) + + # define Arg Group "" + + _args_schema = cls._args_schema + _args_schema.resource_group = AAZResourceGroupNameArg( + required=True, + ) + _args_schema.vm_scale_set_name = AAZStrArg( + options=["--vm-scale-set-name"], + help="The name of the VM scale set.", + required=True, + id_part="name", + ) + _args_schema.skip_shutdown = AAZBoolArg( + options=["--skip-shutdown"], + help="The parameter to request non-graceful VM shutdown. True value for this flag indicates non-graceful shutdown whereas false indicates otherwise. Default value for this flag is false if not specified", + ) + + # define Arg Group "VmInstanceIDs" + + _args_schema = cls._args_schema + _args_schema.instance_ids = AAZListArg( + options=["--instance-ids"], + arg_group="VmInstanceIDs", + help="The virtual machine scale set instance ids. Omitting the virtual machine scale set instance ids will result in the operation being performed on all virtual machines in the virtual machine scale set.", + ) + + instance_ids = cls._args_schema.instance_ids + instance_ids.Element = AAZStrArg() + return cls._args_schema + + def _execute_operations(self): + self.pre_operations() + yield self.VirtualMachineScaleSetsPowerOff(ctx=self.ctx)() + self.post_operations() + + @register_callback + def pre_operations(self): + pass + + @register_callback + def post_operations(self): + pass + + class VirtualMachineScaleSetsPowerOff(AAZHttpOperation): + CLIENT_TYPE = "MgmtClient" + + def __call__(self, *args, **kwargs): + request = self.make_request() + session = self.client.send_request(request=request, stream=False, **kwargs) + if session.http_response.status_code in [202]: + return self.client.build_lro_polling( + self.ctx.args.no_wait, + session, + self.on_200, + self.on_error, + lro_options={"final-state-via": "location"}, + path_format_arguments=self.url_parameters, + ) + if session.http_response.status_code in [200]: + return self.client.build_lro_polling( + self.ctx.args.no_wait, + session, + self.on_200, + self.on_error, + lro_options={"final-state-via": "location"}, + path_format_arguments=self.url_parameters, + ) + + return self.on_error(session.http_response) + + @property + def url(self): + return self.client.format_url( + "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/poweroff", + **self.url_parameters + ) + + @property + def method(self): + return "POST" + + @property + def error_format(self): + return "ODataV4Format" + + @property + def url_parameters(self): + parameters = { + **self.serialize_url_param( + "resourceGroupName", self.ctx.args.resource_group, + required=True, + ), + **self.serialize_url_param( + "subscriptionId", self.ctx.subscription_id, + required=True, + ), + **self.serialize_url_param( + "vmScaleSetName", self.ctx.args.vm_scale_set_name, + required=True, + ), + } + return parameters + + @property + def query_parameters(self): + parameters = { + **self.serialize_query_param( + "skipShutdown", self.ctx.args.skip_shutdown, + ), + **self.serialize_query_param( + "api-version", "2024-11-01", + required=True, + ), + } + return parameters + + @property + def header_parameters(self): + parameters = { + **self.serialize_header_param( + "Content-Type", "application/json", + ), + } + return parameters + + @property + def content(self): + _content_value, _builder = self.new_content_builder( + self.ctx.args, + typ=AAZObjectType, + typ_kwargs={"flags": {"client_flatten": True}} + ) + _builder.set_prop("instanceIds", AAZListType, ".instance_ids") + + instance_ids = _builder.get(".instanceIds") + if instance_ids is not None: + instance_ids.set_elements(AAZStrType, ".") + + return self.serialize_content(_content_value) + + def on_200(self, session): + pass + + +class _StopHelper: + """Helper class for Stop""" + + +__all__ = ["Stop"] From 1c595a1b8bc9598391a7de352b093bd7e75cfaf5 Mon Sep 17 00:00:00 2001 From: william051200 Date: Mon, 16 Feb 2026 08:27:13 +0800 Subject: [PATCH 2/2] Migrate vmss stop --- .../azure/cli/command_modules/vm/commands.py | 3 ++- .../azure/cli/command_modules/vm/custom.py | 21 +++++++++---------- 2 files changed, 12 insertions(+), 12 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 beab1cd8e1b..6478817851e 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -401,6 +401,8 @@ 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('stop', 'stop_vmss', supports_no_wait=True, validator=process_vm_vmss_stop) + with self.command_group('vmss', compute_vmss_sdk, operation_group='virtual_machine_scale_sets') as g: g.custom_command('application set', 'set_vmss_applications', validator=process_set_applications_namespace, min_api='2021-07-01') g.custom_command('application list', 'list_vmss_applications', min_api='2021-07-01') @@ -414,7 +416,6 @@ 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_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) g.custom_command('update-instances', 'update_vmss_instances', supports_no_wait=True) g.wait_command('wait', getter_name='get_vmss', getter_type=compute_custom) 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 639c102a747..f840db26941 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -4509,17 +4509,16 @@ def scale_vmss(cmd, resource_group_name, vm_scale_set_name, new_capacity, no_wai def stop_vmss(cmd, resource_group_name, vm_scale_set_name, instance_ids=None, no_wait=False, skip_shutdown=False): - client = _compute_client_factory(cmd.cli_ctx) - VirtualMachineScaleSetVMInstanceRequiredIDs = cmd.get_models('VirtualMachineScaleSetVMInstanceRequiredIDs') - if instance_ids is None: - instance_ids = ['*'] - instance_ids = VirtualMachineScaleSetVMInstanceRequiredIDs(instance_ids=instance_ids) - if cmd.supported_api_version(min_api='2020-06-01', operation_group='virtual_machine_scale_sets'): - return sdk_no_wait( - no_wait, client.virtual_machine_scale_sets.begin_power_off, resource_group_name, vm_scale_set_name, - vm_instance_i_ds=instance_ids, skip_shutdown=skip_shutdown) - return sdk_no_wait(no_wait, client.virtual_machine_scale_sets.begin_power_off, resource_group_name, - vm_scale_set_name, vm_instance_i_ds=instance_ids) + from .aaz.latest.vmss import Stop as VmssStop + command_args = { + 'resource_group': resource_group_name, + 'vm_scale_set_name': vm_scale_set_name, + 'skip_shutdown': skip_shutdown, + 'no_wait': no_wait + } + if instance_ids: + command_args['instance_ids'] = instance_ids + return VmssStop(cli_ctx=cmd.cli_ctx)(command_args=command_args) def update_vmss_instances(cmd, resource_group_name, vm_scale_set_name, instance_ids, no_wait=False):