Fixed an error in listing nodes created from user images. Added support for handling 307 Temp Redirects, rather than just failing
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/656d43e2 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/656d43e2 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/656d43e2 Branch: refs/heads/trunk Commit: 656d43e28d5697f255835029f8ebe3838ea0f5d4 Parents: 6048e2e Author: Michael Bennett <[email protected]> Authored: Fri Nov 21 18:23:01 2014 -0500 Committer: Michael Bennett <[email protected]> Committed: Fri Nov 21 18:23:01 2014 -0500 ---------------------------------------------------------------------- libcloud/common/azure.py | 12 ++++++++++++ libcloud/compute/drivers/azure.py | 18 +++++++++++++----- libcloud/test/compute/test_azure.py | 21 +++++++++++++++++++-- 3 files changed, 44 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/656d43e2/libcloud/common/azure.py ---------------------------------------------------------------------- diff --git a/libcloud/common/azure.py b/libcloud/common/azure.py index e76097c..727046e 100644 --- a/libcloud/common/azure.py +++ b/libcloud/common/azure.py @@ -41,6 +41,12 @@ API_VERSION = '2012-02-12' AZURE_TIME_FORMAT = '%a, %d %b %Y %H:%M:%S GMT' +class AzureRedirectException(Exception): + + def __init__(self, response): + self.location = response.headers['location'] + + class AzureResponse(XmlResponse): valid_response_codes = [ @@ -84,6 +90,12 @@ class AzureResponse(XmlResponse): driver=self ) + def parse_body(self): + if int(self.status) == httplib.TEMPORARY_REDIRECT and self.connection.driver.follow_redirects: + raise AzureRedirectException(self) + else: + return super(AzureResponse, self).parse_body() + class AzureRawResponse(RawResponse): pass http://git-wip-us.apache.org/repos/asf/libcloud/blob/656d43e2/libcloud/compute/drivers/azure.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/azure.py b/libcloud/compute/drivers/azure.py index a25ee70..25e8e54 100644 --- a/libcloud/compute/drivers/azure.py +++ b/libcloud/compute/drivers/azure.py @@ -27,7 +27,7 @@ import base64 from libcloud.utils.py3 import urlquote as url_quote from libcloud.utils.py3 import urlunquote as url_unquote -from libcloud.common.azure import AzureServiceManagementConnection +from libcloud.common.azure import AzureServiceManagementConnection, AzureRedirectException from libcloud.compute.providers import Provider from libcloud.compute.base import Node, NodeDriver, NodeLocation, NodeSize from libcloud.compute.base import NodeImage, StorageVolume @@ -210,6 +210,7 @@ class AzureNodeDriver(NodeDriver): """ self.subscription_id = subscription_id self.key_file = key_file + self.follow_redirects = kwargs.get('follow_redirects', True) super(AzureNodeDriver, self).__init__( self.subscription_id, self.key_file, @@ -293,7 +294,7 @@ class AzureNodeDriver(NodeDriver): vips = None - if data.deployments[0].virtual_ips is not None: + if len(data.deployments) > 0 and data.deployments[0].virtual_ips is not None: vips = [vip.address for vip in data.deployments[0].virtual_ips] try: @@ -1233,11 +1234,18 @@ class AzureNodeDriver(NodeDriver): try: return self.connection.request( action="https://%s%s" % (request.host, request.path), - data=request.body, headers=request.headers, + data=request.body, + headers=request.headers, method=request.method ) + except AzureRedirectException as e: + from libcloud.utils.py3 import urlparse + parsed_url = urlparse.urlparse(e.location) + request.host = parsed_url.netloc + return self._perform_request(request) except Exception, e: - print e.message + import traceback + print "Exception performing request: {}".format(traceback.format_exc()) def _update_request_uri_query(self, request): """ @@ -2404,13 +2412,13 @@ class AzureXmlSerializer(object): return xml - """ Data Classes Borrowed from the Azure SDK for Python. """ + class WindowsAzureData(object): """ http://git-wip-us.apache.org/repos/asf/libcloud/blob/656d43e2/libcloud/test/compute/test_azure.py ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/test_azure.py b/libcloud/test/compute/test_azure.py index 2f33570..0538604 100644 --- a/libcloud/test/compute/test_azure.py +++ b/libcloud/test/compute/test_azure.py @@ -1,6 +1,7 @@ import libcloud from libcloud.common.types import LibcloudError from libcloud.compute.base import NodeAuthPassword, NodeImage, NodeSize +from libcloud.compute.drivers.azure import azure_service_management_host from libcloud.common.azure import AzureServiceManagementConnection __author__ = 'david' @@ -506,8 +507,24 @@ class AzureMockHttp(MockHttp): return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _3761b98b_673d_526c_8d55_fee918758e6e_services_hostedservices_testdcabc3_deployments_dcoddkinztest02_roles(self, method, url, body, headers): - - return (httplib.TEMPORARY_REDIRECT, None, headers, httplib.responses[httplib.TEMPORARY_REDIRECT]) + redirect_host = "ussouth.management.core.windows.net" + + if not getattr(AzureMockHttp, "in_redirect", False): + setattr(AzureMockHttp, "in_redirect", True) + headers["Location"] = url.replace(azure_service_management_host, redirect_host) + return (httplib.TEMPORARY_REDIRECT, None, headers, httplib.responses[httplib.TEMPORARY_REDIRECT]) + else: + delattr(AzureMockHttp, "in_redirect") + if redirect_host not in url: + if azure_service_management_host in url: + return (httplib.TEMPORARY_REDIRECT, None, headers, httplib.responses[httplib.TEMPORARY_REDIRECT]) + else: + return (httplib.REQUEST_TIMEOUT, None, None, httplib.responses[httplib.REQUEST_TIMEOUT]) + + if method == "GET": + body = self.fixtures.load('_3761b98b_673d_526c_8d55_fee918758e6e_services_hostedservices_testdcabc2.xml') + + return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _3761b98b_673d_526c_8d55_fee918758e6e_operations_acc33f6756cda6fd96826394fce4c9f3(self, method, url, body, headers):
