Skip to content

Commit 1579dc1

Browse files
authored
{Compute} az vm restart: Migrate commands to aaz (#32638)
1 parent af27ac4 commit 1579dc1

5 files changed

Lines changed: 2059 additions & 1607 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
from ._start import *
3434
from ._update import *
3535
from ._wait import *
36+
from ._restart import *
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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 Restart(AAZCommand):
15+
"""Restart an existing VM.
16+
17+
:example: Restart a VM.
18+
az vm restart -g MyResourceGroup -n MyVm
19+
20+
:example: Force restart a VM.
21+
az vm restart -g MyResourceGroup -n MyVm --force
22+
"""
23+
24+
_aaz_info = {
25+
"version": "2022-11-01",
26+
"resources": [
27+
["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.compute/virtualmachines/{}/restart", "2022-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, self._output)
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+
required=True,
50+
)
51+
_args_schema.vm_name = AAZStrArg(
52+
options=["-n", "--name", "--vm-name"],
53+
help="The name of the Virtual Machine. You can configure the default using `az configure --defaults vm=<name>`",
54+
required=True,
55+
id_part="name",
56+
configured_default="vm",
57+
)
58+
return cls._args_schema
59+
60+
def _execute_operations(self):
61+
self.pre_operations()
62+
yield self.VirtualMachinesRestart(ctx=self.ctx)()
63+
self.post_operations()
64+
65+
@register_callback
66+
def pre_operations(self):
67+
pass
68+
69+
@register_callback
70+
def post_operations(self):
71+
pass
72+
73+
def _output(self, *args, **kwargs):
74+
result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
75+
return result
76+
77+
class VirtualMachinesRestart(AAZHttpOperation):
78+
CLIENT_TYPE = "MgmtClient"
79+
80+
def __call__(self, *args, **kwargs):
81+
request = self.make_request()
82+
session = self.client.send_request(request=request, stream=False, **kwargs)
83+
if session.http_response.status_code in [202]:
84+
return self.client.build_lro_polling(
85+
self.ctx.args.no_wait,
86+
session,
87+
self.on_200,
88+
self.on_error,
89+
lro_options={"final-state-via": "azure-async-operation"},
90+
path_format_arguments=self.url_parameters,
91+
)
92+
if session.http_response.status_code in [200]:
93+
return self.client.build_lro_polling(
94+
self.ctx.args.no_wait,
95+
session,
96+
self.on_200,
97+
self.on_error,
98+
lro_options={"final-state-via": "azure-async-operation"},
99+
path_format_arguments=self.url_parameters,
100+
)
101+
102+
return self.on_error(session.http_response)
103+
104+
@property
105+
def url(self):
106+
return self.client.format_url(
107+
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/restart",
108+
**self.url_parameters
109+
)
110+
111+
@property
112+
def method(self):
113+
return "POST"
114+
115+
@property
116+
def error_format(self):
117+
return "MgmtErrorFormat"
118+
119+
@property
120+
def url_parameters(self):
121+
parameters = {
122+
**self.serialize_url_param(
123+
"resourceGroupName", self.ctx.args.resource_group,
124+
required=True,
125+
),
126+
**self.serialize_url_param(
127+
"subscriptionId", self.ctx.subscription_id,
128+
required=True,
129+
),
130+
**self.serialize_url_param(
131+
"vmName", self.ctx.args.vm_name,
132+
required=True,
133+
),
134+
}
135+
return parameters
136+
137+
@property
138+
def query_parameters(self):
139+
parameters = {
140+
**self.serialize_query_param(
141+
"api-version", "2022-11-01",
142+
required=True,
143+
),
144+
}
145+
return parameters
146+
147+
@property
148+
def header_parameters(self):
149+
parameters = {
150+
**self.serialize_header_param(
151+
"Accept", "application/json",
152+
),
153+
}
154+
return parameters
155+
156+
def on_200(self, session):
157+
data = self.deserialize_http_content(session)
158+
self.ctx.set_var(
159+
"instance",
160+
data,
161+
schema_builder=self._build_schema_on_200
162+
)
163+
164+
_schema_on_200 = None
165+
166+
@classmethod
167+
def _build_schema_on_200(cls):
168+
if cls._schema_on_200 is not None:
169+
return cls._schema_on_200
170+
171+
cls._schema_on_200 = AAZObjectType()
172+
173+
_schema_on_200 = cls._schema_on_200
174+
_schema_on_200.end_time = AAZStrType(
175+
serialized_name="endTime",
176+
flags={"read_only": True},
177+
)
178+
_schema_on_200.error = AAZObjectType(
179+
flags={"read_only": True},
180+
)
181+
_schema_on_200.name = AAZStrType(
182+
flags={"read_only": True},
183+
)
184+
_schema_on_200.start_time = AAZStrType(
185+
serialized_name="startTime",
186+
flags={"read_only": True},
187+
)
188+
_schema_on_200.status = AAZStrType(
189+
flags={"read_only": True},
190+
)
191+
192+
error = cls._schema_on_200.error
193+
error.code = AAZStrType()
194+
error.details = AAZListType()
195+
error.innererror = AAZObjectType()
196+
error.message = AAZStrType()
197+
error.target = AAZStrType()
198+
199+
details = cls._schema_on_200.error.details
200+
details.Element = AAZObjectType()
201+
202+
_element = cls._schema_on_200.error.details.Element
203+
_element.code = AAZStrType()
204+
_element.message = AAZStrType()
205+
_element.target = AAZStrType()
206+
207+
innererror = cls._schema_on_200.error.innererror
208+
innererror.errordetail = AAZStrType()
209+
innererror.exceptiontype = AAZStrType()
210+
211+
return cls._schema_on_200
212+
213+
214+
class _RestartHelper:
215+
"""Helper class for Restart"""
216+
217+
218+
__all__ = ["Restart"]

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,10 +1737,18 @@ def resize_vm(cmd, resource_group_name, vm_name, size, no_wait=False):
17371737

17381738

17391739
def restart_vm(cmd, resource_group_name, vm_name, no_wait=False, force=False):
1740-
client = _compute_client_factory(cmd.cli_ctx)
1740+
from .aaz.latest.vm import Redeploy as _VMRedeploy, Restart as _VMRestart
1741+
1742+
command_args = {
1743+
"resource_group": resource_group_name,
1744+
"vm_name": vm_name,
1745+
"no_wait": no_wait,
1746+
}
1747+
17411748
if force:
1742-
return sdk_no_wait(no_wait, client.virtual_machines.begin_redeploy, resource_group_name, vm_name)
1743-
return sdk_no_wait(no_wait, client.virtual_machines.begin_restart, resource_group_name, vm_name)
1749+
return _VMRedeploy(cli_ctx=cmd.cli_ctx)(command_args=command_args)
1750+
1751+
return _VMRestart(cli_ctx=cmd.cli_ctx)(command_args=command_args)
17441752

17451753

17461754
def set_vm(cmd, instance, lro_operation=None, no_wait=False):

0 commit comments

Comments
 (0)