libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From je...@apache.org
Subject svn commit: r1054021 - in /incubator/libcloud/trunk: libcloud/drivers/ test/ test/fixtures/rackspace/
Date Thu, 30 Dec 2010 23:45:26 GMT
Author: jerry
Date: Thu Dec 30 23:45:26 2010
New Revision: 1054021

URL: http://svn.apache.org/viewvc?rev=1054021&view=rev
Log:
Implement shared IP groups in Rackspace driver
Add Content-type for PUTs in RackspaceConnection.request

Submitted By: Andrew Klochkov <aklochkov@griddynamics.com>
URL: https://github.com/apache/libcloud/pull/5

Added:
    incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_servers_ips.xml
    incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_group.xml
    incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups.xml
    incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups_detail.xml
Modified:
    incubator/libcloud/trunk/libcloud/drivers/rackspace.py
    incubator/libcloud/trunk/test/test_rackspace.py

Modified: incubator/libcloud/trunk/libcloud/drivers/rackspace.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/drivers/rackspace.py?rev=1054021&r1=1054020&r2=1054021&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/drivers/rackspace.py (original)
+++ incubator/libcloud/trunk/libcloud/drivers/rackspace.py Thu Dec 30 23:45:26 2010
@@ -147,7 +147,7 @@ class RackspaceConnection(ConnectionUser
         # Due to first-run authentication request, we may not have a path
         if self.path:
             action = self.path + action
-        if method == "POST":
+        if method in ("POST", "PUT"):
             headers = {'Content-Type': 'application/xml; charset=UTF-8'}
         if method == "GET":
             params['cache-busting'] = os.urandom(8).encode('hex')
@@ -158,6 +158,27 @@ class RackspaceConnection(ConnectionUser
         )
 
 
+class SharedIpGroup(object):
+    """
+    Shared IP group info.
+    """
+
+    def __init__(self, id, name, servers=None):
+        self.id = str(id)
+        self.name = name
+        self.servers = servers
+
+
+class NodeIpAddresses(object):
+    """
+    List of public and private IP addresses of a Node.
+    """
+
+    def __init__(self, public_addresses, private_addresses):
+        self.public_addresses = public_addresses
+        self.private_addresses = private_addresses
+
+
 class RackspaceNodeDriver(NodeDriver):
     """
     Rackspace node driver.
@@ -234,11 +255,78 @@ class RackspaceNodeDriver(NodeDriver):
         if files_elm:
             server_elm.append(files_elm)
 
+        shared_ip_elm = self._shared_ip_group_to_xml(kwargs.get("ex_shared_ip_group", None))
+        if shared_ip_elm:
+            server_elm.append(shared_ip_elm)
+
         resp = self.connection.request("/servers",
                                        method='POST',
                                        data=ET.tostring(server_elm))
         return self._to_node(resp.object)
 
+    def ex_create_ip_group(self, group_name, node_id=None):
+        group_elm = ET.Element(
+            'sharedIpGroup',
+            {'xmlns': NAMESPACE,
+             'name': group_name,
+            }
+        )
+        if node_id:
+            ET.SubElement(group_elm,
+                'server',
+                {'id': node_id}
+            )
+
+        resp = self.connection.request('/shared_ip_groups',
+                                       method='POST',
+                                       data=ET.tostring(group_elm))
+        return self._to_shared_ip_group(resp.object)
+
+    def ex_list_ip_groups(self, details=False):
+        uri = '/shared_ip_groups/detail' if details else '/shared_ip_groups'
+        resp = self.connection.request(uri,
+                                       method='GET')
+        groups = self._findall(resp.object, 'sharedIpGroup')
+        return [self._to_shared_ip_group(el) for el in groups]
+
+    def ex_delete_ip_group(self, group_id):
+        uri = '/shared_ip_groups/%s' % group_id
+        resp = self.connection.request(uri, method='DELETE')
+        return resp.status == 204
+
+    def ex_share_ip(self, group_id, node_id, ip, configure_node=True):
+        if configure_node:
+            str_configure = 'true'
+        else:
+            str_configure = 'false'
+
+        elm = ET.Element(
+            'shareIp',
+            {'xmlns': NAMESPACE,
+             'sharedIpGroupId' : group_id,
+             'configureServer' : str_configure}
+        )
+
+        uri = '/servers/%s/ips/public/%s' % (node_id, ip)
+
+        resp = self.connection.request(uri,
+                                       method='PUT',
+                                       data=ET.tostring(elm))
+        return resp.status == 202
+
+    def ex_unshare_ip(self, node_id, ip):
+        uri = '/servers/%s/ips/public/%s' % (node_id, ip)
+
+        resp = self.connection.request(uri,
+                                       method='DELETE')
+        return resp.status == 202
+
+    def ex_list_ip_addresses(self, node_id):
+        uri = '/servers/%s/ips' % node_id
+        resp = self.connection.request(uri,
+                                       method='GET')
+        return self._to_ip_addresses(resp.object)
+
     def _metadata_to_xml(self, metadata):
         if len(metadata) == 0:
             return None
@@ -399,6 +487,28 @@ class RackspaceNodeDriver(NodeDriver):
                     method="POST",
                     data=ET.tostring(image_elm)).object)
 
+    def _to_shared_ip_group(self, el):
+        servers_el = self._findall(el, 'servers')
+        if servers_el:
+            servers = [s.get('id') for s in self._findall(servers_el[0], 'server')]
+        else:
+            servers = None
+        return SharedIpGroup(id=el.get('id'),
+                     name=el.get('name'),
+                     servers=servers)
+
+    def _to_ip_addresses(self, el):
+        return NodeIpAddresses(
+            [ip.get('addr') for ip in self._findall(self._findall(el, 'public')[0], 'ip')],
+            [ip.get('addr') for ip in self._findall(self._findall(el, 'private')[0], 'ip')]
+        )
+
+    def _shared_ip_group_to_xml(self, shared_ip_group):
+        if not shared_ip_group:
+            return None
+
+        return ET.Element('sharedIpGroupId', shared_ip_group)
+
 class RackspaceUKConnection(RackspaceConnection):
     """
     Connection class for the Rackspace UK driver

Added: incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_servers_ips.xml
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_servers_ips.xml?rev=1054021&view=auto
==============================================================================
--- incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_servers_ips.xml (added)
+++ incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_servers_ips.xml Thu Dec 30 23:45:26
2010
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<addresses xmlns="http://docs.rackspacecloud.com/servers/api/v1.0">
+    <public>
+        <ip addr="67.23.10.132"/>
+        <ip addr="67.23.10.131"/>
+    </public>
+    <private>
+        <ip addr="10.176.42.16"/>
+    </private>
+</addresses>

Added: incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_group.xml
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_group.xml?rev=1054021&view=auto
==============================================================================
--- incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_group.xml (added)
+++ incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_group.xml Thu Dec 30
23:45:26 2010
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<sharedIpGroup xmlns="http://docs.rackspacecloud.com/servers/api/v1.0" id="1234" name="Shared
IP Group 1">
+    <servers>
+        <server id="422"/>
+    </servers>
+</sharedIpGroup>

Added: incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups.xml
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups.xml?rev=1054021&view=auto
==============================================================================
--- incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups.xml (added)
+++ incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups.xml Thu Dec
30 23:45:26 2010
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<sharedIpGroups xmlns="http://docs.rackspacecloud.com/servers/api/v1.0">
+    <sharedIpGroup id="1234" name="Shared IP Group 1"/>
+    <sharedIpGroup id="5678" name="Shared IP Group 2"/>
+</sharedIpGroups>

Added: incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups_detail.xml
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups_detail.xml?rev=1054021&view=auto
==============================================================================
--- incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups_detail.xml (added)
+++ incubator/libcloud/trunk/test/fixtures/rackspace/v1_slug_shared_ip_groups_detail.xml Thu
Dec 30 23:45:26 2010
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<sharedIpGroups xmlns="http://docs.rackspacecloud.com/servers/api/v1.0">
+    <sharedIpGroup id="1234" name="Shared IP Group 1">
+        <servers>
+            <server id="422" />
+            <server id="3445" />
+        </servers>
+    </sharedIpGroup>
+    <sharedIpGroup id="5678" name="Shared IP Group 2">
+        <servers>
+            <server id="23203"/>
+            <server id="2456" />
+            <server id="9891" />
+        </servers>
+    </sharedIpGroup>
+</sharedIpGroups>

Modified: incubator/libcloud/trunk/test/test_rackspace.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/test_rackspace.py?rev=1054021&r1=1054020&r2=1054021&view=diff
==============================================================================
--- incubator/libcloud/trunk/test/test_rackspace.py (original)
+++ incubator/libcloud/trunk/test/test_rackspace.py Thu Dec 30 23:45:26 2010
@@ -16,7 +16,10 @@ import sys
 import unittest
 
 from libcloud.types import InvalidCredsError
-from libcloud.drivers.rackspace import RackspaceNodeDriver as Rackspace
+from libcloud.drivers.rackspace import (
+    RackspaceNodeDriver as Rackspace,
+    SharedIpGroup,
+    NodeIpAddresses)
 from libcloud.base import Node, NodeImage, NodeSize
 
 from test import MockHttp, TestCaseMixin
@@ -76,7 +79,7 @@ class RackspaceTests(unittest.TestCase, 
     def test_create_node(self):
         image = NodeImage(id=11, name='Ubuntu 8.10 (intrepid)', driver=self.driver)
         size = NodeSize(1, '256 slice', None, None, None, None, driver=self.driver)
-        node = self.driver.create_node(name='racktest', image=image, size=size)
+        node = self.driver.create_node(name='racktest', image=image, size=size, shared_ip_group='group1')
         self.assertEqual(node.name, 'racktest')
         self.assertEqual(node.extra.get('password'), 'racktestvJq7d3')
 
@@ -115,6 +118,61 @@ class RackspaceTests(unittest.TestCase, 
         self.assertEqual(image.name, "imgtest")
         self.assertEqual(image.id, "12345")
 
+    def test_ex_list_ip_addresses(self):
+        ret = self.driver.ex_list_ip_addresses(node_id=72258)
+        self.assertEquals(2, len(ret.public_addresses))
+        self.assertTrue('67.23.10.131' in ret.public_addresses)
+        self.assertTrue('67.23.10.132' in ret.public_addresses)
+        self.assertEquals(1, len(ret.private_addresses))
+        self.assertTrue('10.176.42.16' in ret.private_addresses)
+
+    def test_ex_list_ip_groups(self):
+        ret = self.driver.ex_list_ip_groups()
+        self.assertEquals(2, len(ret))
+        self.assertEquals('1234', ret[0].id)
+        self.assertEquals('Shared IP Group 1', ret[0].name)
+        self.assertEquals('5678', ret[1].id)
+        self.assertEquals('Shared IP Group 2', ret[1].name)
+        self.assertTrue(ret[0].servers is None)
+
+    def test_ex_list_ip_groups_detail(self):
+        ret = self.driver.ex_list_ip_groups(details=True)
+
+        self.assertEquals(2, len(ret))
+
+        self.assertEquals('1234', ret[0].id)
+        self.assertEquals('Shared IP Group 1', ret[0].name)
+        self.assertEquals(2, len(ret[0].servers))
+        self.assertEquals('422', ret[0].servers[0])
+        self.assertEquals('3445', ret[0].servers[1])
+
+        self.assertEquals('5678', ret[1].id)
+        self.assertEquals('Shared IP Group 2', ret[1].name)
+        self.assertEquals(3, len(ret[1].servers))
+        self.assertEquals('23203', ret[1].servers[0])
+        self.assertEquals('2456', ret[1].servers[1])
+        self.assertEquals('9891', ret[1].servers[2])
+
+    def test_ex_create_ip_group(self):
+        ret = self.driver.ex_create_ip_group('Shared IP Group 1', '5467')
+        self.assertEquals('1234', ret.id)
+        self.assertEquals('Shared IP Group 1', ret.name)
+        self.assertEquals(1, len(ret.servers))
+        self.assertEquals('422', ret.servers[0])
+
+    def test_ex_delete_ip_group(self):
+        ret = self.driver.ex_delete_ip_group('5467')
+        self.assertEquals(True, ret)
+
+    def test_ex_share_ip(self):
+        ret = self.driver.ex_share_ip('1234', '3445', '67.23.21.133')
+        self.assertEquals(True, ret)
+
+    def test_ex_unshare_ip(self):
+        ret = self.driver.ex_unshare_ip('3445', '67.23.21.133')
+        self.assertEquals(True, ret)
+
+
 class RackspaceMockHttp(MockHttp):
 
     fixtures = FileFixtures('rackspace')
@@ -183,6 +241,29 @@ class RackspaceMockHttp(MockHttp):
         # only used by destroy node()
         return (httplib.ACCEPTED, "", {}, httplib.responses[httplib.ACCEPTED])
 
+    def _v1_0_slug_servers_72258_ips(self, method, url, body, headers):
+        body = self.fixtures.load('v1_slug_servers_ips.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _v1_0_slug_shared_ip_groups_5467(self, method, url, body, headers):
+        if method != 'DELETE':
+            raise NotImplemented
+        return (httplib.NO_CONTENT, "", {}, httplib.responses[httplib.NO_CONTENT])
+
+    def _v1_0_slug_shared_ip_groups(self, method, url, body, headers):
+
+        fixture = 'v1_slug_shared_ip_group.xml' if method == 'POST' else 'v1_slug_shared_ip_groups.xml'
+        body = self.fixtures.load(fixture)
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _v1_0_slug_shared_ip_groups_detail(self, method, url, body, headers):
+        body = self.fixtures.load('v1_slug_shared_ip_groups_detail.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _v1_0_slug_servers_3445_ips_public_67_23_21_133(self, method, url, body, headers):
+        return (httplib.ACCEPTED, "", {}, httplib.responses[httplib.ACCEPTED])
+
+
 
 if __name__ == '__main__':
     sys.exit(unittest.main())



Mime
View raw message