Skip to content

Commit 5cf3dae

Browse files
{Compute} az vmss stop: Migrate command to aaz-based implementation (#32799)
1 parent 57b94dd commit 5cf3dae

File tree

4 files changed

+198
-10
lines changed

4 files changed

+198
-10
lines changed

src/azure-cli/azure/cli/command_modules/vm/aaz/latest/vmss/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from ._show import *
2727
from ._simulate_eviction import *
2828
from ._start import *
29+
from ._stop import *
2930
from ._update import *
3031
from ._update_domain_walk import *
3132
from ._wait import *
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
#
5+
# Code generated by aaz-dev-tools
6+
# --------------------------------------------------------------------------------------------
7+
8+
# pylint: skip-file
9+
# flake8: noqa
10+
11+
from azure.cli.core.aaz import *
12+
13+
14+
class Stop(AAZCommand):
15+
"""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.
16+
"""
17+
18+
_aaz_info = {
19+
"version": "2024-11-01",
20+
"resources": [
21+
["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.compute/virtualmachinescalesets/{}/poweroff", "2024-11-01"],
22+
]
23+
}
24+
25+
AZ_SUPPORT_NO_WAIT = True
26+
27+
def _handler(self, command_args):
28+
super()._handler(command_args)
29+
return self.build_lro_poller(self._execute_operations, None)
30+
31+
_args_schema = None
32+
33+
@classmethod
34+
def _build_arguments_schema(cls, *args, **kwargs):
35+
if cls._args_schema is not None:
36+
return cls._args_schema
37+
cls._args_schema = super()._build_arguments_schema(*args, **kwargs)
38+
39+
# define Arg Group ""
40+
41+
_args_schema = cls._args_schema
42+
_args_schema.resource_group = AAZResourceGroupNameArg(
43+
required=True,
44+
)
45+
_args_schema.vm_scale_set_name = AAZStrArg(
46+
options=["--vm-scale-set-name"],
47+
help="The name of the VM scale set.",
48+
required=True,
49+
id_part="name",
50+
)
51+
_args_schema.skip_shutdown = AAZBoolArg(
52+
options=["--skip-shutdown"],
53+
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",
54+
)
55+
56+
# define Arg Group "VmInstanceIDs"
57+
58+
_args_schema = cls._args_schema
59+
_args_schema.instance_ids = AAZListArg(
60+
options=["--instance-ids"],
61+
arg_group="VmInstanceIDs",
62+
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.",
63+
)
64+
65+
instance_ids = cls._args_schema.instance_ids
66+
instance_ids.Element = AAZStrArg()
67+
return cls._args_schema
68+
69+
def _execute_operations(self):
70+
self.pre_operations()
71+
yield self.VirtualMachineScaleSetsPowerOff(ctx=self.ctx)()
72+
self.post_operations()
73+
74+
@register_callback
75+
def pre_operations(self):
76+
pass
77+
78+
@register_callback
79+
def post_operations(self):
80+
pass
81+
82+
class VirtualMachineScaleSetsPowerOff(AAZHttpOperation):
83+
CLIENT_TYPE = "MgmtClient"
84+
85+
def __call__(self, *args, **kwargs):
86+
request = self.make_request()
87+
session = self.client.send_request(request=request, stream=False, **kwargs)
88+
if session.http_response.status_code in [202]:
89+
return self.client.build_lro_polling(
90+
self.ctx.args.no_wait,
91+
session,
92+
self.on_200,
93+
self.on_error,
94+
lro_options={"final-state-via": "location"},
95+
path_format_arguments=self.url_parameters,
96+
)
97+
if session.http_response.status_code in [200]:
98+
return self.client.build_lro_polling(
99+
self.ctx.args.no_wait,
100+
session,
101+
self.on_200,
102+
self.on_error,
103+
lro_options={"final-state-via": "location"},
104+
path_format_arguments=self.url_parameters,
105+
)
106+
107+
return self.on_error(session.http_response)
108+
109+
@property
110+
def url(self):
111+
return self.client.format_url(
112+
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/poweroff",
113+
**self.url_parameters
114+
)
115+
116+
@property
117+
def method(self):
118+
return "POST"
119+
120+
@property
121+
def error_format(self):
122+
return "ODataV4Format"
123+
124+
@property
125+
def url_parameters(self):
126+
parameters = {
127+
**self.serialize_url_param(
128+
"resourceGroupName", self.ctx.args.resource_group,
129+
required=True,
130+
),
131+
**self.serialize_url_param(
132+
"subscriptionId", self.ctx.subscription_id,
133+
required=True,
134+
),
135+
**self.serialize_url_param(
136+
"vmScaleSetName", self.ctx.args.vm_scale_set_name,
137+
required=True,
138+
),
139+
}
140+
return parameters
141+
142+
@property
143+
def query_parameters(self):
144+
parameters = {
145+
**self.serialize_query_param(
146+
"skipShutdown", self.ctx.args.skip_shutdown,
147+
),
148+
**self.serialize_query_param(
149+
"api-version", "2024-11-01",
150+
required=True,
151+
),
152+
}
153+
return parameters
154+
155+
@property
156+
def header_parameters(self):
157+
parameters = {
158+
**self.serialize_header_param(
159+
"Content-Type", "application/json",
160+
),
161+
}
162+
return parameters
163+
164+
@property
165+
def content(self):
166+
_content_value, _builder = self.new_content_builder(
167+
self.ctx.args,
168+
typ=AAZObjectType,
169+
typ_kwargs={"flags": {"client_flatten": True}}
170+
)
171+
_builder.set_prop("instanceIds", AAZListType, ".instance_ids")
172+
173+
instance_ids = _builder.get(".instanceIds")
174+
if instance_ids is not None:
175+
instance_ids.set_elements(AAZStrType, ".")
176+
177+
return self.serialize_content(_content_value)
178+
179+
def on_200(self, session):
180+
pass
181+
182+
183+
class _StopHelper:
184+
"""Helper class for Stop"""
185+
186+
187+
__all__ = ["Stop"]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ def load_command_table(self, _):
409409
g.custom_command('reimage', 'reimage_vmss', supports_no_wait=True)
410410
g.custom_command('restart', 'restart_vmss', supports_no_wait=True)
411411
g.custom_command('scale', 'scale_vmss', supports_no_wait=True)
412+
g.custom_command('stop', 'stop_vmss', supports_no_wait=True, validator=process_vm_vmss_stop)
412413

413414
with self.command_group('vmss application', operation_group='virtual_machine_scale_sets') as g:
414415
g.custom_command('set', 'set_vmss_applications', validator=process_set_applications_namespace)
@@ -418,7 +419,6 @@ def load_command_table(self, _):
418419
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)
419420
g.custom_command('get-instance-view', 'get_vmss_instance_view', table_transformer='{ProvisioningState:statuses[0].displayStatus, PowerState:statuses[1].displayStatus}')
420421
g.custom_show_command('show', 'get_vmss', table_transformer=get_vmss_table_output_transformer(self, False))
421-
g.custom_command('stop', 'stop_vmss', supports_no_wait=True, validator=process_vm_vmss_stop)
422422
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)
423423
g.custom_command('update-instances', 'update_vmss_instances', supports_no_wait=True)
424424
g.wait_command('wait', getter_name='get_vmss', getter_type=compute_custom)

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4593,17 +4593,17 @@ def scale_vmss(cmd, resource_group_name, vm_scale_set_name, new_capacity, no_wai
45934593

45944594

45954595
def stop_vmss(cmd, resource_group_name, vm_scale_set_name, instance_ids=None, no_wait=False, skip_shutdown=False):
4596-
client = _compute_client_factory(cmd.cli_ctx)
4597-
VirtualMachineScaleSetVMInstanceRequiredIDs = cmd.get_models('VirtualMachineScaleSetVMInstanceRequiredIDs')
4596+
from .aaz.latest.vmss import Stop as VmssStop
45984597
if instance_ids is None:
45994598
instance_ids = ['*']
4600-
instance_ids = VirtualMachineScaleSetVMInstanceRequiredIDs(instance_ids=instance_ids)
4601-
if cmd.supported_api_version(min_api='2020-06-01', operation_group='virtual_machine_scale_sets'):
4602-
return sdk_no_wait(
4603-
no_wait, client.virtual_machine_scale_sets.begin_power_off, resource_group_name, vm_scale_set_name,
4604-
vm_instance_i_ds=instance_ids, skip_shutdown=skip_shutdown)
4605-
return sdk_no_wait(no_wait, client.virtual_machine_scale_sets.begin_power_off, resource_group_name,
4606-
vm_scale_set_name, vm_instance_i_ds=instance_ids)
4599+
command_args = {
4600+
'resource_group': resource_group_name,
4601+
'vm_scale_set_name': vm_scale_set_name,
4602+
'skip_shutdown': skip_shutdown,
4603+
'instance_ids': instance_ids,
4604+
'no_wait': no_wait
4605+
}
4606+
return VmssStop(cli_ctx=cmd.cli_ctx)(command_args=command_args)
46074607

46084608

46094609
def update_vmss_instances(cmd, resource_group_name, vm_scale_set_name, instance_ids, no_wait=False):

0 commit comments

Comments
 (0)