|
| 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 | + "disk grant-access", |
| 16 | +) |
| 17 | +class GrantAccess(AAZCommand): |
| 18 | + """Grant a resource access to a managed disk. |
| 19 | +
|
| 20 | + :example: Grant a resource read access to a managed disk. |
| 21 | + az disk grant-access --access-level Read --duration-in-seconds 3600 --name MyManagedDisk --resource-group MyResourceGroup |
| 22 | +
|
| 23 | + :example: Grant a resource read access to a disk to generate access SAS and security data access SAS |
| 24 | + az disk grant-access --access-level Read --duration-in-seconds 3600 --name MyDisk --resource-group MyResourceGroup --secure-vm-guest-state-sas |
| 25 | + """ |
| 26 | + |
| 27 | + _aaz_info = { |
| 28 | + "version": "2023-04-02", |
| 29 | + "resources": [ |
| 30 | + ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.compute/disks/{}/begingetaccess", "2023-04-02"], |
| 31 | + ] |
| 32 | + } |
| 33 | + |
| 34 | + AZ_SUPPORT_NO_WAIT = True |
| 35 | + |
| 36 | + def _handler(self, command_args): |
| 37 | + super()._handler(command_args) |
| 38 | + return self.build_lro_poller(self._execute_operations, self._output) |
| 39 | + |
| 40 | + _args_schema = None |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def _build_arguments_schema(cls, *args, **kwargs): |
| 44 | + if cls._args_schema is not None: |
| 45 | + return cls._args_schema |
| 46 | + cls._args_schema = super()._build_arguments_schema(*args, **kwargs) |
| 47 | + |
| 48 | + # define Arg Group "" |
| 49 | + |
| 50 | + _args_schema = cls._args_schema |
| 51 | + _args_schema.disk_name = AAZStrArg( |
| 52 | + options=["-n", "--name", "--disk-name"], |
| 53 | + help="The name of the managed disk that is being created. The name can't be changed after the disk is created. Supported characters for the name are a-z, A-Z, 0-9, _ and -. The maximum name length is 80 characters.", |
| 54 | + required=True, |
| 55 | + id_part="name", |
| 56 | + ) |
| 57 | + _args_schema.resource_group = AAZResourceGroupNameArg( |
| 58 | + required=True, |
| 59 | + ) |
| 60 | + |
| 61 | + # define Arg Group "GrantAccessData" |
| 62 | + |
| 63 | + _args_schema = cls._args_schema |
| 64 | + _args_schema.access_level = AAZStrArg( |
| 65 | + options=["--access", "--access-level"], |
| 66 | + arg_group="GrantAccessData", |
| 67 | + help="Access level.", |
| 68 | + required=True, |
| 69 | + default="Read", |
| 70 | + enum={"None": "None", "Read": "Read", "Write": "Write"}, |
| 71 | + ) |
| 72 | + _args_schema.duration_in_seconds = AAZIntArg( |
| 73 | + options=["--duration-in-seconds"], |
| 74 | + arg_group="GrantAccessData", |
| 75 | + help="Time duration in seconds until the SAS access expires.", |
| 76 | + required=True, |
| 77 | + ) |
| 78 | + _args_schema.secure_vm_guest_state_sas = AAZBoolArg( |
| 79 | + options=["-s", "--secure-vm-guest-state-sas"], |
| 80 | + arg_group="GrantAccessData", |
| 81 | + help="Get SAS on managed disk with VM guest state. It will be used by default when the create option of disk is 'secureOSUpload'", |
| 82 | + ) |
| 83 | + return cls._args_schema |
| 84 | + |
| 85 | + def _execute_operations(self): |
| 86 | + self.pre_operations() |
| 87 | + yield self.DisksGrantAccess(ctx=self.ctx)() |
| 88 | + self.post_operations() |
| 89 | + |
| 90 | + @register_callback |
| 91 | + def pre_operations(self): |
| 92 | + pass |
| 93 | + |
| 94 | + @register_callback |
| 95 | + def post_operations(self): |
| 96 | + pass |
| 97 | + |
| 98 | + def _output(self, *args, **kwargs): |
| 99 | + result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True) |
| 100 | + return result |
| 101 | + |
| 102 | + class DisksGrantAccess(AAZHttpOperation): |
| 103 | + CLIENT_TYPE = "MgmtClient" |
| 104 | + |
| 105 | + def __call__(self, *args, **kwargs): |
| 106 | + request = self.make_request() |
| 107 | + session = self.client.send_request(request=request, stream=False, **kwargs) |
| 108 | + if session.http_response.status_code in [202]: |
| 109 | + return self.client.build_lro_polling( |
| 110 | + self.ctx.args.no_wait, |
| 111 | + session, |
| 112 | + self.on_200, |
| 113 | + self.on_error, |
| 114 | + lro_options={"final-state-via": "location"}, |
| 115 | + path_format_arguments=self.url_parameters, |
| 116 | + ) |
| 117 | + if session.http_response.status_code in [200]: |
| 118 | + return self.client.build_lro_polling( |
| 119 | + self.ctx.args.no_wait, |
| 120 | + session, |
| 121 | + self.on_200, |
| 122 | + self.on_error, |
| 123 | + lro_options={"final-state-via": "location"}, |
| 124 | + path_format_arguments=self.url_parameters, |
| 125 | + ) |
| 126 | + |
| 127 | + return self.on_error(session.http_response) |
| 128 | + |
| 129 | + @property |
| 130 | + def url(self): |
| 131 | + return self.client.format_url( |
| 132 | + "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/disks/{diskName}/beginGetAccess", |
| 133 | + **self.url_parameters |
| 134 | + ) |
| 135 | + |
| 136 | + @property |
| 137 | + def method(self): |
| 138 | + return "POST" |
| 139 | + |
| 140 | + @property |
| 141 | + def error_format(self): |
| 142 | + return "MgmtErrorFormat" |
| 143 | + |
| 144 | + @property |
| 145 | + def url_parameters(self): |
| 146 | + parameters = { |
| 147 | + **self.serialize_url_param( |
| 148 | + "diskName", self.ctx.args.disk_name, |
| 149 | + required=True, |
| 150 | + ), |
| 151 | + **self.serialize_url_param( |
| 152 | + "resourceGroupName", self.ctx.args.resource_group, |
| 153 | + required=True, |
| 154 | + ), |
| 155 | + **self.serialize_url_param( |
| 156 | + "subscriptionId", self.ctx.subscription_id, |
| 157 | + required=True, |
| 158 | + ), |
| 159 | + } |
| 160 | + return parameters |
| 161 | + |
| 162 | + @property |
| 163 | + def query_parameters(self): |
| 164 | + parameters = { |
| 165 | + **self.serialize_query_param( |
| 166 | + "api-version", "2023-04-02", |
| 167 | + required=True, |
| 168 | + ), |
| 169 | + } |
| 170 | + return parameters |
| 171 | + |
| 172 | + @property |
| 173 | + def header_parameters(self): |
| 174 | + parameters = { |
| 175 | + **self.serialize_header_param( |
| 176 | + "Content-Type", "application/json", |
| 177 | + ), |
| 178 | + **self.serialize_header_param( |
| 179 | + "Accept", "application/json", |
| 180 | + ), |
| 181 | + } |
| 182 | + return parameters |
| 183 | + |
| 184 | + @property |
| 185 | + def content(self): |
| 186 | + _content_value, _builder = self.new_content_builder( |
| 187 | + self.ctx.args, |
| 188 | + typ=AAZObjectType, |
| 189 | + typ_kwargs={"flags": {"required": True, "client_flatten": True}} |
| 190 | + ) |
| 191 | + _builder.set_prop("access", AAZStrType, ".access_level", typ_kwargs={"flags": {"required": True}}) |
| 192 | + _builder.set_prop("durationInSeconds", AAZIntType, ".duration_in_seconds", typ_kwargs={"flags": {"required": True}}) |
| 193 | + _builder.set_prop("getSecureVMGuestStateSAS", AAZBoolType, ".secure_vm_guest_state_sas") |
| 194 | + |
| 195 | + return self.serialize_content(_content_value) |
| 196 | + |
| 197 | + def on_200(self, session): |
| 198 | + data = self.deserialize_http_content(session) |
| 199 | + self.ctx.set_var( |
| 200 | + "instance", |
| 201 | + data, |
| 202 | + schema_builder=self._build_schema_on_200 |
| 203 | + ) |
| 204 | + |
| 205 | + _schema_on_200 = None |
| 206 | + |
| 207 | + @classmethod |
| 208 | + def _build_schema_on_200(cls): |
| 209 | + if cls._schema_on_200 is not None: |
| 210 | + return cls._schema_on_200 |
| 211 | + |
| 212 | + cls._schema_on_200 = AAZObjectType() |
| 213 | + |
| 214 | + _schema_on_200 = cls._schema_on_200 |
| 215 | + _schema_on_200.access_sas = AAZStrType( |
| 216 | + serialized_name="accessSAS", |
| 217 | + flags={"read_only": True}, |
| 218 | + ) |
| 219 | + _schema_on_200.security_data_access_sas = AAZStrType( |
| 220 | + serialized_name="securityDataAccessSAS", |
| 221 | + flags={"read_only": True}, |
| 222 | + ) |
| 223 | + |
| 224 | + return cls._schema_on_200 |
| 225 | + |
| 226 | + |
| 227 | +class _GrantAccessHelper: |
| 228 | + """Helper class for GrantAccess""" |
| 229 | + |
| 230 | + |
| 231 | +__all__ = ["GrantAccess"] |
0 commit comments