| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
try: |
| 10 |
from ansible.module_utils.ansible_release import __version__ as ANSIBLE_VERSION |
| 11 |
except Exception: |
| 12 |
ANSIBLE_VERSION = 'unknown' |
| 13 |
|
| 14 |
try: |
| 15 |
from azure.core._pipeline_client import PipelineClient |
| 16 |
from azure.core.polling import LROPoller |
| 17 |
from azure.core.pipeline import PipelineResponse |
| 18 |
from azure.core.pipeline.policies import BearerTokenCredentialPolicy, UserAgentPolicy |
| 19 |
from azure.mgmt.core.polling.arm_polling import ARMPolling |
| 20 |
import uuid |
| 21 |
from azure.core.configuration import Configuration |
| 22 |
except ImportError: |
| 23 |
|
| 24 |
Configuration = object |
| 25 |
|
| 26 |
ANSIBLE_USER_AGENT = 'Ansible/{0}'.format(ANSIBLE_VERSION) |
| 27 |
|
| 28 |
|
| 29 |
class GenericRestClientConfiguration(Configuration): |
| 30 |
|
| 31 |
def __init__(self, credential, subscription_id, credential_scopes=None, base_url=None): |
| 32 |
|
| 33 |
if credential is None: |
| 34 |
raise ValueError("Parameter 'credentials' must not be None.") |
| 35 |
if subscription_id is None: |
| 36 |
raise ValueError("Parameter 'subscription_id' must not be None.") |
| 37 |
if not base_url: |
| 38 |
base_url = 'https://management.azure.com' |
| 39 |
if not credential_scopes: |
| 40 |
credential_scopes = 'https://management.azure.com/.default' |
| 41 |
|
| 42 |
super(GenericRestClientConfiguration, self).__init__() |
| 43 |
|
| 44 |
self.user_agent_policy = UserAgentPolicy(ANSIBLE_USER_AGENT) |
| 45 |
self.credentials = credential |
| 46 |
self.subscription_id = subscription_id |
| 47 |
self.authentication_policy = BearerTokenCredentialPolicy(credential, credential_scopes) |
| 48 |
|
| 49 |
|
| 50 |
class GenericRestClient(object): |
| 51 |
|
| 52 |
def __init__(self, credential, subscription_id, base_url=None, credential_scopes=None): |
| 53 |
self._config = GenericRestClientConfiguration(credential, subscription_id, credential_scopes[0]) |
| 54 |
self._client = PipelineClient(base_url, config=self._config) |
| 55 |
self.models = None |
| 56 |
|
| 57 |
|
| 58 |
self.error_map = {} |
| 59 |
|
| 60 |
def query(self, url, method, query_parameters, header_parameters, body, expected_status_codes, polling_timeout, polling_interval): |
| 61 |
|
| 62 |
operation_config = {} |
| 63 |
|
| 64 |
request = None |
| 65 |
|
| 66 |
if header_parameters is None: |
| 67 |
header_parameters = {} |
| 68 |
|
| 69 |
header_parameters['x-ms-client-request-id'] = str(uuid.uuid1()) |
| 70 |
|
| 71 |
if method == 'GET': |
| 72 |
request = self._client.get(url, query_parameters, header_parameters, body) |
| 73 |
elif method == 'PUT': |
| 74 |
request = self._client.put(url, query_parameters, header_parameters, body) |
| 75 |
elif method == 'POST': |
| 76 |
request = self._client.post(url, query_parameters, header_parameters, body) |
| 77 |
elif method == 'HEAD': |
| 78 |
request = self._client.head(url, query_parameters, header_parameters, body) |
| 79 |
elif method == 'PATCH': |
| 80 |
request = self._client.patch(url, query_parameters, header_parameters, body) |
| 81 |
elif method == 'DELETE': |
| 82 |
request = self._client.delete(url, query_parameters, header_parameters, body) |
| 83 |
elif method == 'MERGE': |
| 84 |
request = self._client.merge(url, query_parameters, header_parameters, body) |
| 85 |
|
| 86 |
response = self._client.send_request(request, **operation_config) |
| 87 |
|
| 88 |
if response.status_code not in expected_status_codes: |
| 89 |
return self.on_error(response) |
| 90 |
|
| 91 |
elif response.status_code == 202 and polling_timeout > 0: |
| 92 |
def get_long_running_output(response): |
| 93 |
return response |
| 94 |
poller = LROPoller(self._client, |
| 95 |
PipelineResponse(None, response, None), |
| 96 |
get_long_running_output, |
| 97 |
ARMPolling(polling_interval, **operation_config)) |
| 98 |
response = self.get_poller_result(poller, polling_timeout) |
| 99 |
|
| 100 |
return response |
| 101 |
|
| 102 |
def get_poller_result(self, poller, timeout): |
| 103 |
try: |
| 104 |
poller.wait(timeout=timeout) |
| 105 |
return poller.result() |
| 106 |
except Exception as exc: |
| 107 |
raise |
| 108 |
|
| 109 |
def on_error(self, response): |
| 110 |
""" handle errors in response |
| 111 |
""" |
| 112 |
|
| 113 |
error_type = self.error_map.get(response.status_code) |
| 114 |
if error_type: |
| 115 |
raise error_type(response=response) |
| 116 |
raise SendRequestException(response.text(), response.status_code) |
| 117 |
|
| 118 |
|
| 119 |
class SendRequestException(Exception): |
| 120 |
def __init__(self, response, status_code): |
| 121 |
self.response = response |
| 122 |
self.status_code = status_code |