You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2012/03/13 22:11:10 UTC

svn commit: r1300356 - in /libcloud/trunk: CHANGES libcloud/common/openstack.py test/compute/test_openstack.py

Author: tomaz
Date: Tue Mar 13 21:11:10 2012
New Revision: 1300356

URL: http://svn.apache.org/viewvc?rev=1300356&view=rev
Log:
Allow user to re-use auth tokens and pass 'ex_force_auth_token' keyword
argument to the OpenStack driver constructor. This patch has been contributed by
Dave King and is part of LIBCLOUD-164.

Modified:
    libcloud/trunk/CHANGES
    libcloud/trunk/libcloud/common/openstack.py
    libcloud/trunk/test/compute/test_openstack.py

Modified: libcloud/trunk/CHANGES
URL: http://svn.apache.org/viewvc/libcloud/trunk/CHANGES?rev=1300356&r1=1300355&r2=1300356&view=diff
==============================================================================
--- libcloud/trunk/CHANGES (original)
+++ libcloud/trunk/CHANGES Tue Mar 13 21:11:10 2012
@@ -53,6 +53,10 @@ Changes with Apache Libcloud in developm
     - Add new EC2 instance type - m1.medium.
       [Tomaz Muraus]
 
+    - Allow user to re-use auth tokens and pass 'ex_force_auth_token' keyword
+      argument to the OpenStack driver constructor.
+      [Dave King]
+
   *) Storage:
 
     - Don't lowercase special header names in the Amazon S3 storage driver. ;

Modified: libcloud/trunk/libcloud/common/openstack.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/libcloud/common/openstack.py?rev=1300356&r1=1300355&r2=1300356&view=diff
==============================================================================
--- libcloud/trunk/libcloud/common/openstack.py (original)
+++ libcloud/trunk/libcloud/common/openstack.py Tue Mar 13 21:11:10 2012
@@ -293,6 +293,37 @@ class OpenStackServiceCatalog(object):
 
 class OpenStackBaseConnection(ConnectionUserAndKey):
 
+    """
+    Base class for OpenStack connections.
+
+    @param user_id: User name to use when authenticating
+    @type user_id: C{string}
+
+    @param key: Secret to use when authenticating.
+    @type key: C{string}
+
+    @param secure: Use HTTPS?  (True by default.)
+    @type secure: C{bool}
+
+    @param ex_force_base_url: Base URL for connection requests.  If
+    not specified, this will be determined by authenticating.
+    @type ex_force_base_url: C{string}
+
+    @param ex_force_auth_url: Base URL for authentication requests.
+    @type ex_force_auth_url: C{string}
+
+    @param ex_force_auth_version: Authentication version to use.  If
+    not specified, defaults to AUTH_API_VERSION.
+    @type ex_force_auth_version: C{string}
+
+    @param ex_force_auth_token: Authentication token to use for
+    connection requests.  If specified, the connection will not attempt
+    to authenticate, and the value of ex_force_base_url will be used to
+    determine the base request URL.  If ex_force_auth_token is passed in,
+    ex_force_base_url must also be provided.
+    @type ex_force_auth_token: C{string}
+    """
+
     auth_url = None
     auth_token = None
     service_catalog = None
@@ -301,12 +332,21 @@ class OpenStackBaseConnection(Connection
                  host=None, port=None,
                  ex_force_base_url=None,
                  ex_force_auth_url=None,
-                 ex_force_auth_version=None):
+                 ex_force_auth_version=None,
+                 ex_force_auth_token=None):
 
         self._ex_force_base_url = ex_force_base_url
         self._ex_force_auth_url = ex_force_auth_url
         self._auth_version = ex_force_auth_version
 
+        if ex_force_auth_token:
+            self.auth_token = ex_force_auth_token
+
+        if ex_force_auth_token and not ex_force_base_url:
+            raise LibcloudError(
+                'Must also provide ex_force_base_url when specifying '
+                'ex_force_auth_token.')
+
         if not self._auth_version:
             self._auth_version = AUTH_API_VERSION
 
@@ -368,8 +408,8 @@ class OpenStackBaseConnection(Connection
             # pull out and parse the service catalog
             self.service_catalog = OpenStackServiceCatalog(osa.urls, ex_force_auth_version=self._auth_version)
 
-            # Set up connection info
-            (self.host, self.port, self.secure, self.request_path) = self._tuple_from_url(self._ex_force_base_url or self.get_endpoint())
+        # Set up connection info
+        (self.host, self.port, self.secure, self.request_path) = self._tuple_from_url(self._ex_force_base_url or self.get_endpoint())
 
     def _add_cache_busting_to_params(self, params):
         cache_busting_number = binascii.hexlify(os.urandom(8))

Modified: libcloud/trunk/test/compute/test_openstack.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/test/compute/test_openstack.py?rev=1300356&r1=1300355&r2=1300356&view=diff
==============================================================================
--- libcloud/trunk/test/compute/test_openstack.py (original)
+++ libcloud/trunk/test/compute/test_openstack.py Tue Mar 13 21:11:10 2012
@@ -551,6 +551,29 @@ class OpenStack_1_1_Tests(unittest.TestC
         self.assertEqual(self.driver.connection.port, '666')
         self.assertEqual(self.driver.connection.request_path, '/forced_url')
 
+    def test_get_endpoint_populates_host_port_and_request_path(self):
+        # simulate a subclass overriding this method
+        self.driver.connection.get_endpoint = lambda : 'http://endpoint_auth_url.com:1555/service_url'
+        self.driver.connection.auth_token = None
+        self.driver.connection._ex_force_base_url = None
+        self.driver.connection._populate_hosts_and_request_paths()
+
+        # assert that we use the result of get endpoint
+        self.assertEqual(self.driver.connection.host, 'endpoint_auth_url.com')
+        self.assertEqual(self.driver.connection.port, '1555')
+        self.assertEqual(self.driver.connection.request_path, '/service_url')
+
+    def test_set_auth_token_populates_host_port_and_request_path(self):
+        # change base url and trash the current auth token so we can re-authenticate
+        self.driver.connection._ex_force_base_url = 'http://some_other_ex_force_base_url.com:1222/some-service'
+        self.driver.connection.auth_token = "preset-auth-token"
+        self.driver.connection._populate_hosts_and_request_paths()
+
+        # assert that we use the base url and not the auth url
+        self.assertEqual(self.driver.connection.host, 'some_other_ex_force_base_url.com')
+        self.assertEqual(self.driver.connection.port, '1222')
+        self.assertEqual(self.driver.connection.request_path, '/some-service')
+
     def test_list_nodes(self):
         nodes = self.driver.list_nodes()
         self.assertEqual(len(nodes), 2)