|
| 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 | +# -------------------------------------------------------------------------- |
| 26 | +import asyncio |
| 27 | + |
| 28 | +from ..azure_exceptions import CloudError |
| 29 | +from .arm_polling import ( |
| 30 | + failed, |
| 31 | + BadStatus, |
| 32 | + BadResponse, |
| 33 | + OperationFailed, |
| 34 | + ARMPolling |
| 35 | +) |
| 36 | + |
| 37 | +__all__ = ["AsyncARMPolling"] |
| 38 | + |
| 39 | +class AsyncARMPolling(ARMPolling): |
| 40 | + """A subclass or ARMPolling that redefine "run" as async. |
| 41 | + """ |
| 42 | + |
| 43 | + async def run(self): |
| 44 | + try: |
| 45 | + await self._poll() |
| 46 | + except BadStatus: |
| 47 | + self._operation.status = 'Failed' |
| 48 | + raise CloudError(self._response) |
| 49 | + |
| 50 | + except BadResponse as err: |
| 51 | + self._operation.status = 'Failed' |
| 52 | + raise CloudError(self._response, str(err)) |
| 53 | + |
| 54 | + except OperationFailed: |
| 55 | + raise CloudError(self._response) |
| 56 | + |
| 57 | + async def _poll(self): |
| 58 | + """Poll status of operation so long as operation is incomplete and |
| 59 | + we have an endpoint to query. |
| 60 | +
|
| 61 | + :param callable update_cmd: The function to call to retrieve the |
| 62 | + latest status of the long running operation. |
| 63 | + :raises: OperationFailed if operation status 'Failed' or 'Cancelled'. |
| 64 | + :raises: BadStatus if response status invalid. |
| 65 | + :raises: BadResponse if response invalid. |
| 66 | + """ |
| 67 | + |
| 68 | + while not self.finished(): |
| 69 | + await self._delay() |
| 70 | + await self.update_status() |
| 71 | + |
| 72 | + if failed(self._operation.status): |
| 73 | + raise OperationFailed("Operation failed or cancelled") |
| 74 | + |
| 75 | + elif self._operation.should_do_final_get(): |
| 76 | + if self._operation.method == 'POST' and self._operation.location_url: |
| 77 | + final_get_url = self._operation.location_url |
| 78 | + else: |
| 79 | + final_get_url = self._operation.initial_response.request.url |
| 80 | + self._response = await self.request_status(final_get_url) |
| 81 | + self._operation.get_status_from_resource(self._response) |
| 82 | + |
| 83 | + async def _delay(self): |
| 84 | + """Check for a 'retry-after' header to set timeout, |
| 85 | + otherwise use configured timeout. |
| 86 | + """ |
| 87 | + if self._response is None: |
| 88 | + await asyncio.sleep(0) |
| 89 | + if self._response.headers.get('retry-after'): |
| 90 | + await asyncio.sleep(int(self._response.headers['retry-after'])) |
| 91 | + else: |
| 92 | + await asyncio.sleep(self._timeout) |
| 93 | + |
| 94 | + async def update_status(self): |
| 95 | + """Update the current status of the LRO. |
| 96 | + """ |
| 97 | + if self._operation.async_url: |
| 98 | + self._response = await self.request_status(self._operation.async_url) |
| 99 | + self._operation.set_async_url_if_present(self._response) |
| 100 | + self._operation.get_status_from_async(self._response) |
| 101 | + elif self._operation.location_url: |
| 102 | + self._response = await self.request_status(self._operation.location_url) |
| 103 | + self._operation.set_async_url_if_present(self._response) |
| 104 | + self._operation.get_status_from_location(self._response) |
| 105 | + elif self._operation.method == "PUT": |
| 106 | + initial_url = self._operation.initial_response.request.url |
| 107 | + self._response = await self.request_status(initial_url) |
| 108 | + self._operation.set_async_url_if_present(self._response) |
| 109 | + self._operation.get_status_from_resource(self._response) |
| 110 | + else: |
| 111 | + raise BadResponse("Unable to find status link for polling.") |
| 112 | + |
| 113 | + async def request_status(self, status_link): |
| 114 | + """Do a simple GET to this status link. |
| 115 | +
|
| 116 | + This method re-inject 'x-ms-client-request-id'. |
| 117 | +
|
| 118 | + :rtype: requests.Response |
| 119 | + """ |
| 120 | + # ARM requires to re-inject 'x-ms-client-request-id' while polling |
| 121 | + header_parameters = { |
| 122 | + 'x-ms-client-request-id': self._operation.initial_response.request.headers['x-ms-client-request-id'] |
| 123 | + } |
| 124 | + request = self._client.get(status_link, headers=header_parameters) |
| 125 | + return await self._client.async_send(request, stream=False, **self._operation_config) |
0 commit comments