Cleaned up and formatted the code for readability and consistency. Updated tests for the new changes. Refactored some data structures to make things more DRY. Added a new function to list all cloud services. Modified the parsing and creation of Nodes to get the public ip from the deployment data object, since Azure appears to only return it there in some cases. Also modified nodes to include the service name they belong to in the extra dict and modified reboot and destroy node to search for that, rather than requiring it to be passed it
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/76d3e2e8 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/76d3e2e8 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/76d3e2e8 Branch: refs/heads/trunk Commit: 76d3e2e822391510a2481a23f8b36f1a53226e38 Parents: c9a8587 Author: Michael Bennett <[email protected]> Authored: Fri Oct 10 18:02:50 2014 -0400 Committer: Michael Bennett <[email protected]> Committed: Wed Nov 19 13:03:16 2014 -0500 ---------------------------------------------------------------------- libcloud/common/azure.py | 81 +- libcloud/compute/drivers/azure.py | 1817 +++++++++++++++++------------- libcloud/test/compute/test_azure.py | 327 ++++-- 3 files changed, 1317 insertions(+), 908 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/76d3e2e8/libcloud/common/azure.py ---------------------------------------------------------------------- diff --git a/libcloud/common/azure.py b/libcloud/common/azure.py index 931da74..1075c95 100644 --- a/libcloud/common/azure.py +++ b/libcloud/common/azure.py @@ -44,15 +44,18 @@ AZURE_TIME_FORMAT = '%a, %d %b %Y %H:%M:%S GMT' class AzureResponse(XmlResponse): - - valid_response_codes = [httplib.NOT_FOUND, httplib.CONFLICT, - # added TEMPORARY_REDIRECT as this can sometimes be - # sent by azure instead of a success or fail response - httplib.BAD_REQUEST, httplib.TEMPORARY_REDIRECT] + valid_response_codes = [ + httplib.NOT_FOUND, + httplib.CONFLICT, + httplib.BAD_REQUEST, + httplib.TEMPORARY_REDIRECT + # added TEMPORARY_REDIRECT as this can sometimes be + # sent by azure instead of a success or fail response + ] def success(self): i = int(self.status) - return i >= 200 and i <= 299 or i in self.valid_response_codes + return 200 <= i <= 299 or i in self.valid_response_codes def parse_error(self, msg=None): error_msg = 'Unknown error' @@ -77,8 +80,10 @@ class AzureResponse(XmlResponse): if self.status in [httplib.UNAUTHORIZED, httplib.FORBIDDEN]: raise InvalidCredsError(error_msg) - raise LibcloudError('%s Status code: %d.' % (error_msg, self.status), - driver=self) + raise LibcloudError( + '%s Status code: %d.' % (error_msg, self.status), + driver=self + ) class AzureRawResponse(RawResponse): @@ -105,16 +110,26 @@ class AzureConnection(ConnectionUserAndKey): # Add the authorization header headers['Authorization'] = self._get_azure_auth_signature( - method=self.method, headers=headers, params=params, - account=self.user_id, secret_key=self.key, path=self.action) + method=self.method, + headers=headers, + params=params, + account=self.user_id, + secret_key=self.key, + path=self.action + ) # Azure cribs about this in 'raw' connections headers.pop('Host', None) return params, headers - def _get_azure_auth_signature(self, method, headers, params, - account, secret_key, path='/'): + def _get_azure_auth_signature(self, + method, + headers, + params, + account, + secret_key, + path='/'): """ Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) ) ); @@ -137,11 +152,19 @@ class AzureConnection(ConnectionUserAndKey): special_header_values = [] xms_header_values = [] param_list = [] - special_header_keys = ['content-encoding', 'content-language', - 'content-length', 'content-md5', - 'content-type', 'date', 'if-modified-since', - 'if-match', 'if-none-match', - 'if-unmodified-since', 'range'] + special_header_keys = [ + 'content-encoding', + 'content-language', + 'content-length', + 'content-md5', + 'content-type', + 'date', + 'if-modified-since', + 'if-match', + 'if-none-match', + 'if-unmodified-since', + 'range' + ] # Split the x-ms headers and normal headers and make everything # lower case @@ -192,9 +215,11 @@ class AzureConnection(ConnectionUserAndKey): return 'SharedKey %s:%s' % (self.user_id, b64_hmac.decode('utf-8')) + class AzureBaseDriver(object): name = "Microsoft Azure Service Management API" + class AzureServiceManagementConnection(CertificateConnection): # This needs the following approach - # 1. Make request using LibcloudHTTPSConnection which is a overloaded @@ -202,13 +227,18 @@ class AzureServiceManagementConnection(CertificateConnection): # 2. Depending on the type of operation use a PollingConnection # when the response id is returned # 3. The Response can be used in an AzureServiceManagementResponse - """Authentication class for "Service Account" authentication.""" + + """ + Authentication class for "Service Account" authentication. + """ + driver = AzureBaseDriver responseCls = AzureResponse rawResponseCls = AzureRawResponse name = 'Azure Service Management API Connection' host = 'management.core.windows.net' keyfile = "" + def __init__(self, subscription_id, key_file, *args, **kwargs): """ Check to see if PyCrypto is available, and convert key file path into a @@ -222,17 +252,21 @@ class AzureServiceManagementConnection(CertificateConnection): """ super(AzureServiceManagementConnection, self).__init__( - key_file, *args, **kwargs) + key_file, + *args, + **kwargs + ) self.subscription_id = subscription_id keypath = os.path.expanduser(key_file) - self.keyfile = keypath; + self.keyfile = keypath is_file_path = os.path.exists(keypath) and os.path.isfile(keypath) if not is_file_path: raise InvalidCredsError( 'You need an certificate PEM file to authenticate with ' - 'Microsoft Azure. This can be found in the portal.') + 'Microsoft Azure. This can be found in the portal.' + ) self.key_file = key_file def add_default_headers(self, headers): @@ -244,8 +278,3 @@ class AzureServiceManagementConnection(CertificateConnection): headers['x-ms-date'] = time.strftime(AZURE_TIME_FORMAT, time.gmtime()) #headers['host'] = self.host return headers - - - - -
