libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rbogorods...@apache.org
Subject svn commit: r1045270 - in /incubator/libcloud/trunk: libcloud/drivers/gogrid.py test/fixtures/gogrid/lookup_list_ip_datacenter.json test/test_gogrid.py
Date Mon, 13 Dec 2010 18:15:38 GMT
Author: rbogorodskiy
Date: Mon Dec 13 18:15:38 2010
New Revision: 1045270

URL: http://svn.apache.org/viewvc?rev=1045270&view=rev
Log:
Add locations support to GoGrid driver.

- In list_locations() obtain locations using common/lookup/list
  API call instead of hardcoded location
- Take into account 'location' kwarg for create_node

Added:
    incubator/libcloud/trunk/test/fixtures/gogrid/lookup_list_ip_datacenter.json
Modified:
    incubator/libcloud/trunk/libcloud/drivers/gogrid.py
    incubator/libcloud/trunk/test/test_gogrid.py

Modified: incubator/libcloud/trunk/libcloud/drivers/gogrid.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/drivers/gogrid.py?rev=1045270&r1=1045269&r2=1045270&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/drivers/gogrid.py (original)
+++ incubator/libcloud/trunk/libcloud/drivers/gogrid.py Mon Dec 13 18:15:38 2010
@@ -182,6 +182,17 @@ class GoGridNodeDriver(NodeDriver):
         return [ self._to_image(el)
                  for el in object['list'] ]
 
+    def _to_location(self, element):
+        location = NodeLocation(id=element['id'],
+                name=element['name'],
+                country="US",
+                driver=self.connection.driver)
+        return location
+
+    def _to_locations(self, object):
+        return [self._to_location(el)
+                for el in object['list']]
+
     def list_images(self, location=None):
         images = self._to_images(
                     self.connection.request('/api/grid/image/list').object)
@@ -237,8 +248,10 @@ class GoGridNodeDriver(NodeDriver):
         return self.connection.request("/api/grid/server/delete", params,
                                         method='POST')
 
-    def _get_first_ip(self):
+    def _get_first_ip(self, location=None):
         params = {'ip.state': 'Unassigned', 'ip.type': 'public'}
+        if location is not None:
+            params['datacenter'] = location.id
         object = self.connection.request("/api/grid/ip/list", params).object
         if object['list']:
             return object['list'][0]['ip']
@@ -251,7 +264,10 @@ class GoGridNodeDriver(NodeDriver):
                     for i in self._instance_types.values() ]
 
     def list_locations(self):
-        return [NodeLocation(0, "GoGrid Los Angeles", 'US', self)]
+        locations = self._to_locations(
+            self.connection.request('/api/common/lookup/list',
+                params={'lookup': 'ip.datacenter'}).object)
+        return locations
 
     def ex_create_node_nowait(self, **kwargs):
         """Don't block until GoGrid allocates id for a node
@@ -263,7 +279,7 @@ class GoGridNodeDriver(NodeDriver):
         name = kwargs['name']
         image = kwargs['image']
         size = kwargs['size']
-        first_ip = self._get_first_ip()
+        first_ip = self._get_first_ip(kwargs.get('location'))
         params = {'name': name,
                   'image': image.id,
                   'description': kwargs.get('ex_description', ''),
@@ -325,5 +341,5 @@ class GoGridNodeDriver(NodeDriver):
                   'friendlyName': name}
         object = self.connection.request('/api/grid/image/save', params=params,
                                          method='POST').object
-        
+
         return self._to_images(object)[0]

Added: incubator/libcloud/trunk/test/fixtures/gogrid/lookup_list_ip_datacenter.json
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/fixtures/gogrid/lookup_list_ip_datacenter.json?rev=1045270&view=auto
==============================================================================
--- incubator/libcloud/trunk/test/fixtures/gogrid/lookup_list_ip_datacenter.json (added)
+++ incubator/libcloud/trunk/test/fixtures/gogrid/lookup_list_ip_datacenter.json Mon Dec 13
18:15:38 2010
@@ -0,0 +1,24 @@
+{
+    "list": [
+        {
+            "description": "US West 1 Datacenter", 
+            "id": 1, 
+            "name": "US-West-1", 
+            "object": "option"
+        }, 
+        {
+            "description": "US East 1 Datacenter", 
+            "id": 2, 
+            "name": "US-East-1", 
+            "object": "option"
+        }
+    ], 
+    "method": "/common/lookup/list", 
+    "status": "success", 
+    "summary": {
+        "numpages": 0, 
+        "returned": 2, 
+        "start": 0, 
+        "total": 2
+    }
+}

Modified: incubator/libcloud/trunk/test/test_gogrid.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/test_gogrid.py?rev=1045270&r1=1045269&r2=1045270&view=diff
==============================================================================
--- incubator/libcloud/trunk/test/test_gogrid.py (original)
+++ incubator/libcloud/trunk/test/test_gogrid.py Mon Dec 13 18:15:38 2010
@@ -12,8 +12,10 @@
 # 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.
+import httplib
 import sys
 import unittest
+import urlparse
 
 try:
     import json
@@ -22,12 +24,11 @@ except ImportError:
 
 from libcloud.types import LibcloudError, InvalidCredsError
 from libcloud.drivers.gogrid import GoGridNodeDriver
-from libcloud.base import Node, NodeImage, NodeSize
+from libcloud.base import Node, NodeImage, NodeSize, NodeLocation
 
 from test import MockHttp, TestCaseMixin
 from test.file_fixtures import FileFixtures
 
-import httplib
 
 class GoGridTests(unittest.TestCase, TestCaseMixin):
 
@@ -102,6 +103,16 @@ class GoGridTests(unittest.TestCase, Tes
         else:
             self.fail("test should have thrown")
 
+    def test_list_locations(self):
+        locations = self.driver.list_locations()
+        location_names = [location.name for location in locations]
+
+        self.assertEqual(len(locations), 2)
+        for i in 0, 1:
+            self.assertTrue(isinstance(locations[i], NodeLocation))
+        self.assertTrue("US-West-1" in location_names)
+        self.assertTrue("US-East-1" in location_names)
+
     def test_ex_save_image(self):
         node = self.driver.list_nodes()[0]
         image = self.driver.ex_save_image(node, "testimage")
@@ -161,5 +172,18 @@ class GoGridMockHttp(MockHttp):
         body = self.fixtures.load('image_save.json')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
+    def _api_common_lookup_list(self, method, url, body, headers):
+        _valid_lookups = ("ip.datacenter",)
+
+        lookup = urlparse.parse_qs(
+                    urlparse.urlparse(url).query)["lookup"][0]
+        if lookup in _valid_lookups:
+            fixture_path = "lookup_list_%s.json" % \
+                    (lookup.replace(".", "_"))
+        else:
+            raise NotImplementedError
+        body = self.fixtures.load(fixture_path)
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
 if __name__ == '__main__':
     sys.exit(unittest.main())



Mime
View raw message