Skip to content

Commit f87bc09

Browse files
{Compute} az vmss start: Migrate to CodeGen based (#31909)
1 parent 58c8b02 commit f87bc09

7 files changed

Lines changed: 201 additions & 23 deletions

File tree

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,16 +2800,6 @@
28002800
crafted: true
28012801
"""
28022802

2803-
helps['vmss start'] = """
2804-
type: command
2805-
short-summary: Start VMs within a VMSS.
2806-
examples:
2807-
- name: Start VMs within a VMSS. (autogenerated)
2808-
text: |
2809-
az vmss start --instance-ids 1 --name MyScaleSet --resource-group MyResourceGroup
2810-
crafted: true
2811-
"""
2812-
28132803
helps['vmss stop'] = """
28142804
type: command
28152805
short-summary: Power off (stop) VMs within a VMSS.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ def load_arguments(self, _):
705705
c.argument('host_group', min_api='2020-06-01',
706706
help='Name or ID of dedicated host group that the virtual machine scale set resides in')
707707

708-
for scope in ['vmss deallocate', 'vmss delete-instances', 'vmss restart', 'vmss start', 'vmss stop', 'vmss show', 'vmss update-instances', 'vmss simulate-eviction']:
708+
for scope in ['vmss deallocate', 'vmss delete-instances', 'vmss restart', 'vmss stop', 'vmss show', 'vmss update-instances', 'vmss simulate-eviction']:
709709
with self.argument_context(scope) as c:
710710
for dest in scaleset_name_aliases:
711711
c.argument(dest, vmss_name_type, id_part=None) # due to instance-ids parameter

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
@@ -18,6 +18,7 @@
1818
from ._patch import *
1919
from ._perform_maintenance import *
2020
from ._simulate_eviction import *
21+
from ._start import *
2122
from ._update import *
2223
from ._update_domain_walk import *
2324
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+
@register_command(
15+
"vmss start",
16+
)
17+
class Start(AAZCommand):
18+
"""Start VMs within a VMSS.
19+
20+
:example: Start VMs within a VMSS.
21+
az vmss start --instance-ids 1 --name MyScaleSet --resource-group MyResourceGroup
22+
"""
23+
24+
_aaz_info = {
25+
"version": "2024-11-01",
26+
"resources": [
27+
["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.compute/virtualmachinescalesets/{}/start", "2024-11-01"],
28+
]
29+
}
30+
31+
AZ_SUPPORT_NO_WAIT = True
32+
33+
def _handler(self, command_args):
34+
super()._handler(command_args)
35+
return self.build_lro_poller(self._execute_operations, None)
36+
37+
_args_schema = None
38+
39+
@classmethod
40+
def _build_arguments_schema(cls, *args, **kwargs):
41+
if cls._args_schema is not None:
42+
return cls._args_schema
43+
cls._args_schema = super()._build_arguments_schema(*args, **kwargs)
44+
45+
# define Arg Group ""
46+
47+
_args_schema = cls._args_schema
48+
_args_schema.resource_group = AAZResourceGroupNameArg(
49+
help="Name of resource group. You can configure the default group using `az configure --defaults group=<name>`",
50+
required=True,
51+
)
52+
_args_schema.name = AAZStrArg(
53+
options=["-n", "--name"],
54+
help="Scale set name. You can configure the default using `az configure --defaults vmss=<name>`",
55+
required=True,
56+
id_part="name",
57+
)
58+
59+
# define Arg Group "VmInstanceIDs"
60+
61+
_args_schema = cls._args_schema
62+
_args_schema.instance_ids = AAZListArg(
63+
options=["--instance-ids"],
64+
arg_group="VmInstanceIDs",
65+
help="Space-separated list of IDs (ex: 1 2 3 ...) or * for all instances. If not provided, the action will be applied on the scaleset itself.",
66+
)
67+
68+
instance_ids = cls._args_schema.instance_ids
69+
instance_ids.Element = AAZStrArg()
70+
return cls._args_schema
71+
72+
def _execute_operations(self):
73+
self.pre_operations()
74+
yield self.VirtualMachineScaleSetsStart(ctx=self.ctx)()
75+
self.post_operations()
76+
77+
@register_callback
78+
def pre_operations(self):
79+
pass
80+
81+
@register_callback
82+
def post_operations(self):
83+
pass
84+
85+
class VirtualMachineScaleSetsStart(AAZHttpOperation):
86+
CLIENT_TYPE = "MgmtClient"
87+
88+
def __call__(self, *args, **kwargs):
89+
request = self.make_request()
90+
session = self.client.send_request(request=request, stream=False, **kwargs)
91+
if session.http_response.status_code in [202]:
92+
return self.client.build_lro_polling(
93+
self.ctx.args.no_wait,
94+
session,
95+
self.on_200,
96+
self.on_error,
97+
lro_options={"final-state-via": "location"},
98+
path_format_arguments=self.url_parameters,
99+
)
100+
if session.http_response.status_code in [200]:
101+
return self.client.build_lro_polling(
102+
self.ctx.args.no_wait,
103+
session,
104+
self.on_200,
105+
self.on_error,
106+
lro_options={"final-state-via": "location"},
107+
path_format_arguments=self.url_parameters,
108+
)
109+
110+
return self.on_error(session.http_response)
111+
112+
@property
113+
def url(self):
114+
return self.client.format_url(
115+
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/start",
116+
**self.url_parameters
117+
)
118+
119+
@property
120+
def method(self):
121+
return "POST"
122+
123+
@property
124+
def error_format(self):
125+
return "ODataV4Format"
126+
127+
@property
128+
def url_parameters(self):
129+
parameters = {
130+
**self.serialize_url_param(
131+
"resourceGroupName", self.ctx.args.resource_group,
132+
required=True,
133+
),
134+
**self.serialize_url_param(
135+
"subscriptionId", self.ctx.subscription_id,
136+
required=True,
137+
),
138+
**self.serialize_url_param(
139+
"vmScaleSetName", self.ctx.args.name,
140+
required=True,
141+
),
142+
}
143+
return parameters
144+
145+
@property
146+
def query_parameters(self):
147+
parameters = {
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 _StartHelper:
184+
"""Helper class for Start"""
185+
186+
187+
__all__ = ["Start"]

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ def load_command_table(self, _):
430430
g.custom_command('restart', 'restart_vmss', supports_no_wait=True)
431431
g.custom_command('scale', 'scale_vmss', supports_no_wait=True)
432432
g.custom_show_command('show', 'get_vmss', table_transformer=get_vmss_table_output_transformer(self, False))
433-
g.custom_command('start', 'start_vmss', supports_no_wait=True)
434433
g.custom_command('stop', 'stop_vmss', supports_no_wait=True, validator=process_vm_vmss_stop)
435434
g.generic_update_command('update', getter_name='get_vmss_modified', setter_name='update_vmss', supports_no_wait=True, command_type=compute_custom, validator=validate_vmss_update_namespace)
436435
g.custom_command('update-instances', 'update_vmss_instances', supports_no_wait=True)

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4043,16 +4043,6 @@ def scale_vmss(cmd, resource_group_name, vm_scale_set_name, new_capacity, no_wai
40434043
resource_group_name, vm_scale_set_name, vmss_new)
40444044

40454045

4046-
def start_vmss(cmd, resource_group_name, vm_scale_set_name, instance_ids=None, no_wait=False):
4047-
client = _compute_client_factory(cmd.cli_ctx)
4048-
VirtualMachineScaleSetVMInstanceRequiredIDs = cmd.get_models('VirtualMachineScaleSetVMInstanceRequiredIDs')
4049-
if instance_ids is None:
4050-
instance_ids = ['*']
4051-
instance_ids = VirtualMachineScaleSetVMInstanceRequiredIDs(instance_ids=instance_ids)
4052-
return sdk_no_wait(no_wait, client.virtual_machine_scale_sets.begin_start,
4053-
resource_group_name, vm_scale_set_name, vm_instance_i_ds=instance_ids)
4054-
4055-
40564046
def stop_vmss(cmd, resource_group_name, vm_scale_set_name, instance_ids=None, no_wait=False, skip_shutdown=False):
40574047
client = _compute_client_factory(cmd.cli_ctx)
40584048
VirtualMachineScaleSetVMInstanceRequiredIDs = cmd.get_models('VirtualMachineScaleSetVMInstanceRequiredIDs')

src/azure-cli/azure/cli/command_modules/vm/operations/vmss.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
from knack.log import get_logger
77

88
from ..aaz.latest.vmss import ListInstances as _VMSSListInstances
9+
from ..aaz.latest.vmss import Start as _Start
10+
from azure.cli.core.aaz import AAZUndefined, has_value
911

1012
logger = get_logger(__name__)
1113

1214

1315
class VMSSListInstances(_VMSSListInstances):
1416
def _output(self, *args, **kwargs):
15-
from azure.cli.core.aaz import AAZUndefined, has_value
1617

1718
# Resolve flatten conflict
1819
# When the type field conflicts, the type in inner layer is ignored and the outer layer is applied
@@ -25,3 +26,13 @@ def _output(self, *args, **kwargs):
2526
result = self.deserialize_output(self.ctx.vars.instance.value, client_flatten=True)
2627
next_link = self.deserialize_output(self.ctx.vars.instance.next_link)
2728
return result, next_link
29+
30+
31+
class VMSSStart(_Start):
32+
33+
def pre_operations(self):
34+
args = self.ctx.args
35+
36+
if not has_value(args.instance_ids):
37+
# if instance id is not provide, override with '*'
38+
args.instance_ids = ["*"]

0 commit comments

Comments
 (0)