1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
#-------------------------------------------------------------------------
# Copyright (c) Microsoft. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#--------------------------------------------------------------------------
import os
import sys
import time
import requests
from .constants import (
AZURE_MANAGEMENT_CERTFILE,
AZURE_MANAGEMENT_SUBSCRIPTIONID,
X_MS_VERSION,
_USER_AGENT_STRING,
DEFAULT_HTTP_TIMEOUT,
MANAGEMENT_HOST,
)
from .models import (
AsynchronousOperationResult,
AzureAsyncOperationHttpError,
Operation,
)
from ._common_conversion import (
_str,
)
from ._common_error import (
_ERROR_ASYNC_OP_FAILURE,
_ERROR_ASYNC_OP_TIMEOUT,
_general_error_handler,
_validate_not_none,
)
from ._common_serialization import (
_get_request_body,
)
from ._http import (
HTTPError,
HTTPRequest,
)
from ._http.httpclient import _HTTPClient
from ._serialization import (
_MinidomXmlToObject,
)
class _ServiceManagementClient(object):
def __init__(self, subscription_id=None, cert_file=None,
host=MANAGEMENT_HOST, request_session=None,
timeout=DEFAULT_HTTP_TIMEOUT):
self.requestid = None
self.subscription_id = subscription_id
self.cert_file = cert_file
self.host = host
self.request_session = request_session
self.x_ms_version = X_MS_VERSION
self.content_type = 'application/atom+xml;type=entry;charset=utf-8'
if not self.cert_file and not request_session:
if AZURE_MANAGEMENT_CERTFILE in os.environ:
self.cert_file = os.environ[AZURE_MANAGEMENT_CERTFILE]
if not self.subscription_id:
if AZURE_MANAGEMENT_SUBSCRIPTIONID in os.environ:
self.subscription_id = os.environ[
AZURE_MANAGEMENT_SUBSCRIPTIONID]
if not self.request_session:
if not self.cert_file or not self.subscription_id:
raise ValueError(
'You need to provide subscription id and certificate file')
if not self.request_session:
if _ServiceManagementClient.should_use_requests(self.cert_file):
self.request_session = requests.Session()
self.request_session.cert = self.cert_file
self._httpclient = _HTTPClient(
service_instance=self, cert_file=self.cert_file,
request_session=self.request_session, timeout=timeout,
user_agent=_USER_AGENT_STRING)
self._filter = self._httpclient.perform_request
@staticmethod
def should_use_requests(cert_file):
if sys.platform.lower().startswith('win') and cert_file:
# On Windows, auto-detect between Windows Store Certificate
# (winhttp) and OpenSSL .pem certificate file (httplib).
#
# We used to only support certificates installed in the Windows
# Certificate Store.
# cert_file example: CURRENT_USER\my\CertificateName
#
# We now support using an OpenSSL .pem certificate file,
# for a consistent experience across all platforms.
# cert_file example: account\certificate.pem
#
# When using OpenSSL .pem certificate file on Windows, make sure
# you are on CPython 2.7.4 or later.
# If it's not an existing file on disk, then treat it as a path in
# the Windows Certificate Store, which means we can't use requests.
if not os.path.isfile(cert_file):
return False
return True
def with_filter(self, filter):
'''Returns a new service which will process requests with the
specified filter. Filtering operations can include logging, automatic
retrying, etc... The filter is a lambda which receives the HTTPRequest
and another lambda. The filter can perform any pre-processing on the
request, pass it off to the next lambda, and then perform any
post-processing on the response.'''
res = type(self)(self.subscription_id, self.cert_file, self.host,
self.request_session, self._httpclient.timeout)
old_filter = self._filter
def new_filter(request):
return filter(request, old_filter)
res._filter = new_filter
return res
def set_proxy(self, host, port, user=None, password=None):
'''
Sets the proxy server host and port for the HTTP CONNECT Tunnelling.
host:
Address of the proxy. Ex: '192.168.0.100'
port:
Port of the proxy. Ex: 6000
user:
User for proxy authorization.
password:
Password for proxy authorization.
'''
self._httpclient.set_proxy(host, port, user, password)
@property
def timeout(self):
return self._httpclient.timeout
@timeout.setter
def timeout(self, value):
self._httpclient.timeout = value
def perform_get(self, path, x_ms_version=None):
'''
Performs a GET request and returns the response.
path:
Path to the resource.
Ex: '/<subscription-id>/services/hostedservices/<service-name>'
x_ms_version:
If specified, this is used for the x-ms-version header.
Otherwise, self.x_ms_version is used.
'''
request = HTTPRequest()
request.method = 'GET'
request.host = self.host
request.path = path
request.path, request.query = self._httpclient._update_request_uri_query(request)
request.headers = self._update_management_header(request, x_ms_version)
response = self._perform_request(request)
return response
def perform_put(self, path, body, x_ms_version=None):
'''
Performs a PUT request and returns the response.
path:
Path to the resource.
Ex: '/<subscription-id>/services/hostedservices/<service-name>'
body:
Body for the PUT request.
x_ms_version:
If specified, this is used for the x-ms-version header.
Otherwise, self.x_ms_version is used.
'''
request = HTTPRequest()
request.method = 'PUT'
request.host = self.host
request.path = path
request.body = _get_request_body(body)
request.path, request.query = self._httpclient._update_request_uri_query(request)
request.headers = self._update_management_header(request, x_ms_version)
response = self._perform_request(request)
return response
def perform_post(self, path, body, x_ms_version=None):
'''
Performs a POST request and returns the response.
path:
Path to the resource.
Ex: '/<subscription-id>/services/hostedservices/<service-name>'
body:
Body for the POST request.
x_ms_version:
If specified, this is used for the x-ms-version header.
Otherwise, self.x_ms_version is used.
'''
request = HTTPRequest()
request.method = 'POST'
request.host = self.host
request.path = path
request.body = _get_request_body(body)
request.path, request.query = self._httpclient._update_request_uri_query(request)
request.headers = self._update_management_header(request, x_ms_version)
response = self._perform_request(request)
return response
def perform_delete(self, path, x_ms_version=None):
'''
Performs a DELETE request and returns the response.
path:
Path to the resource.
Ex: '/<subscription-id>/services/hostedservices/<service-name>'
x_ms_version:
If specified, this is used for the x-ms-version header.
Otherwise, self.x_ms_version is used.
'''
request = HTTPRequest()
request.method = 'DELETE'
request.host = self.host
request.path = path
request.path, request.query = self._httpclient._update_request_uri_query(request)
request.headers = self._update_management_header(request, x_ms_version)
response = self._perform_request(request)
return response
#--Operations for tracking asynchronous requests ---------------------
def wait_for_operation_status_progress_default_callback(elapsed):
sys.stdout.write('.')
sys.stdout.flush()
def wait_for_operation_status_success_default_callback(elapsed):
sys.stdout.write('\n')
sys.stdout.flush()
def wait_for_operation_status_failure_default_callback(elapsed, ex):
sys.stdout.write('\n')
sys.stdout.flush()
print(vars(ex.result))
raise ex
def wait_for_operation_status(self,
request_id, wait_for_status='Succeeded', timeout=30, sleep_interval=5,
progress_callback=wait_for_operation_status_progress_default_callback,
success_callback=wait_for_operation_status_success_default_callback,
failure_callback=wait_for_operation_status_failure_default_callback):
'''
Waits for an asynchronous operation to complete.
This calls get_operation_status in a loop and returns when the expected
status is reached. The result of get_operation_status is returned. By
default, an exception is raised on timeout or error status.
request_id:
The request ID for the request you wish to track.
wait_for_status:
Status to wait for. Default is 'Succeeded'.
timeout:
Total timeout in seconds. Default is 30s.
sleep_interval:
Sleep time in seconds for each iteration. Default is 5s.
progress_callback:
Callback for each iteration.
Default prints '.'.
Set it to None for no progress notification.
success_callback:
Callback on success. Default prints newline.
Set it to None for no success notification.
failure_callback:
Callback on failure. Default prints newline+error details then
raises exception.
Set it to None for no failure notification.
'''
loops = timeout // sleep_interval + 1
start_time = time.time()
for _ in range(int(loops)):
result = self.get_operation_status(request_id)
elapsed = time.time() - start_time
if result.status == wait_for_status:
if success_callback is not None:
success_callback(elapsed)
return result
elif result.error:
if failure_callback is not None:
ex = AzureAsyncOperationHttpError(_ERROR_ASYNC_OP_FAILURE, result.status, result)
failure_callback(elapsed, ex)
return result
else:
if progress_callback is not None:
progress_callback(elapsed)
time.sleep(sleep_interval)
if failure_callback is not None:
ex = AzureAsyncOperationHttpError(_ERROR_ASYNC_OP_TIMEOUT, result.status, result)
failure_callback(elapsed, ex)
return result
def get_operation_status(self, request_id):
'''
Returns the status of the specified operation. After calling an
asynchronous operation, you can call Get Operation Status to determine
whether the operation has succeeded, failed, or is still in progress.
request_id:
The request ID for the request you wish to track.
'''
_validate_not_none('request_id', request_id)
return self._perform_get(
'/' + self.subscription_id + '/operations/' + _str(request_id),
Operation)
#--Helper functions --------------------------------------------------
def _perform_request(self, request):
try:
resp = self._filter(request)
except HTTPError as ex:
return _management_error_handler(ex)
return resp
def _update_management_header(self, request, x_ms_version):
''' Add additional headers for management. '''
if request.method in ['PUT', 'POST', 'MERGE', 'DELETE']:
request.headers.append(('Content-Length', str(len(request.body))))
# append additional headers base on the service
request.headers.append(('x-ms-version', x_ms_version or self.x_ms_version))
# if it is not GET or HEAD request, must set content-type.
if not request.method in ['GET', 'HEAD']:
for name, _ in request.headers:
if 'content-type' == name.lower():
break
else:
request.headers.append(
('Content-Type',
self.content_type))
return request.headers
def _perform_get(self, path, response_type=None, x_ms_version=None):
response = self.perform_get(path, x_ms_version)
if response_type is not None:
return _MinidomXmlToObject.parse_response(response, response_type)
return response
def _perform_put(self, path, body, as_async=False, x_ms_version=None):
response = self.perform_put(path, body, x_ms_version)
if as_async:
return parse_response_for_async_op(response)
return None
def _perform_post(self, path, body, response_type=None, as_async=False,
x_ms_version=None):
response = self.perform_post(path, body, x_ms_version)
if response_type is not None:
return _MinidomXmlToObject.parse_response(response, response_type)
if as_async:
return parse_response_for_async_op(response)
return None
def _perform_delete(self, path, as_async=False, x_ms_version=None):
response = self.perform_delete(path, x_ms_version)
if as_async:
return parse_response_for_async_op(response)
return None
def _get_path(self, resource, name, suffix=None):
path = '/' + self.subscription_id + '/' + resource
if name is not None:
path += '/' + _str(name)
if suffix is not None:
path += '/' + suffix
return path
def _get_cloud_services_path(self, cloud_service_id, resource=None, name=None):
path = '/' + self.subscription_id + '/cloudservices/' + cloud_service_id
if resource is not None:
path += '/resources/' + _str(resource)
if name is not None:
path += '/' + _str(name)
return path
def _management_error_handler(http_error):
''' Simple error handler for management service. '''
return _general_error_handler(http_error)
def parse_response_for_async_op(response):
''' Extracts request id from response header. '''
if response is None:
return None
result = AsynchronousOperationResult()
if response.headers:
for name, value in response.headers:
if name.lower() == 'x-ms-request-id':
result.request_id = value
return result
|