Updated Branches:
  refs/heads/trunk 573100712 -> 7cb7423f3

Move Joyent compute driver to a single class + region argument model.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/66386bce
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/66386bce
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/66386bce

Branch: refs/heads/trunk
Commit: 66386bce123176294f2eb7d7e4d216af4a6076df
Parents: 5731007
Author: Tomaz Muraus <[email protected]>
Authored: Thu Oct 17 18:01:21 2013 +0200
Committer: Tomaz Muraus <[email protected]>
Committed: Thu Oct 17 19:58:32 2013 +0200

----------------------------------------------------------------------
 CHANGES                              |  7 ++++++
 libcloud/compute/drivers/joyent.py   | 37 ++++++++++++++--------------
 libcloud/test/compute/test_joyent.py | 41 ++++++++++++++++++++++---------
 3 files changed, 55 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/66386bce/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 3895fee..ac5ea27 100644
--- a/CHANGES
+++ b/CHANGES
@@ -140,6 +140,13 @@ Changes with Apache Libcloud in development
       (LIBCLOUD-409)
       [Oleg Suharev]
 
+    - Align Joyent driver with other drivers and deprecate "location" argument
+      in the driver constructor in favor of "region" argument.
+
+      Note: Deprecated argument will continue to work until the next major
+      release.
+      [Tomaz Muraus]
+
   *) Storage
 
     - Deprecate CLOUDFILES_US and CLOUDFILES_UK provider constant and replace

http://git-wip-us.apache.org/repos/asf/libcloud/blob/66386bce/libcloud/compute/drivers/joyent.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/joyent.py 
b/libcloud/compute/drivers/joyent.py
index f972c94..f3c0132 100644
--- a/libcloud/compute/drivers/joyent.py
+++ b/libcloud/compute/drivers/joyent.py
@@ -46,8 +46,8 @@ NODE_STATE_MAP = {
     'deleted': NodeState.TERMINATED
 }
 
-LOCATIONS = ['us-east-1', 'us-west-1', 'us-sw-1', 'eu-ams-1']
-DEFAULT_LOCATION = LOCATIONS[0]
+VALID_REGIONS = ['us-east-1', 'us-west-1', 'us-sw-1', 'eu-ams-1']
+DEFAULT_REGION = 'us-east-1'
 
 
 class JoyentResponse(JsonResponse):
@@ -59,7 +59,7 @@ class JoyentResponse(JsonResponse):
                             httplib.NO_CONTENT]
 
     def parse_error(self):
-        if self.status == 401:
+        if self.status == httplib.UNAUTHORIZED:
             data = self.parse_body()
             raise InvalidCredsError(data['code'] + ': ' + data['message'])
         return self.body
@@ -96,23 +96,22 @@ class JoyentNodeDriver(NodeDriver):
     connectionCls = JoyentConnection
     features = {'create_node': ['generates_password']}
 
-    def __init__(self, *args, **kwargs):
-        """
-        @inherits: :class:`NodeDriver.__init__`
-
-        :keyword    location: Location which should be used
-        :type       location: ``str``
-        """
+    def __init__(self, key, secret=None, secure=True, host=None, port=None,
+                 region=DEFAULT_REGION, **kwargs):
+        # Location is here for backward compatibility reasons
         if 'location' in kwargs:
-            if kwargs['location'] not in LOCATIONS:
-                msg = 'Invalid location: "%s". Valid locations: %s'
-                raise LibcloudError(msg % (kwargs['location'],
-                                    ', '.join(LOCATIONS)), driver=self)
-        else:
-            kwargs['location'] = DEFAULT_LOCATION
-
-        super(JoyentNodeDriver, self).__init__(*args, **kwargs)
-        self.connection.host = kwargs['location'] + API_HOST_SUFFIX
+            region = kwargs['location']
+
+        if region not in VALID_REGIONS:
+            msg = 'Invalid region: "%s". Valid region: %s'
+            raise LibcloudError(msg % (region,
+                                ', '.join(VALID_REGIONS)), driver=self)
+
+        super(JoyentNodeDriver, self).__init__(key=key, secret=secret,
+                                               secure=secure, host=host,
+                                               port=port, region=region,
+                                               **kwargs)
+        self.connection.host = region + API_HOST_SUFFIX
 
     def list_images(self):
         result = self.connection.request('/my/datasets').object

http://git-wip-us.apache.org/repos/asf/libcloud/blob/66386bce/libcloud/test/compute/test_joyent.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_joyent.py 
b/libcloud/test/compute/test_joyent.py
index 4826e47..78cce9b 100644
--- a/libcloud/test/compute/test_joyent.py
+++ b/libcloud/test/compute/test_joyent.py
@@ -14,15 +14,13 @@
 # limitations under the License.
 
 import sys
-import unittest
 
 from libcloud.utils.py3 import httplib
 from libcloud.common.types import LibcloudError
-from libcloud.compute.base import Node, NodeState
+from libcloud.compute.base import NodeState
 from libcloud.compute.drivers.joyent import JoyentNodeDriver
 
-from libcloud.test import MockHttp
-from libcloud.test.compute import TestCaseMixin
+from libcloud.test import MockHttp, unittest
 from libcloud.test.file_fixtures import ComputeFileFixtures
 from libcloud.test.secrets import JOYENT_PARAMS
 
@@ -32,13 +30,34 @@ class JoyentTestCase(unittest.TestCase):
         JoyentNodeDriver.connectionCls.conn_classes = (None, JoyentHttp)
         self.driver = JoyentNodeDriver(*JOYENT_PARAMS)
 
-    def test_instantiate_invalid_location(self):
-        try:
-            JoyentNodeDriver('user', 'key', location='invalid')
-        except LibcloudError:
-            pass
-        else:
-            self.fail('Exception was not thrown')
+    def test_instantiate_multiple_drivers_with_different_region(self):
+        kwargs1 = {'region': 'us-east-1'}
+        kwargs2 = {'region': 'us-west-1'}
+        driver1 = JoyentNodeDriver(*JOYENT_PARAMS, **kwargs1)
+        driver2 = JoyentNodeDriver(*JOYENT_PARAMS, **kwargs2)
+
+        self.assertTrue(driver1.connection.host.startswith(kwargs1['region']))
+        self.assertTrue(driver2.connection.host.startswith(kwargs2['region']))
+
+        driver1.list_nodes()
+        driver2.list_nodes()
+        driver1.list_nodes()
+
+        self.assertTrue(driver1.connection.host.startswith(kwargs1['region']))
+        self.assertTrue(driver2.connection.host.startswith(kwargs2['region']))
+
+    def test_location_backward_compatibility(self):
+        kwargs = {'location': 'us-west-1'}
+        driver = JoyentNodeDriver(*JOYENT_PARAMS, **kwargs)
+        self.assertTrue(driver.connection.host.startswith(kwargs['location']))
+
+    def test_instantiate_invalid_region(self):
+        expected_msg = 'Invalid region.+'
+
+        self.assertRaisesRegexp(LibcloudError, expected_msg, JoyentNodeDriver,
+                                'user', 'key', location='invalid')
+        self.assertRaisesRegexp(LibcloudError, expected_msg, JoyentNodeDriver,
+                                'user', 'key', region='invalid')
 
     def test_list_sizes(self):
         sizes = self.driver.list_sizes()

Reply via email to