You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by pq...@apache.org on 2011/03/14 02:25:26 UTC
svn commit: r1081244 - in /incubator/libcloud/trunk/libcloud:
common/rackspace.py compute/drivers/rackspace.py
storage/drivers/cloudfiles.py
Author: pquerna
Date: Mon Mar 14 01:25:26 2011
New Revision: 1081244
URL: http://svn.apache.org/viewvc?rev=1081244&view=rev
Log:
Start refactoring out common rackspace classes from both cloudfiles and cloud servers
Added:
incubator/libcloud/trunk/libcloud/common/rackspace.py (with props)
Modified:
incubator/libcloud/trunk/libcloud/compute/drivers/rackspace.py
incubator/libcloud/trunk/libcloud/storage/drivers/cloudfiles.py
Added: incubator/libcloud/trunk/libcloud/common/rackspace.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/common/rackspace.py?rev=1081244&view=auto
==============================================================================
--- incubator/libcloud/trunk/libcloud/common/rackspace.py (added)
+++ incubator/libcloud/trunk/libcloud/common/rackspace.py Mon Mar 14 01:25:26 2011
@@ -0,0 +1,86 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+"""
+Common utilities for Rackspace Cloud Servers and Cloud Files
+"""
+import httplib
+from urllib2 import urlparse
+from libcloud.common.base import ConnectionUserAndKey
+from libcloud.compute.types import InvalidCredsError
+
+AUTH_HOST_US='auth.api.rackspacecloud.com'
+AUTH_HOST_UK='lon.auth.api.rackspacecloud.com'
+
+class RackspaceBaseConnection(ConnectionUserAndKey):
+ def __init__(self, user_id, key, secure):
+ self.cdn_management_url = None
+ self.storage_url = None
+ self.auth_token = None
+ self.request_path = None
+ self.__host = None
+ super(RackspaceBaseConnection, self).__init__(user_id, key, secure=secure)
+
+ def add_default_headers(self, headers):
+ headers['X-Auth-Token'] = self.auth_token
+ headers['Accept'] = self.accept_format
+ return headers
+
+ @property
+ def host(self):
+ """
+ Rackspace uses a separate host for API calls which is only provided
+ after an initial authentication request. If we haven't made that
+ request yet, do it here. Otherwise, just return the management host.
+ """
+ if not self.__host:
+ # Initial connection used for authentication
+ conn = self.conn_classes[self.secure](self.auth_host, self.port[self.secure])
+ conn.request(
+ method='GET',
+ url='/%s' % (self.api_version),
+ headers={
+ 'X-Auth-User': self.user_id,
+ 'X-Auth-Key': self.key
+ }
+ )
+
+ resp = conn.getresponse()
+
+ if resp.status != httplib.NO_CONTENT:
+ raise InvalidCredsError()
+
+ headers = dict(resp.getheaders())
+
+ try:
+ self.server_url = headers['x-server-management-url']
+ self.storage_url = headers['x-storage-url']
+ self.cdn_management_url = headers['x-cdn-management-url']
+ self.auth_token = headers['x-auth-token']
+ except KeyError:
+ raise InvalidCredsError()
+
+ scheme, server, self.request_path, param, query, fragment = (
+ urlparse.urlparse(getattr(self, self._url_key))
+ )
+
+ if scheme is "https" and self.secure is not True:
+ raise InvalidCredsError()
+
+ # Set host to where we want to make further requests to;
+ self.__host = server
+ conn.close()
+
+ return self.__host
Propchange: incubator/libcloud/trunk/libcloud/common/rackspace.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/libcloud/trunk/libcloud/common/rackspace.py
------------------------------------------------------------------------------
svn:keywords = Date Revision Author HeadURL Id
Propchange: incubator/libcloud/trunk/libcloud/common/rackspace.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/libcloud/trunk/libcloud/compute/drivers/rackspace.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/compute/drivers/rackspace.py?rev=1081244&r1=1081243&r2=1081244&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/compute/drivers/rackspace.py (original)
+++ incubator/libcloud/trunk/libcloud/compute/drivers/rackspace.py Mon Mar 14 01:25:26 2011
@@ -23,16 +23,15 @@ import urlparse
from xml.etree import ElementTree as ET
from xml.parsers.expat import ExpatError
-from libcloud.common.base import ConnectionUserAndKey, Response
+from libcloud.common.base import Response
from libcloud.common.types import InvalidCredsError, MalformedResponseError
from libcloud.compute.types import NodeState, Provider
from libcloud.compute.base import NodeDriver, Node
from libcloud.compute.base import NodeSize, NodeImage, NodeLocation
-RACKSPACE_US_AUTH_HOST='auth.api.rackspacecloud.com'
-RACKSPACE_UK_AUTH_HOST='lon.auth.api.rackspacecloud.com'
+from libcloud.common.rackspace import AUTH_HOST_US, AUTH_HOST_UK, RackspaceBaseConnection
-NAMESPACE = 'http://docs.rackspacecloud.com/servers/api/v1.0'
+NAMESPACE='http://docs.rackspacecloud.com/servers/api/v1.0'
#
# Prices need to be hardcoded as Rackspace doesn't expose them through
@@ -79,67 +78,19 @@ class RackspaceResponse(Response):
return '%s %s %s' % (self.status, self.error, text)
-class RackspaceConnection(ConnectionUserAndKey):
+class RackspaceConnection(RackspaceBaseConnection):
"""
Connection class for the Rackspace driver
"""
- api_version = 'v1.0'
- auth_host = RACKSPACE_US_AUTH_HOST
responseCls = RackspaceResponse
+ auth_host = AUTH_HOST_US
+ _url_key = "server_url"
def __init__(self, user_id, key, secure=True):
- self.__host = None
- self.path = None
- self.token = None
super(RackspaceConnection, self).__init__(user_id, key, secure)
-
- def add_default_headers(self, headers):
- headers['X-Auth-Token'] = self.token;
- headers['Accept'] = 'application/xml'
- return headers
-
- @property
- def host(self):
- """
- Rackspace uses a separate host for API calls which is only provided
- after an initial authentication request. If we haven't made that
- request yet, do it here. Otherwise, just return the management host.
-
- TODO: Fixup for when our token expires (!!!)
- """
- if not self.__host:
- # Initial connection used for authentication
- conn = self.conn_classes[self.secure](self.auth_host, self.port[self.secure])
- conn.request(
- method='GET',
- url='/%s' % self.api_version,
- headers={
- 'X-Auth-User': self.user_id,
- 'X-Auth-Key': self.key
- }
- )
- resp = conn.getresponse()
- headers = dict(resp.getheaders())
- try:
- self.token = headers['x-auth-token']
- endpoint = headers['x-server-management-url']
- except KeyError:
- raise InvalidCredsError()
-
- scheme, server, self.path, param, query, fragment = (
- urlparse.urlparse(endpoint)
- )
- if scheme is "https" and self.secure is not 1:
- # TODO: Custom exception (?)
- raise InvalidCredsError()
-
- # Set host to where we want to make further requests to;
- # close auth conn
- self.__host = server
- conn.close()
-
- return self.__host
+ self.api_version = 'v1.0'
+ self.accept_format = 'application/xml'
def request(self, action, params=None, data='', headers=None, method='GET'):
if not headers:
@@ -147,8 +98,8 @@ class RackspaceConnection(ConnectionUser
if not params:
params = {}
# Due to first-run authentication request, we may not have a path
- if self.path:
- action = self.path + action
+ if self.server_url:
+ action = self.server_url + action
if method in ("POST", "PUT"):
headers = {'Content-Type': 'application/xml; charset=UTF-8'}
if method == "GET":
@@ -482,7 +433,7 @@ class RackspaceNodeDriver(NodeDriver):
'hostId': el.get('hostId'),
'imageId': el.get('imageId'),
'flavorId': el.get('flavorId'),
- 'uri': "https://%s%s/servers/%s" % (self.connection.host, self.connection.path, el.get('id')),
+ 'uri': "https://%s%s/servers/%s" % (self.connection.host, self.connection.request_path, el.get('id')),
'metadata': metadata,
})
return n
@@ -588,7 +539,7 @@ class RackspaceUKConnection(RackspaceCon
"""
Connection class for the Rackspace UK driver
"""
- auth_host = RACKSPACE_UK_AUTH_HOST
+ auth_host = AUTH_HOST_UK
class RackspaceUKNodeDriver(RackspaceNodeDriver):
"""Driver for Rackspace in the UK (London)
Modified: incubator/libcloud/trunk/libcloud/storage/drivers/cloudfiles.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/storage/drivers/cloudfiles.py?rev=1081244&r1=1081243&r2=1081244&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/storage/drivers/cloudfiles.py (original)
+++ incubator/libcloud/trunk/libcloud/storage/drivers/cloudfiles.py Mon Mar 14 01:25:26 2011
@@ -37,8 +37,8 @@ from libcloud.storage.types import Objec
from libcloud.storage.types import ObjectHashMismatchError
from libcloud.storage.types import InvalidContainerNameError
-AUTH_HOST_US = 'auth.api.rackspacecloud.com'
-AUTH_HOST_UK = 'lon.auth.api.rackspacecloud.com'
+from libcloud.common.rackspace import AUTH_HOST_US, AUTH_HOST_UK, RackspaceBaseConnection
+
API_VERSION = 'v1.0'
class CloudFilesResponse(Response):
@@ -79,74 +79,19 @@ class CloudFilesResponse(Response):
return data
-class CloudFilesConnection(ConnectionUserAndKey):
+class CloudFilesConnection(RackspaceBaseConnection):
"""
Base connection class for the Cloudfiles driver.
"""
- auth_host = None
- api_version = API_VERSION
responseCls = CloudFilesResponse
+ auth_host = None
+ _url_key = "storage_url"
def __init__(self, user_id, key, secure=True):
- self.cdn_management_url = None
- self.storage_url = None
- self.auth_token = None
- self.request_path = None
-
- self.__host = None
- super(CloudFilesConnection, self).__init__(user_id, key, secure)
-
- def add_default_headers(self, headers):
- headers['X-Auth-Token'] = self.auth_token
- headers['Accept'] = 'application/json'
- return headers
-
- @property
- def host(self):
- """
- Rackspace uses a separate host for API calls which is only provided
- after an initial authentication request. If we haven't made that
- request yet, do it here. Otherwise, just return the management host.
- """
- if not self.__host:
- # Initial connection used for authentication
- conn = self.conn_classes[self.secure](self.auth_host, self.port[self.secure])
- conn.request(
- method='GET',
- url='/%s' % (self.api_version),
- headers={
- 'X-Auth-User': self.user_id,
- 'X-Auth-Key': self.key
- }
- )
-
- resp = conn.getresponse()
-
- if resp.status != httplib.NO_CONTENT:
- raise InvalidCredsError()
-
- headers = dict(resp.getheaders())
-
- try:
- self.storage_url = headers['x-storage-url']
- self.cdn_management_url = headers['x-cdn-management-url']
- self.auth_token = headers['x-auth-token']
- except KeyError:
- raise InvalidCredsError()
-
- scheme, server, self.request_path, param, query, fragment = (
- urlparse.urlparse(self.storage_url)
- )
-
- if scheme is "https" and self.secure is not True:
- raise InvalidCredsError()
-
- # Set host to where we want to make further requests to;
- self.__host = server
- conn.close()
-
- return self.__host
+ super(CloudFilesConnection, self).__init__(user_id, key, secure=secure)
+ self.api_version = API_VERSION
+ self.accept_format = 'application/json'
def request(self, action, params=None, data='', headers=None, method='GET',
raw=False):