Skip to content

Commit b4136bd

Browse files
{Compute} vmss delete-instances: migrate to codegen based command (#31962)
1 parent 3d3f70c commit b4136bd

File tree

6 files changed

+186
-20
lines changed

6 files changed

+186
-20
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,15 +2327,6 @@
23272327
crafted: true
23282328
"""
23292329

2330-
helps['vmss delete-instances'] = """
2331-
type: command
2332-
short-summary: Delete VMs within a VMSS.
2333-
examples:
2334-
- name: Delete VMs within a VMSS. (autogenerated)
2335-
text: |
2336-
az vmss delete-instances --instance-ids 0 --name MyScaleSet --resource-group MyResourceGroup
2337-
crafted: true
2338-
"""
23392330

23402331
helps['vmss diagnostics'] = """
23412332
type: group

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

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

709-
for scope in ['vmss deallocate', 'vmss delete-instances', 'vmss restart', 'vmss stop', 'vmss show', 'vmss update-instances', 'vmss simulate-eviction']:
709+
for scope in ['vmss deallocate', 'vmss restart', 'vmss stop', 'vmss show', 'vmss update-instances', 'vmss simulate-eviction']:
710710
with self.argument_context(scope) as c:
711711
for dest in scaleset_name_aliases:
712712
c.argument(dest, vmss_name_type, id_part=None) # due to instance-ids parameter
@@ -876,7 +876,7 @@ def load_arguments(self, _):
876876
with self.argument_context('vmss wait') as c:
877877
c.argument('instance_id', id_part='child_name_1', help="Wait on the VM instance with this ID. If missing, wait on the VMSS.")
878878

879-
for scope in ['vmss update-instances', 'vmss delete-instances']:
879+
for scope in ['vmss update-instances']:
880880
with self.argument_context(scope) as c:
881881
c.argument('instance_ids', multi_ids_type, help='Space-separated list of IDs (ex: 1 2 3 ...) or * for all instances.')
882882

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
@@ -10,6 +10,7 @@
1010

1111
from .__cmd_group import *
1212
from ._delete import *
13+
from ._delete_instances import *
1314
from ._get_os_upgrade_history import *
1415
from ._list import *
1516
from ._list_instance_public_ips import *
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 delete-instances",
16+
)
17+
class DeleteInstances(AAZCommand):
18+
"""Delete VMs within a VMSS.
19+
20+
:example: Delete VMs within a VMSS.
21+
az vmss delete-instances --instance-ids 0 --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/{}/delete", "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+
_args_schema.instance_ids = AAZListArg(
59+
options=["--instance-ids"],
60+
help="Space-separated list of IDs (ex: 1 2 3 ...) or * for all instances.",
61+
required=True,
62+
)
63+
64+
instance_ids = cls._args_schema.instance_ids
65+
instance_ids.Element = AAZStrArg()
66+
return cls._args_schema
67+
68+
def _execute_operations(self):
69+
self.pre_operations()
70+
yield self.VirtualMachineScaleSetsDeleteInstances(ctx=self.ctx)()
71+
self.post_operations()
72+
73+
@register_callback
74+
def pre_operations(self):
75+
pass
76+
77+
@register_callback
78+
def post_operations(self):
79+
pass
80+
81+
class VirtualMachineScaleSetsDeleteInstances(AAZHttpOperation):
82+
CLIENT_TYPE = "MgmtClient"
83+
84+
def __call__(self, *args, **kwargs):
85+
request = self.make_request()
86+
session = self.client.send_request(request=request, stream=False, **kwargs)
87+
if session.http_response.status_code in [202]:
88+
return self.client.build_lro_polling(
89+
self.ctx.args.no_wait,
90+
session,
91+
self.on_200,
92+
self.on_error,
93+
lro_options={"final-state-via": "location"},
94+
path_format_arguments=self.url_parameters,
95+
)
96+
if session.http_response.status_code in [200]:
97+
return self.client.build_lro_polling(
98+
self.ctx.args.no_wait,
99+
session,
100+
self.on_200,
101+
self.on_error,
102+
lro_options={"final-state-via": "location"},
103+
path_format_arguments=self.url_parameters,
104+
)
105+
106+
return self.on_error(session.http_response)
107+
108+
@property
109+
def url(self):
110+
return self.client.format_url(
111+
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/delete",
112+
**self.url_parameters
113+
)
114+
115+
@property
116+
def method(self):
117+
return "POST"
118+
119+
@property
120+
def error_format(self):
121+
return "ODataV4Format"
122+
123+
@property
124+
def url_parameters(self):
125+
parameters = {
126+
**self.serialize_url_param(
127+
"resourceGroupName", self.ctx.args.resource_group,
128+
required=True,
129+
),
130+
**self.serialize_url_param(
131+
"subscriptionId", self.ctx.subscription_id,
132+
required=True,
133+
),
134+
**self.serialize_url_param(
135+
"vmScaleSetName", self.ctx.args.name,
136+
required=True,
137+
),
138+
}
139+
return parameters
140+
141+
@property
142+
def query_parameters(self):
143+
parameters = {
144+
**self.serialize_query_param(
145+
"api-version", "2024-11-01",
146+
required=True,
147+
),
148+
}
149+
return parameters
150+
151+
@property
152+
def header_parameters(self):
153+
parameters = {
154+
**self.serialize_header_param(
155+
"Content-Type", "application/json",
156+
),
157+
}
158+
return parameters
159+
160+
@property
161+
def content(self):
162+
_content_value, _builder = self.new_content_builder(
163+
self.ctx.args,
164+
typ=AAZObjectType,
165+
typ_kwargs={"flags": {"required": True, "client_flatten": True}}
166+
)
167+
_builder.set_prop("instanceIds", AAZListType, ".instance_ids", typ_kwargs={"flags": {"required": True}})
168+
169+
instance_ids = _builder.get(".instanceIds")
170+
if instance_ids is not None:
171+
instance_ids.set_elements(AAZStrType, ".")
172+
173+
return self.serialize_content(_content_value)
174+
175+
def on_200(self, session):
176+
pass
177+
178+
179+
class _DeleteInstancesHelper:
180+
"""Helper class for DeleteInstances"""
181+
182+
183+
__all__ = ["DeleteInstances"]

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ def load_command_table(self, _):
422422
g.custom_command('application list', 'list_vmss_applications', min_api='2021-07-01')
423423
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)
424424
g.custom_command('deallocate', 'deallocate_vmss', supports_no_wait=True)
425-
g.custom_command('delete-instances', 'delete_vmss_instances', supports_no_wait=True)
426425
g.custom_command('get-instance-view', 'get_vmss_instance_view', table_transformer='{ProvisioningState:statuses[0].displayStatus, PowerState:statuses[1].displayStatus}')
427426
g.custom_command('list-instance-connection-info', 'list_vmss_instance_connection_info')
428427
g.custom_command('list-instance-public-ips', 'list_vmss_instance_public_ips')

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,14 +3812,6 @@ def deallocate_vmss(cmd, resource_group_name, vm_scale_set_name, instance_ids=No
38123812
resource_group_name, vm_scale_set_name, vm_instance_i_ds)
38133813

38143814

3815-
def delete_vmss_instances(cmd, resource_group_name, vm_scale_set_name, instance_ids, no_wait=False):
3816-
client = _compute_client_factory(cmd.cli_ctx)
3817-
VirtualMachineScaleSetVMInstanceRequiredIDs = cmd.get_models('VirtualMachineScaleSetVMInstanceRequiredIDs')
3818-
instance_ids = VirtualMachineScaleSetVMInstanceRequiredIDs(instance_ids=instance_ids)
3819-
return sdk_no_wait(no_wait, client.virtual_machine_scale_sets.begin_delete_instances,
3820-
resource_group_name, vm_scale_set_name, instance_ids)
3821-
3822-
38233815
def get_vmss(cmd, resource_group_name, name, instance_id=None, include_user_data=False):
38243816
client = _compute_client_factory(cmd.cli_ctx)
38253817

0 commit comments

Comments
 (0)