Skip to content

Commit 9153a1b

Browse files
authored
{Compute} az vm boot-diagnostics get-boot-log-uris: Migrate to aaz (#31049)
1 parent 37dfc67 commit 9153a1b

File tree

7 files changed

+1275
-925
lines changed

7 files changed

+1275
-925
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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_group(
15+
"vm boot-diagnostics",
16+
)
17+
class __CMDGroup(AAZCommandGroup):
18+
"""Troubleshoot the startup of an Azure Virtual Machine. Use this feature to troubleshoot boot failures for custom or platform images.
19+
"""
20+
pass
21+
22+
23+
__all__ = ["__CMDGroup"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 .__cmd_group import *
12+
from ._get_boot_log_uris import *
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
"vm boot-diagnostics get-boot-log-uris",
16+
)
17+
class GetBootLogUris(AAZCommand):
18+
"""Get SAS URIs for a virtual machine's boot diagnostic
19+
20+
:example: Get SAS URIs for a virtual machine's boot diagnostic logs.
21+
az vm boot-diagnostics get-boot-log-uris -g MyResourceGroup -n MyVirtualMachine
22+
"""
23+
24+
_aaz_info = {
25+
"version": "2024-07-01",
26+
"resources": [
27+
["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.compute/virtualmachines/{}/retrievebootdiagnosticsdata", "2024-07-01"],
28+
]
29+
}
30+
31+
def _handler(self, command_args):
32+
super()._handler(command_args)
33+
self._execute_operations()
34+
return self._output()
35+
36+
_args_schema = None
37+
38+
@classmethod
39+
def _build_arguments_schema(cls, *args, **kwargs):
40+
if cls._args_schema is not None:
41+
return cls._args_schema
42+
cls._args_schema = super()._build_arguments_schema(*args, **kwargs)
43+
44+
# define Arg Group ""
45+
46+
_args_schema = cls._args_schema
47+
_args_schema.resource_group = AAZResourceGroupNameArg(
48+
required=True,
49+
)
50+
_args_schema.name = AAZStrArg(
51+
options=["-n", "--name"],
52+
help="The name of the Virtual Machine. You can configure the default using `az configure --defaults vm=<name>`.",
53+
required=True,
54+
id_part="name",
55+
)
56+
_args_schema.expire = AAZIntArg(
57+
options=["--expire"],
58+
help="Expiration duration in minutes for the SAS URIs with a value between 1 to 1440 minutes. If not specified, SAS URIs will be generated with a default expiration duration of 120 minutes.",
59+
)
60+
return cls._args_schema
61+
62+
def _execute_operations(self):
63+
self.pre_operations()
64+
self.VirtualMachinesRetrieveBootDiagnosticsData(ctx=self.ctx)()
65+
self.post_operations()
66+
67+
@register_callback
68+
def pre_operations(self):
69+
pass
70+
71+
@register_callback
72+
def post_operations(self):
73+
pass
74+
75+
def _output(self, *args, **kwargs):
76+
result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
77+
return result
78+
79+
class VirtualMachinesRetrieveBootDiagnosticsData(AAZHttpOperation):
80+
CLIENT_TYPE = "MgmtClient"
81+
82+
def __call__(self, *args, **kwargs):
83+
request = self.make_request()
84+
session = self.client.send_request(request=request, stream=False, **kwargs)
85+
if session.http_response.status_code in [200]:
86+
return self.on_200(session)
87+
88+
return self.on_error(session.http_response)
89+
90+
@property
91+
def url(self):
92+
return self.client.format_url(
93+
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/retrieveBootDiagnosticsData",
94+
**self.url_parameters
95+
)
96+
97+
@property
98+
def method(self):
99+
return "POST"
100+
101+
@property
102+
def error_format(self):
103+
return "ODataV4Format"
104+
105+
@property
106+
def url_parameters(self):
107+
parameters = {
108+
**self.serialize_url_param(
109+
"resourceGroupName", self.ctx.args.resource_group,
110+
required=True,
111+
),
112+
**self.serialize_url_param(
113+
"subscriptionId", self.ctx.subscription_id,
114+
required=True,
115+
),
116+
**self.serialize_url_param(
117+
"vmName", self.ctx.args.name,
118+
required=True,
119+
),
120+
}
121+
return parameters
122+
123+
@property
124+
def query_parameters(self):
125+
parameters = {
126+
**self.serialize_query_param(
127+
"sasUriExpirationTimeInMinutes", self.ctx.args.expire,
128+
),
129+
**self.serialize_query_param(
130+
"api-version", "2024-07-01",
131+
required=True,
132+
),
133+
}
134+
return parameters
135+
136+
@property
137+
def header_parameters(self):
138+
parameters = {
139+
**self.serialize_header_param(
140+
"Accept", "application/json",
141+
),
142+
}
143+
return parameters
144+
145+
def on_200(self, session):
146+
data = self.deserialize_http_content(session)
147+
self.ctx.set_var(
148+
"instance",
149+
data,
150+
schema_builder=self._build_schema_on_200
151+
)
152+
153+
_schema_on_200 = None
154+
155+
@classmethod
156+
def _build_schema_on_200(cls):
157+
if cls._schema_on_200 is not None:
158+
return cls._schema_on_200
159+
160+
cls._schema_on_200 = AAZObjectType()
161+
162+
_schema_on_200 = cls._schema_on_200
163+
_schema_on_200.console_screenshot_blob_uri = AAZStrType(
164+
serialized_name="consoleScreenshotBlobUri",
165+
flags={"read_only": True},
166+
)
167+
_schema_on_200.serial_console_log_blob_uri = AAZStrType(
168+
serialized_name="serialConsoleLogBlobUri",
169+
flags={"read_only": True},
170+
)
171+
172+
return cls._schema_on_200
173+
174+
175+
class _GetBootLogUrisHelper:
176+
"""Helper class for GetBootLogUris"""
177+
178+
179+
__all__ = ["GetBootLogUris"]

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ def load_command_table(self, _):
345345
g.custom_command('disable', 'disable_boot_diagnostics')
346346
g.custom_command('enable', 'enable_boot_diagnostics')
347347
g.custom_command('get-boot-log', 'get_boot_log')
348-
g.custom_command('get-boot-log-uris', 'get_boot_log_uris', min_api='2020-06-01')
349348

350349
with self.command_group('vm diagnostics', compute_vm_sdk) as g:
351350
g.custom_command('set', 'set_diagnostics_extension')

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

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from ._client_factory import (_compute_client_factory, cf_vm_image_term)
4848

4949
from .aaz.latest.vm.disk import AttachDetachDataDisk
50+
from .aaz.latest.vm import Update as UpdateVM
5051

5152
from .generated.custom import * # noqa: F403, pylint: disable=unused-wildcard-import,wildcard-import
5253
try:
@@ -1836,33 +1837,57 @@ def create_av_set(cmd, availability_set_name, resource_group_name, platform_faul
18361837

18371838

18381839
# region VirtualMachines BootDiagnostics
1839-
def disable_boot_diagnostics(cmd, resource_group_name, vm_name):
1840-
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
1841-
diag_profile = vm.diagnostics_profile
1842-
if not (diag_profile and diag_profile.boot_diagnostics and diag_profile.boot_diagnostics.enabled):
1843-
return
1840+
class DisableBootDiagnostics(UpdateVM):
1841+
def pre_instance_update(self, instance):
1842+
from azure.cli.core.aaz import has_value
1843+
diag_profile = False if not has_value(instance.properties.diagnostics_profile) else (
1844+
instance.properties.diagnostics_profile)
1845+
if not (diag_profile and has_value(diag_profile.boot_diagnostics) and
1846+
diag_profile.boot_diagnostics.enabled.to_serialized_data()):
1847+
return
1848+
boot_diag = {'enabled': False, 'storage_uri': None}
1849+
instance.properties.diagnostics_profile = {'boot_diagnostics': boot_diag}
18441850

1845-
diag_profile.boot_diagnostics.enabled = False
1846-
diag_profile.boot_diagnostics.storage_uri = None
1847-
set_vm(cmd, vm, ExtensionUpdateLongRunningOperation(cmd.cli_ctx, 'disabling boot diagnostics', 'done'))
18481851

1852+
def disable_boot_diagnostics(cmd, resource_group_name, vm_name):
1853+
ExtensionUpdateLongRunningOperation(cmd.cli_ctx, 'enabling boot diagnostics', 'done')(
1854+
DisableBootDiagnostics(cli_ctx=cmd.cli_ctx)(command_args={
1855+
'resource_group': resource_group_name,
1856+
'vm_name': vm_name
1857+
})
1858+
)
18491859

1850-
def enable_boot_diagnostics(cmd, resource_group_name, vm_name, storage=None):
1851-
from azure.cli.command_modules.vm._vm_utils import get_storage_blob_uri
1852-
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
1853-
storage_uri = None
1854-
if storage:
1855-
storage_uri = get_storage_blob_uri(cmd.cli_ctx, storage)
18561860

1857-
DiagnosticsProfile, BootDiagnostics = cmd.get_models('DiagnosticsProfile', 'BootDiagnostics')
1861+
class EnableBootDiagnostics(UpdateVM):
1862+
@classmethod
1863+
def _build_arguments_schema(cls, *args, **kwargs):
1864+
from azure.cli.core.aaz import AAZStrArg
1865+
args_schema = super()._build_arguments_schema(*args, **kwargs)
1866+
args_schema.storage = AAZStrArg(
1867+
options=["--storage"],
1868+
help="Storage account"
1869+
)
1870+
return args_schema
1871+
1872+
def pre_instance_update(self, instance):
1873+
from azure.cli.core.aaz import has_value
1874+
from azure.cli.command_modules.vm._vm_utils import get_storage_blob_uri
1875+
args = self.ctx.args
1876+
storage_uri = None
1877+
if has_value(args.storage):
1878+
storage_uri = get_storage_blob_uri(self.cli_ctx, args.storage.to_serialized_data())
1879+
boot_diag = {'enabled': True, 'storage_uri': storage_uri}
1880+
instance.properties.diagnostics_profile = {'boot_diagnostics': boot_diag}
18581881

1859-
boot_diag = BootDiagnostics(enabled=True, storage_uri=storage_uri)
1860-
if vm.diagnostics_profile is None:
1861-
vm.diagnostics_profile = DiagnosticsProfile(boot_diagnostics=boot_diag)
1862-
else:
1863-
vm.diagnostics_profile.boot_diagnostics = boot_diag
18641882

1865-
set_vm(cmd, vm, ExtensionUpdateLongRunningOperation(cmd.cli_ctx, 'enabling boot diagnostics', 'done'))
1883+
def enable_boot_diagnostics(cmd, resource_group_name, vm_name, storage=None):
1884+
ExtensionUpdateLongRunningOperation(cmd.cli_ctx, 'enabling boot diagnostics', 'done')(
1885+
EnableBootDiagnostics(cli_ctx=cmd.cli_ctx)(command_args={
1886+
'resource_group': resource_group_name,
1887+
'vm_name': vm_name,
1888+
'storage': storage
1889+
})
1890+
)
18661891

18671892

18681893
class BootLogStreamWriter: # pylint: disable=too-few-public-methods
@@ -1946,12 +1971,6 @@ def get_boot_log(cmd, resource_group_name, vm_name):
19461971

19471972
# our streamwriter not seekable, so no parallel.
19481973
storage_client.get_blob_to_stream(container, blob, BootLogStreamWriter(sys.stdout), max_connections=1)
1949-
1950-
1951-
def get_boot_log_uris(cmd, resource_group_name, vm_name, expire=None):
1952-
client = _compute_client_factory(cmd.cli_ctx)
1953-
return client.virtual_machines.retrieve_boot_diagnostics_data(
1954-
resource_group_name, vm_name, sas_uri_expiration_time_in_minutes=expire)
19551974
# endregion
19561975

19571976

0 commit comments

Comments
 (0)