Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/azure/mgmt/core/policies/_base_async.py: 30%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

37 statements  

1# -------------------------------------------------------------------------- 

2# 

3# Copyright (c) Microsoft Corporation. All rights reserved. 

4# 

5# The MIT License (MIT) 

6# 

7# Permission is hereby granted, free of charge, to any person obtaining a copy 

8# of this software and associated documentation files (the ""Software""), to 

9# deal in the Software without restriction, including without limitation the 

10# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 

11# sell copies of the Software, and to permit persons to whom the Software is 

12# furnished to do so, subject to the following conditions: 

13# 

14# The above copyright notice and this permission notice shall be included in 

15# all copies or substantial portions of the Software. 

16# 

17# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 

22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 

23# IN THE SOFTWARE. 

24# 

25# -------------------------------------------------------------------------- 

26import asyncio 

27import json 

28import logging 

29import uuid 

30 

31from azure.core.pipeline import PipelineRequest 

32from azure.core.pipeline.policies import AsyncHTTPPolicy 

33 

34from . import ARMAutoResourceProviderRegistrationPolicy 

35 

36_LOGGER = logging.getLogger(__name__) 

37 

38 

39class AsyncARMAutoResourceProviderRegistrationPolicy(ARMAutoResourceProviderRegistrationPolicy, AsyncHTTPPolicy): 

40 """Auto register an ARM resource provider if not done yet.""" 

41 

42 async def send(self, request: PipelineRequest): # pylint: disable=invalid-overridden-method 

43 http_request = request.http_request 

44 response = await self.next.send(request) 

45 if response.http_response.status_code == 409: 

46 rp_name = self._check_rp_not_registered_err(response) 

47 if rp_name: 

48 url_prefix = self._extract_subscription_url(http_request.url) 

49 register_rp_status = await self._async_register_rp(request, url_prefix, rp_name) 

50 if not register_rp_status: 

51 return response 

52 # Change the 'x-ms-client-request-id' otherwise the Azure endpoint 

53 # just returns the same 409 payload without looking at the actual query 

54 if "x-ms-client-request-id" in http_request.headers: 

55 http_request.headers["x-ms-client-request-id"] = str(uuid.uuid4()) 

56 response = await self.next.send(request) 

57 return response 

58 

59 async def _async_register_rp(self, initial_request, url_prefix, rp_name): 

60 """Synchronously register the RP is paremeter. 

61 

62 Return False if we have a reason to believe this didn't work 

63 """ 

64 post_url = "{}providers/{}/register?api-version=2016-02-01".format(url_prefix, rp_name) 

65 get_url = "{}providers/{}?api-version=2016-02-01".format(url_prefix, rp_name) 

66 _LOGGER.warning( 

67 "Resource provider '%s' used by this operation is not " "registered. We are registering for you.", 

68 rp_name, 

69 ) 

70 post_response = await self.next.send(self._build_next_request(initial_request, "POST", post_url)) 

71 if post_response.http_response.status_code != 200: 

72 _LOGGER.warning("Registration failed. Please register manually.") 

73 return False 

74 

75 while True: 

76 await asyncio.sleep(10) 

77 get_response = await self.next.send(self._build_next_request(initial_request, "GET", get_url)) 

78 rp_info = json.loads(get_response.http_response.text()) 

79 if rp_info["registrationState"] == "Registered": 

80 _LOGGER.warning("Registration succeeded.") 

81 return True