1# coding=utf-8 2# -------------------------------------------------------------------------- 3# Copyright (c) Microsoft Corporation. All rights reserved. 4# Licensed under the MIT License. See License.txt in the project root for license information. 5# Code generated by Microsoft (R) AutoRest Code Generator. 6# Changes may cause incorrect behavior and will be lost if the code is regenerated. 7# -------------------------------------------------------------------------- 8from typing import Any, AsyncIterable, Callable, Dict, Generic, Optional, TypeVar, Union 9import warnings 10 11from azure.core.async_paging import AsyncItemPaged, AsyncList 12from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error 13from azure.core.pipeline import PipelineResponse 14from azure.core.pipeline.transport import AsyncHttpResponse, HttpRequest 15from azure.core.polling import AsyncLROPoller, AsyncNoPolling, AsyncPollingMethod 16from azure.mgmt.core.exceptions import ARMErrorFormat 17from azure.mgmt.core.polling.async_arm_polling import AsyncARMPolling 18 19from ... import models as _models 20 21T = TypeVar('T') 22ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]] 23 24class NatGatewaysOperations: 25 """NatGatewaysOperations async operations. 26 27 You should not instantiate this class directly. Instead, you should create a Client instance that 28 instantiates it for you and attaches it as an attribute. 29 30 :ivar models: Alias to model classes used in this operation group. 31 :type models: ~azure.mgmt.network.v2020_03_01.models 32 :param client: Client for service requests. 33 :param config: Configuration of service client. 34 :param serializer: An object model serializer. 35 :param deserializer: An object model deserializer. 36 """ 37 38 models = _models 39 40 def __init__(self, client, config, serializer, deserializer) -> None: 41 self._client = client 42 self._serialize = serializer 43 self._deserialize = deserializer 44 self._config = config 45 46 async def _delete_initial( 47 self, 48 resource_group_name: str, 49 nat_gateway_name: str, 50 **kwargs 51 ) -> None: 52 cls = kwargs.pop('cls', None) # type: ClsType[None] 53 error_map = { 54 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError 55 } 56 error_map.update(kwargs.pop('error_map', {})) 57 api_version = "2020-03-01" 58 accept = "application/json" 59 60 # Construct URL 61 url = self._delete_initial.metadata['url'] # type: ignore 62 path_format_arguments = { 63 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 64 'natGatewayName': self._serialize.url("nat_gateway_name", nat_gateway_name, 'str'), 65 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), 66 } 67 url = self._client.format_url(url, **path_format_arguments) 68 69 # Construct parameters 70 query_parameters = {} # type: Dict[str, Any] 71 query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') 72 73 # Construct headers 74 header_parameters = {} # type: Dict[str, Any] 75 header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') 76 77 request = self._client.delete(url, query_parameters, header_parameters) 78 pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) 79 response = pipeline_response.http_response 80 81 if response.status_code not in [200, 202, 204]: 82 map_error(status_code=response.status_code, response=response, error_map=error_map) 83 raise HttpResponseError(response=response, error_format=ARMErrorFormat) 84 85 if cls: 86 return cls(pipeline_response, None, {}) 87 88 _delete_initial.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/natGateways/{natGatewayName}'} # type: ignore 89 90 async def begin_delete( 91 self, 92 resource_group_name: str, 93 nat_gateway_name: str, 94 **kwargs 95 ) -> AsyncLROPoller[None]: 96 """Deletes the specified nat gateway. 97 98 :param resource_group_name: The name of the resource group. 99 :type resource_group_name: str 100 :param nat_gateway_name: The name of the nat gateway. 101 :type nat_gateway_name: str 102 :keyword callable cls: A custom type or function that will be passed the direct response 103 :keyword str continuation_token: A continuation token to restart a poller from a saved state. 104 :keyword polling: Pass in True if you'd like the AsyncARMPolling polling method, 105 False for no polling, or your own initialized polling object for a personal polling strategy. 106 :paramtype polling: bool or ~azure.core.polling.AsyncPollingMethod 107 :keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present. 108 :return: An instance of AsyncLROPoller that returns either None or the result of cls(response) 109 :rtype: ~azure.core.polling.AsyncLROPoller[None] 110 :raises ~azure.core.exceptions.HttpResponseError: 111 """ 112 polling = kwargs.pop('polling', True) # type: Union[bool, AsyncPollingMethod] 113 cls = kwargs.pop('cls', None) # type: ClsType[None] 114 lro_delay = kwargs.pop( 115 'polling_interval', 116 self._config.polling_interval 117 ) 118 cont_token = kwargs.pop('continuation_token', None) # type: Optional[str] 119 if cont_token is None: 120 raw_result = await self._delete_initial( 121 resource_group_name=resource_group_name, 122 nat_gateway_name=nat_gateway_name, 123 cls=lambda x,y,z: x, 124 **kwargs 125 ) 126 127 kwargs.pop('error_map', None) 128 kwargs.pop('content_type', None) 129 130 def get_long_running_output(pipeline_response): 131 if cls: 132 return cls(pipeline_response, None, {}) 133 134 path_format_arguments = { 135 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 136 'natGatewayName': self._serialize.url("nat_gateway_name", nat_gateway_name, 'str'), 137 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), 138 } 139 140 if polling is True: polling_method = AsyncARMPolling(lro_delay, lro_options={'final-state-via': 'location'}, path_format_arguments=path_format_arguments, **kwargs) 141 elif polling is False: polling_method = AsyncNoPolling() 142 else: polling_method = polling 143 if cont_token: 144 return AsyncLROPoller.from_continuation_token( 145 polling_method=polling_method, 146 continuation_token=cont_token, 147 client=self._client, 148 deserialization_callback=get_long_running_output 149 ) 150 else: 151 return AsyncLROPoller(self._client, raw_result, get_long_running_output, polling_method) 152 begin_delete.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/natGateways/{natGatewayName}'} # type: ignore 153 154 async def get( 155 self, 156 resource_group_name: str, 157 nat_gateway_name: str, 158 expand: Optional[str] = None, 159 **kwargs 160 ) -> "_models.NatGateway": 161 """Gets the specified nat gateway in a specified resource group. 162 163 :param resource_group_name: The name of the resource group. 164 :type resource_group_name: str 165 :param nat_gateway_name: The name of the nat gateway. 166 :type nat_gateway_name: str 167 :param expand: Expands referenced resources. 168 :type expand: str 169 :keyword callable cls: A custom type or function that will be passed the direct response 170 :return: NatGateway, or the result of cls(response) 171 :rtype: ~azure.mgmt.network.v2020_03_01.models.NatGateway 172 :raises: ~azure.core.exceptions.HttpResponseError 173 """ 174 cls = kwargs.pop('cls', None) # type: ClsType["_models.NatGateway"] 175 error_map = { 176 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError 177 } 178 error_map.update(kwargs.pop('error_map', {})) 179 api_version = "2020-03-01" 180 accept = "application/json" 181 182 # Construct URL 183 url = self.get.metadata['url'] # type: ignore 184 path_format_arguments = { 185 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 186 'natGatewayName': self._serialize.url("nat_gateway_name", nat_gateway_name, 'str'), 187 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), 188 } 189 url = self._client.format_url(url, **path_format_arguments) 190 191 # Construct parameters 192 query_parameters = {} # type: Dict[str, Any] 193 query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') 194 if expand is not None: 195 query_parameters['$expand'] = self._serialize.query("expand", expand, 'str') 196 197 # Construct headers 198 header_parameters = {} # type: Dict[str, Any] 199 header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') 200 201 request = self._client.get(url, query_parameters, header_parameters) 202 pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) 203 response = pipeline_response.http_response 204 205 if response.status_code not in [200]: 206 map_error(status_code=response.status_code, response=response, error_map=error_map) 207 raise HttpResponseError(response=response, error_format=ARMErrorFormat) 208 209 deserialized = self._deserialize('NatGateway', pipeline_response) 210 211 if cls: 212 return cls(pipeline_response, deserialized, {}) 213 214 return deserialized 215 get.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/natGateways/{natGatewayName}'} # type: ignore 216 217 async def _create_or_update_initial( 218 self, 219 resource_group_name: str, 220 nat_gateway_name: str, 221 parameters: "_models.NatGateway", 222 **kwargs 223 ) -> "_models.NatGateway": 224 cls = kwargs.pop('cls', None) # type: ClsType["_models.NatGateway"] 225 error_map = { 226 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError 227 } 228 error_map.update(kwargs.pop('error_map', {})) 229 api_version = "2020-03-01" 230 content_type = kwargs.pop("content_type", "application/json") 231 accept = "application/json" 232 233 # Construct URL 234 url = self._create_or_update_initial.metadata['url'] # type: ignore 235 path_format_arguments = { 236 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 237 'natGatewayName': self._serialize.url("nat_gateway_name", nat_gateway_name, 'str'), 238 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), 239 } 240 url = self._client.format_url(url, **path_format_arguments) 241 242 # Construct parameters 243 query_parameters = {} # type: Dict[str, Any] 244 query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') 245 246 # Construct headers 247 header_parameters = {} # type: Dict[str, Any] 248 header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str') 249 header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') 250 251 body_content_kwargs = {} # type: Dict[str, Any] 252 body_content = self._serialize.body(parameters, 'NatGateway') 253 body_content_kwargs['content'] = body_content 254 request = self._client.put(url, query_parameters, header_parameters, **body_content_kwargs) 255 pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) 256 response = pipeline_response.http_response 257 258 if response.status_code not in [200, 201, 202]: 259 map_error(status_code=response.status_code, response=response, error_map=error_map) 260 raise HttpResponseError(response=response, error_format=ARMErrorFormat) 261 262 if response.status_code == 200: 263 deserialized = self._deserialize('NatGateway', pipeline_response) 264 265 if response.status_code == 201: 266 deserialized = self._deserialize('NatGateway', pipeline_response) 267 268 if response.status_code == 202: 269 deserialized = self._deserialize('NatGateway', pipeline_response) 270 271 if cls: 272 return cls(pipeline_response, deserialized, {}) 273 274 return deserialized 275 _create_or_update_initial.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/natGateways/{natGatewayName}'} # type: ignore 276 277 async def begin_create_or_update( 278 self, 279 resource_group_name: str, 280 nat_gateway_name: str, 281 parameters: "_models.NatGateway", 282 **kwargs 283 ) -> AsyncLROPoller["_models.NatGateway"]: 284 """Creates or updates a nat gateway. 285 286 :param resource_group_name: The name of the resource group. 287 :type resource_group_name: str 288 :param nat_gateway_name: The name of the nat gateway. 289 :type nat_gateway_name: str 290 :param parameters: Parameters supplied to the create or update nat gateway operation. 291 :type parameters: ~azure.mgmt.network.v2020_03_01.models.NatGateway 292 :keyword callable cls: A custom type or function that will be passed the direct response 293 :keyword str continuation_token: A continuation token to restart a poller from a saved state. 294 :keyword polling: Pass in True if you'd like the AsyncARMPolling polling method, 295 False for no polling, or your own initialized polling object for a personal polling strategy. 296 :paramtype polling: bool or ~azure.core.polling.AsyncPollingMethod 297 :keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present. 298 :return: An instance of AsyncLROPoller that returns either NatGateway or the result of cls(response) 299 :rtype: ~azure.core.polling.AsyncLROPoller[~azure.mgmt.network.v2020_03_01.models.NatGateway] 300 :raises ~azure.core.exceptions.HttpResponseError: 301 """ 302 polling = kwargs.pop('polling', True) # type: Union[bool, AsyncPollingMethod] 303 cls = kwargs.pop('cls', None) # type: ClsType["_models.NatGateway"] 304 lro_delay = kwargs.pop( 305 'polling_interval', 306 self._config.polling_interval 307 ) 308 cont_token = kwargs.pop('continuation_token', None) # type: Optional[str] 309 if cont_token is None: 310 raw_result = await self._create_or_update_initial( 311 resource_group_name=resource_group_name, 312 nat_gateway_name=nat_gateway_name, 313 parameters=parameters, 314 cls=lambda x,y,z: x, 315 **kwargs 316 ) 317 318 kwargs.pop('error_map', None) 319 kwargs.pop('content_type', None) 320 321 def get_long_running_output(pipeline_response): 322 deserialized = self._deserialize('NatGateway', pipeline_response) 323 324 if cls: 325 return cls(pipeline_response, deserialized, {}) 326 return deserialized 327 328 path_format_arguments = { 329 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 330 'natGatewayName': self._serialize.url("nat_gateway_name", nat_gateway_name, 'str'), 331 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), 332 } 333 334 if polling is True: polling_method = AsyncARMPolling(lro_delay, lro_options={'final-state-via': 'azure-async-operation'}, path_format_arguments=path_format_arguments, **kwargs) 335 elif polling is False: polling_method = AsyncNoPolling() 336 else: polling_method = polling 337 if cont_token: 338 return AsyncLROPoller.from_continuation_token( 339 polling_method=polling_method, 340 continuation_token=cont_token, 341 client=self._client, 342 deserialization_callback=get_long_running_output 343 ) 344 else: 345 return AsyncLROPoller(self._client, raw_result, get_long_running_output, polling_method) 346 begin_create_or_update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/natGateways/{natGatewayName}'} # type: ignore 347 348 async def update_tags( 349 self, 350 resource_group_name: str, 351 nat_gateway_name: str, 352 parameters: "_models.TagsObject", 353 **kwargs 354 ) -> "_models.NatGateway": 355 """Updates nat gateway tags. 356 357 :param resource_group_name: The name of the resource group. 358 :type resource_group_name: str 359 :param nat_gateway_name: The name of the nat gateway. 360 :type nat_gateway_name: str 361 :param parameters: Parameters supplied to update nat gateway tags. 362 :type parameters: ~azure.mgmt.network.v2020_03_01.models.TagsObject 363 :keyword callable cls: A custom type or function that will be passed the direct response 364 :return: NatGateway, or the result of cls(response) 365 :rtype: ~azure.mgmt.network.v2020_03_01.models.NatGateway 366 :raises: ~azure.core.exceptions.HttpResponseError 367 """ 368 cls = kwargs.pop('cls', None) # type: ClsType["_models.NatGateway"] 369 error_map = { 370 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError 371 } 372 error_map.update(kwargs.pop('error_map', {})) 373 api_version = "2020-03-01" 374 content_type = kwargs.pop("content_type", "application/json") 375 accept = "application/json" 376 377 # Construct URL 378 url = self.update_tags.metadata['url'] # type: ignore 379 path_format_arguments = { 380 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 381 'natGatewayName': self._serialize.url("nat_gateway_name", nat_gateway_name, 'str'), 382 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), 383 } 384 url = self._client.format_url(url, **path_format_arguments) 385 386 # Construct parameters 387 query_parameters = {} # type: Dict[str, Any] 388 query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') 389 390 # Construct headers 391 header_parameters = {} # type: Dict[str, Any] 392 header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str') 393 header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') 394 395 body_content_kwargs = {} # type: Dict[str, Any] 396 body_content = self._serialize.body(parameters, 'TagsObject') 397 body_content_kwargs['content'] = body_content 398 request = self._client.patch(url, query_parameters, header_parameters, **body_content_kwargs) 399 pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) 400 response = pipeline_response.http_response 401 402 if response.status_code not in [200]: 403 map_error(status_code=response.status_code, response=response, error_map=error_map) 404 raise HttpResponseError(response=response, error_format=ARMErrorFormat) 405 406 deserialized = self._deserialize('NatGateway', pipeline_response) 407 408 if cls: 409 return cls(pipeline_response, deserialized, {}) 410 411 return deserialized 412 update_tags.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/natGateways/{natGatewayName}'} # type: ignore 413 414 def list_all( 415 self, 416 **kwargs 417 ) -> AsyncIterable["_models.NatGatewayListResult"]: 418 """Gets all the Nat Gateways in a subscription. 419 420 :keyword callable cls: A custom type or function that will be passed the direct response 421 :return: An iterator like instance of either NatGatewayListResult or the result of cls(response) 422 :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.network.v2020_03_01.models.NatGatewayListResult] 423 :raises: ~azure.core.exceptions.HttpResponseError 424 """ 425 cls = kwargs.pop('cls', None) # type: ClsType["_models.NatGatewayListResult"] 426 error_map = { 427 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError 428 } 429 error_map.update(kwargs.pop('error_map', {})) 430 api_version = "2020-03-01" 431 accept = "application/json" 432 433 def prepare_request(next_link=None): 434 # Construct headers 435 header_parameters = {} # type: Dict[str, Any] 436 header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') 437 438 if not next_link: 439 # Construct URL 440 url = self.list_all.metadata['url'] # type: ignore 441 path_format_arguments = { 442 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), 443 } 444 url = self._client.format_url(url, **path_format_arguments) 445 # Construct parameters 446 query_parameters = {} # type: Dict[str, Any] 447 query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') 448 449 request = self._client.get(url, query_parameters, header_parameters) 450 else: 451 url = next_link 452 query_parameters = {} # type: Dict[str, Any] 453 request = self._client.get(url, query_parameters, header_parameters) 454 return request 455 456 async def extract_data(pipeline_response): 457 deserialized = self._deserialize('NatGatewayListResult', pipeline_response) 458 list_of_elem = deserialized.value 459 if cls: 460 list_of_elem = cls(list_of_elem) 461 return deserialized.next_link or None, AsyncList(list_of_elem) 462 463 async def get_next(next_link=None): 464 request = prepare_request(next_link) 465 466 pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) 467 response = pipeline_response.http_response 468 469 if response.status_code not in [200]: 470 map_error(status_code=response.status_code, response=response, error_map=error_map) 471 raise HttpResponseError(response=response, error_format=ARMErrorFormat) 472 473 return pipeline_response 474 475 return AsyncItemPaged( 476 get_next, extract_data 477 ) 478 list_all.metadata = {'url': '/subscriptions/{subscriptionId}/providers/Microsoft.Network/natGateways'} # type: ignore 479 480 def list( 481 self, 482 resource_group_name: str, 483 **kwargs 484 ) -> AsyncIterable["_models.NatGatewayListResult"]: 485 """Gets all nat gateways in a resource group. 486 487 :param resource_group_name: The name of the resource group. 488 :type resource_group_name: str 489 :keyword callable cls: A custom type or function that will be passed the direct response 490 :return: An iterator like instance of either NatGatewayListResult or the result of cls(response) 491 :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.network.v2020_03_01.models.NatGatewayListResult] 492 :raises: ~azure.core.exceptions.HttpResponseError 493 """ 494 cls = kwargs.pop('cls', None) # type: ClsType["_models.NatGatewayListResult"] 495 error_map = { 496 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError 497 } 498 error_map.update(kwargs.pop('error_map', {})) 499 api_version = "2020-03-01" 500 accept = "application/json" 501 502 def prepare_request(next_link=None): 503 # Construct headers 504 header_parameters = {} # type: Dict[str, Any] 505 header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') 506 507 if not next_link: 508 # Construct URL 509 url = self.list.metadata['url'] # type: ignore 510 path_format_arguments = { 511 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 512 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), 513 } 514 url = self._client.format_url(url, **path_format_arguments) 515 # Construct parameters 516 query_parameters = {} # type: Dict[str, Any] 517 query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') 518 519 request = self._client.get(url, query_parameters, header_parameters) 520 else: 521 url = next_link 522 query_parameters = {} # type: Dict[str, Any] 523 request = self._client.get(url, query_parameters, header_parameters) 524 return request 525 526 async def extract_data(pipeline_response): 527 deserialized = self._deserialize('NatGatewayListResult', pipeline_response) 528 list_of_elem = deserialized.value 529 if cls: 530 list_of_elem = cls(list_of_elem) 531 return deserialized.next_link or None, AsyncList(list_of_elem) 532 533 async def get_next(next_link=None): 534 request = prepare_request(next_link) 535 536 pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) 537 response = pipeline_response.http_response 538 539 if response.status_code not in [200]: 540 map_error(status_code=response.status_code, response=response, error_map=error_map) 541 raise HttpResponseError(response=response, error_format=ARMErrorFormat) 542 543 return pipeline_response 544 545 return AsyncItemPaged( 546 get_next, extract_data 547 ) 548 list.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/natGateways'} # type: ignore 549