-
Notifications
You must be signed in to change notification settings - Fork 3.4k
{Compute} az vm list-usage & az vm stop: Migrate commands to aaz-based implementation
#32925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 4 commits
fb92bab
3ae8f3a
1b56d85
cfc36e4
b49ed65
a6ba7e3
d80cfc4
afc30df
96989df
be24dbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # | ||
| # Code generated by aaz-dev-tools | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| # pylint: skip-file | ||
| # flake8: noqa | ||
|
|
||
| from azure.cli.core.aaz import * | ||
|
|
||
|
|
||
| @register_command( | ||
| "vm list-usage", | ||
| ) | ||
| class ListUsage(AAZCommand): | ||
| """List available usage resources for VMs. | ||
|
|
||
| :example: Get the compute resource usage for the West US region. | ||
| az vm list-usage -l westus | ||
| """ | ||
|
|
||
| _aaz_info = { | ||
| "version": "2024-11-01", | ||
| "resources": [ | ||
| ["mgmt-plane", "/subscriptions/{}/providers/microsoft.compute/locations/{}/usages", "2024-11-01"], | ||
| ] | ||
| } | ||
|
|
||
| AZ_SUPPORT_PAGINATION = True | ||
|
|
||
| def _handler(self, command_args): | ||
| super()._handler(command_args) | ||
| return self.build_paging(self._execute_operations, self._output) | ||
|
|
||
| _args_schema = None | ||
|
|
||
| @classmethod | ||
| def _build_arguments_schema(cls, *args, **kwargs): | ||
| if cls._args_schema is not None: | ||
| return cls._args_schema | ||
| cls._args_schema = super()._build_arguments_schema(*args, **kwargs) | ||
|
|
||
| # define Arg Group "" | ||
|
|
||
| _args_schema = cls._args_schema | ||
| _args_schema.location = AAZResourceLocationArg( | ||
| help="Location. Values from: `az account list-locations`. You can configure the default location using `az configure --defaults location=<location>`.", | ||
| required=True, | ||
| ) | ||
| return cls._args_schema | ||
|
|
||
| def _execute_operations(self): | ||
| self.pre_operations() | ||
| self.UsageList(ctx=self.ctx)() | ||
| self.post_operations() | ||
|
|
||
| @register_callback | ||
| def pre_operations(self): | ||
| pass | ||
|
|
||
| @register_callback | ||
| def post_operations(self): | ||
| pass | ||
|
|
||
| def _output(self, *args, **kwargs): | ||
| result = self.deserialize_output(self.ctx.vars.instance.value, client_flatten=True) | ||
| next_link = self.deserialize_output(self.ctx.vars.instance.next_link) | ||
| return result, next_link | ||
|
|
||
| class UsageList(AAZHttpOperation): | ||
| CLIENT_TYPE = "MgmtClient" | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| request = self.make_request() | ||
| session = self.client.send_request(request=request, stream=False, **kwargs) | ||
| if session.http_response.status_code in [200]: | ||
| return self.on_200(session) | ||
|
|
||
| return self.on_error(session.http_response) | ||
|
|
||
| @property | ||
| def url(self): | ||
| return self.client.format_url( | ||
| "/subscriptions/{subscriptionId}/providers/Microsoft.Compute/locations/{location}/usages", | ||
| **self.url_parameters | ||
| ) | ||
|
|
||
| @property | ||
| def method(self): | ||
| return "GET" | ||
|
|
||
| @property | ||
| def error_format(self): | ||
| return "ODataV4Format" | ||
|
|
||
| @property | ||
| def url_parameters(self): | ||
| parameters = { | ||
| **self.serialize_url_param( | ||
| "location", self.ctx.args.location, | ||
| required=True, | ||
| ), | ||
| **self.serialize_url_param( | ||
| "subscriptionId", self.ctx.subscription_id, | ||
| required=True, | ||
| ), | ||
| } | ||
| return parameters | ||
|
|
||
| @property | ||
| def query_parameters(self): | ||
| parameters = { | ||
| **self.serialize_query_param( | ||
| "api-version", "2024-11-01", | ||
| required=True, | ||
| ), | ||
| } | ||
| return parameters | ||
|
|
||
| @property | ||
| def header_parameters(self): | ||
| parameters = { | ||
| **self.serialize_header_param( | ||
| "Accept", "application/json", | ||
| ), | ||
| } | ||
| return parameters | ||
|
|
||
| def on_200(self, session): | ||
| data = self.deserialize_http_content(session) | ||
| self.ctx.set_var( | ||
| "instance", | ||
| data, | ||
| schema_builder=self._build_schema_on_200 | ||
| ) | ||
|
|
||
| _schema_on_200 = None | ||
|
|
||
| @classmethod | ||
| def _build_schema_on_200(cls): | ||
| if cls._schema_on_200 is not None: | ||
| return cls._schema_on_200 | ||
|
|
||
| cls._schema_on_200 = AAZObjectType() | ||
|
|
||
| _schema_on_200 = cls._schema_on_200 | ||
| _schema_on_200.next_link = AAZStrType( | ||
| serialized_name="nextLink", | ||
| ) | ||
| _schema_on_200.value = AAZListType( | ||
| flags={"required": True}, | ||
| ) | ||
|
|
||
| value = cls._schema_on_200.value | ||
| value.Element = AAZObjectType() | ||
|
|
||
| _element = cls._schema_on_200.value.Element | ||
| _element.current_value = AAZIntType( | ||
| serialized_name="currentValue", | ||
| flags={"required": True}, | ||
| ) | ||
| _element.limit = AAZIntType( | ||
| flags={"required": True}, | ||
| ) | ||
| _element.name = AAZObjectType( | ||
| flags={"required": True}, | ||
| ) | ||
| _element.unit = AAZStrType( | ||
| flags={"required": True}, | ||
| ) | ||
|
|
||
| name = cls._schema_on_200.value.Element.name | ||
| name.localized_value = AAZStrType( | ||
| serialized_name="localizedValue", | ||
| ) | ||
| name.value = AAZStrType() | ||
|
|
||
| return cls._schema_on_200 | ||
|
|
||
|
|
||
| class _ListUsageHelper: | ||
| """Helper class for ListUsage""" | ||
|
|
||
|
|
||
| __all__ = ["ListUsage"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # | ||
| # Code generated by aaz-dev-tools | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| # pylint: skip-file | ||
| # flake8: noqa | ||
|
|
||
| from azure.cli.core.aaz import * | ||
|
|
||
|
|
||
| @register_command( | ||
| "vm stop", | ||
| ) | ||
| class Stop(AAZCommand): | ||
| """Power off (stop) a running VM. | ||
|
|
||
| The VM will continue to be billed. To avoid this, you can deallocate the VM through "az vm deallocate". | ||
|
|
||
| :example: Power off (stop) a running VM. | ||
| az vm stop --resource-group MyResourceGroup --name MyVm | ||
|
|
||
| :example: Power off a running VM without shutting down. | ||
| az vm stop --resource-group MyResourceGroup --name MyVm --skip-shutdow | ||
|
|
||
| :example: Power off VMs in a resource group. | ||
| az vm stop --ids $(az vm list -g MyResourceGroup --query "[].id" -o tsv) | ||
| """ | ||
|
|
||
| _aaz_info = { | ||
| "version": "2024-11-01", | ||
| "resources": [ | ||
| ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.compute/virtualmachines/{}/poweroff", "2024-11-01"], | ||
| ] | ||
| } | ||
|
|
||
| AZ_SUPPORT_NO_WAIT = True | ||
|
|
||
| def _handler(self, command_args): | ||
| super()._handler(command_args) | ||
| return self.build_lro_poller(self._execute_operations, None) | ||
|
|
||
| _args_schema = None | ||
|
|
||
| @classmethod | ||
| def _build_arguments_schema(cls, *args, **kwargs): | ||
| if cls._args_schema is not None: | ||
| return cls._args_schema | ||
| cls._args_schema = super()._build_arguments_schema(*args, **kwargs) | ||
|
|
||
| # define Arg Group "" | ||
|
|
||
| _args_schema = cls._args_schema | ||
| _args_schema.resource_group = AAZResourceGroupNameArg( | ||
| required=True, | ||
| ) | ||
| _args_schema.name = AAZStrArg( | ||
| options=["-n", "--name"], | ||
| help="The name of the virtual machine.", | ||
| required=True, | ||
| id_part="name", | ||
| ) | ||
|
Comment on lines
+59
to
+64
|
||
| _args_schema.skip_shutdown = AAZBoolArg( | ||
| options=["--skip-shutdown"], | ||
| 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", | ||
| ) | ||
| return cls._args_schema | ||
|
|
||
| def _execute_operations(self): | ||
| self.pre_operations() | ||
| yield self.VirtualMachinesPowerOff(ctx=self.ctx)() | ||
| self.post_operations() | ||
|
|
||
| @register_callback | ||
| def pre_operations(self): | ||
| pass | ||
|
|
||
| @register_callback | ||
| def post_operations(self): | ||
| pass | ||
|
|
||
| class VirtualMachinesPowerOff(AAZHttpOperation): | ||
| CLIENT_TYPE = "MgmtClient" | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| request = self.make_request() | ||
| session = self.client.send_request(request=request, stream=False, **kwargs) | ||
| if session.http_response.status_code in [202]: | ||
| return self.client.build_lro_polling( | ||
| self.ctx.args.no_wait, | ||
| session, | ||
| self.on_200, | ||
| self.on_error, | ||
| lro_options={"final-state-via": "location"}, | ||
| path_format_arguments=self.url_parameters, | ||
| ) | ||
| if session.http_response.status_code in [200]: | ||
| return self.client.build_lro_polling( | ||
| self.ctx.args.no_wait, | ||
| session, | ||
| self.on_200, | ||
| self.on_error, | ||
| lro_options={"final-state-via": "location"}, | ||
| path_format_arguments=self.url_parameters, | ||
| ) | ||
|
|
||
| return self.on_error(session.http_response) | ||
|
|
||
| @property | ||
| def url(self): | ||
| return self.client.format_url( | ||
| "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/powerOff", | ||
| **self.url_parameters | ||
| ) | ||
|
|
||
| @property | ||
| def method(self): | ||
| return "POST" | ||
|
|
||
| @property | ||
| def error_format(self): | ||
| return "ODataV4Format" | ||
|
|
||
| @property | ||
| def url_parameters(self): | ||
| parameters = { | ||
| **self.serialize_url_param( | ||
| "resourceGroupName", self.ctx.args.resource_group, | ||
| required=True, | ||
| ), | ||
| **self.serialize_url_param( | ||
| "subscriptionId", self.ctx.subscription_id, | ||
| required=True, | ||
| ), | ||
| **self.serialize_url_param( | ||
| "vmName", self.ctx.args.name, | ||
| required=True, | ||
| ), | ||
| } | ||
| return parameters | ||
|
|
||
| @property | ||
| def query_parameters(self): | ||
| parameters = { | ||
| **self.serialize_query_param( | ||
| "skipShutdown", self.ctx.args.skip_shutdown, | ||
| ), | ||
| **self.serialize_query_param( | ||
| "api-version", "2024-11-01", | ||
| required=True, | ||
| ), | ||
| } | ||
| return parameters | ||
|
|
||
| def on_200(self, session): | ||
| pass | ||
|
|
||
|
|
||
| class _StopHelper: | ||
| """Helper class for Stop""" | ||
|
|
||
|
|
||
| __all__ = ["Stop"] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in the example:
--skip-shutdowis missing the trailingn. It should be--skip-shutdown.