libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rbogorods...@apache.org
Subject svn commit: r1067448 - in /incubator/libcloud/trunk: CHANGES libcloud/drivers/gogrid.py test/fixtures/gogrid/ip_list.json test/test_gogrid.py
Date Sat, 05 Feb 2011 14:32:32 GMT
Author: rbogorodskiy
Date: Sat Feb  5 14:32:31 2011
New Revision: 1067448

URL: http://svn.apache.org/viewvc?rev=1067448&view=rev
Log:
Implement ex_list_ips() for GoGrid driver to list
IP addresses assigned to the account.

Make use of it in _get_first_ip().

Modified:
    incubator/libcloud/trunk/CHANGES
    incubator/libcloud/trunk/libcloud/drivers/gogrid.py
    incubator/libcloud/trunk/test/fixtures/gogrid/ip_list.json
    incubator/libcloud/trunk/test/test_gogrid.py

Modified: incubator/libcloud/trunk/CHANGES
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/CHANGES?rev=1067448&r1=1067447&r2=1067448&view=diff
==============================================================================
--- incubator/libcloud/trunk/CHANGES (original)
+++ incubator/libcloud/trunk/CHANGES Sat Feb  5 14:32:31 2011
@@ -2,6 +2,11 @@
 
 Changes with Apache Libcloud 0.4.3
 
+    *) Implement ex_list_ips() for GoGrid driver to list
+       IP addresses assigned to the account. Make use of
+       it in _get_first_ip().
+       [Roman Bogorodskiy]
+
     *) Implement ex_edit_image method for GoGrid driver
        which allows changing image attributes like name,
        description and make image public or private.

Modified: incubator/libcloud/trunk/libcloud/drivers/gogrid.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/drivers/gogrid.py?rev=1067448&r1=1067447&r2=1067448&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/drivers/gogrid.py (original)
+++ incubator/libcloud/trunk/libcloud/drivers/gogrid.py Sat Feb  5 14:32:31 2011
@@ -121,6 +121,18 @@ class GoGridConnection(ConnectionUserAnd
         m = hashlib.md5(key+secret+str(int(time.time())))
         return m.hexdigest()
 
+class GoGridIpAddress(object):
+    """
+    IP Address
+    """
+
+    def __init__(self, id, ip, public, state, subnet):
+        self.id = id
+        self.ip = ip
+        self.public = public
+        self.state = state
+        self.subnet = subnet
+
 class GoGridNode(Node):
     # Generating uuid based on public ip to get around missing id on
     # create_node in gogrid api
@@ -191,6 +203,18 @@ class GoGridNodeDriver(NodeDriver):
                 driver=self.connection.driver)
         return location
 
+    def _to_ip(self, element):
+        ip = GoGridIpAddress(id=element['id'],
+                ip=element['ip'],
+                public=element['public'],
+                subnet=element['subnet'],
+                state=element["state"]["name"])
+        return ip
+
+    def _to_ips(self, object):
+        return [ self._to_ip(el)
+                for el in object['list'] ]
+
     def _to_locations(self, object):
         return [self._to_location(el)
                 for el in object['list']]
@@ -254,13 +278,10 @@ class GoGridNodeDriver(NodeDriver):
                                         method='POST')
 
     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']
-        else:
+        ips = self.ex_list_ips(public=True, assigned=False)
+        try:
+            return ips[0].ip 
+        except IndexError:
             raise LibcloudError('No public unassigned IPs left',
                     GoGridNodeDriver)
 
@@ -403,3 +424,34 @@ class GoGridNodeDriver(NodeDriver):
                 params=params).object
 
         return self._to_image(object['list'][0])
+
+    def ex_list_ips(self, **kwargs):
+        """Return list of IP addresses assigned to
+        the account.
+
+        @keyword    public: set to True to list only
+                    public IPs or False to list only
+                    private IPs. Set to None or not specify
+                    at all not to filter by type
+        @type       public: C{bool}
+        @keyword    assigned: set to True to list only addresses
+                    assigned to servers, False to list unassigned
+                    addresses and set to None or don't set at all
+                    not no filter by state
+        @type       assigned: C{bool}
+        @return:    C{list} of L{GoGridIpAddress}es
+        """
+
+        params = {}
+
+        if "public" in kwargs and kwargs["public"] is not None:
+            params["ip.type"] = {True: "Public",
+                    False: "Private"}[kwargs["public"]]
+        if "assigned" in kwargs and kwargs["assigned"] is not None:
+            params["ip.state"] = {True: "Assigned",
+                    False: "Unassigned"}[kwargs["assigned"]]
+
+        ips = self._to_ips(
+                self.connection.request('/api/grid/ip/list',
+                    params=params).object)
+        return ips

Modified: incubator/libcloud/trunk/test/fixtures/gogrid/ip_list.json
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/fixtures/gogrid/ip_list.json?rev=1067448&r1=1067447&r2=1067448&view=diff
==============================================================================
--- incubator/libcloud/trunk/test/fixtures/gogrid/ip_list.json (original)
+++ incubator/libcloud/trunk/test/fixtures/gogrid/ip_list.json Sat Feb  5 14:32:31 2011
@@ -1,220 +1,69 @@
 {
     "list": [
         {
-            "id": 1659917,
-            "ip": "192.168.0.192",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is reserved or in use",
-                "id": 2,
-                "name": "Assigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659918,
-            "ip": "192.168.0.193",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is reserved or in use",
-                "id": 2,
-                "name": "Assigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659919,
-            "ip": "192.168.0.194",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659920,
-            "ip": "192.168.0.195",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659921,
-            "ip": "192.168.0.196",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659922,
-            "ip": "192.168.0.197",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659923,
-            "ip": "192.168.0.198",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is reserved or in use",
-                "id": 2,
-                "name": "Assigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659924,
-            "ip": "192.168.0.199",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is reserved or in use",
-                "id": 2,
-                "name": "Assigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659925,
-            "ip": "192.168.0.200",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is reserved or in use",
-                "id": 2,
-                "name": "Assigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659926,
-            "ip": "192.168.0.201",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659927,
-            "ip": "192.168.0.202",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659928,
-            "ip": "192.168.0.203",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659929,
-            "ip": "192.168.0.204",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659930,
-            "ip": "192.168.0.205",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659931,
-            "ip": "192.168.0.206",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is available to use",
-                "id": 1,
-                "name": "Unassigned",
-                "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
-        },
-        {
-            "id": 1659932,
-            "ip": "192.168.0.207",
-            "object": "ip",
-            "public": true,
-            "state": {
-                "description": "IP is reserved or in use",
-                "id": 2,
-                "name": "Assigned",
+            "datacenter": {
+                "description": "US West 1 Datacenter", 
+                "id": 1, 
+                "name": "US-West-1", 
+                "object": "option"
+            }, 
+            "id": 5348099, 
+            "ip": "192.168.75.66", 
+            "object": "ip", 
+            "public": true, 
+            "state": {
+                "description": "IP is available to use", 
+                "id": 1, 
+                "name": "Unassigned", 
+                "object": "option"
+            }, 
+            "subnet": "192.168.75.64/255.255.255.240"
+        }, 
+        {
+            "datacenter": {
+                "description": "US West 1 Datacenter", 
+                "id": 1, 
+                "name": "US-West-1", 
+                "object": "option"
+            }, 
+            "id": 5348100, 
+            "ip": "192.168.75.67", 
+            "object": "ip", 
+            "public": true, 
+            "state": {
+                "description": "IP is reserved or in use", 
+                "id": 2, 
+                "name": "Assigned", 
+                "object": "option"
+            }, 
+            "subnet": "192.168.75.64/255.255.255.240"
+        }, 
+        {
+            "datacenter": {
+                "description": "US West 1 Datacenter", 
+                "id": 1, 
+                "name": "US-West-1", 
+                "object": "option"
+            }, 
+            "id": 5348101, 
+            "ip": "192.168.75.68", 
+            "object": "ip", 
+            "public": false, 
+            "state": {
+                "description": "IP is available to use", 
+                "id": 1, 
+                "name": "Unassigned", 
                 "object": "option"
-            },
-            "subnet": "192.168.0.192/255.255.255.240"
+            }, 
+            "subnet": "192.168.75.64/255.255.255.240"
         }
-    ],
-    "method": "/grid/ip/list",
-    "status": "success",
+    ], 
+    "method": "/grid/ip/list", 
+    "status": "success", 
     "summary": {
-        "numpages": 0,
-        "returned": 16,
-        "start": 0,
-        "total": 16
+        "numpages": 0, 
+        "returned": 3, 
+        "start": 0, 
+        "total": 3
     }
 }

Modified: incubator/libcloud/trunk/test/test_gogrid.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/test_gogrid.py?rev=1067448&r1=1067447&r2=1067448&view=diff
==============================================================================
--- incubator/libcloud/trunk/test/test_gogrid.py (original)
+++ incubator/libcloud/trunk/test/test_gogrid.py Sat Feb  5 14:32:31 2011
@@ -23,7 +23,7 @@ except ImportError:
     import simplejson as json
 
 from libcloud.types import LibcloudError, InvalidCredsError
-from libcloud.drivers.gogrid import GoGridNodeDriver
+from libcloud.drivers.gogrid import GoGridNodeDriver, GoGridIpAddress
 from libcloud.base import Node, NodeImage, NodeSize, NodeLocation
 
 from test import MockHttp, TestCaseMixin
@@ -132,6 +132,31 @@ class GoGridTests(unittest.TestCase, Tes
 
         self.assertTrue(isinstance(ret, Node))
 
+    def test_ex_list_ips(self):
+        ips = self.driver.ex_list_ips()
+
+        expected_ips = {"192.168.75.66": GoGridIpAddress(id="5348099",
+            ip="192.168.75.66", public=True, state="Unassigned",
+            subnet="192.168.75.64/255.255.255.240"),
+            "192.168.75.67": GoGridIpAddress(id="5348100",
+                ip="192.168.75.67", public=True, state="Assigned",
+                subnet="192.168.75.64/255.255.255.240"),
+            "192.168.75.68": GoGridIpAddress(id="5348101",
+                ip="192.168.75.68", public=False, state="Unassigned",
+                subnet="192.168.75.64/255.255.255.240")}
+
+        self.assertEqual(len(expected_ips), 3)
+
+        for ip in ips:
+            self.assertTrue(ip.ip in expected_ips)
+            self.assertEqual(ip.public, expected_ips[ip.ip].public)
+            self.assertEqual(ip.state, expected_ips[ip.ip].state)
+            self.assertEqual(ip.subnet, expected_ips[ip.ip].subnet)
+
+            del expected_ips[ip.ip]
+
+        self.assertEqual(len(expected_ips), 0)
+
 class GoGridMockHttp(MockHttp):
 
     fixtures = FileFixtures('gogrid')



Mime
View raw message