From 2318c80ab123be14fdb9ed11542d1cbec81a6d8d Mon Sep 17 00:00:00 2001 From: Aymeric Barantal Date: Wed, 21 Sep 2011 14:35:30 +0200 Subject: [PATCH 6/7] add snapshot_disk and update_disk ex methods. Change name and param order of all attach and detach methods --- libcloud/compute/drivers/gandi.py | 56 ++++++++++++++++++--- test/compute/fixtures/gandi/disk_create_from.xml | 53 ++++++++++++++++++++ test/compute/fixtures/gandi/disk_update.xml | 53 ++++++++++++++++++++ test/compute/test_gandi.py | 27 +++++++++-- 4 files changed, 177 insertions(+), 12 deletions(-) create mode 100644 test/compute/fixtures/gandi/disk_create_from.xml create mode 100644 test/compute/fixtures/gandi/disk_update.xml diff --git a/libcloud/compute/drivers/gandi.py b/libcloud/compute/drivers/gandi.py index a9b1ab7..5f8dc81 100644 --- a/libcloud/compute/drivers/gandi.py +++ b/libcloud/compute/drivers/gandi.py @@ -15,6 +15,8 @@ """ Gandi driver for compute """ +from datetime import datetime + from libcloud.common.gandi import BaseGandiDriver, GandiException, \ NetworkInterface, IPAddress, Disk from libcloud.compute.types import NodeState @@ -323,30 +325,68 @@ class GandiNodeDriver(BaseGandiDriver, NodeDriver): disks = [] return self._to_disks(res) - def ex_attach_disk(self, disk, node): + def ex_node_attach_disk(self, node, disk): """Specific method to attach a disk to a node""" - op = self.connection.request('vm.disk_attach', int(node.id), int(disk.id)) + op = self.connection.request('vm.disk_attach', + int(node.id), int(disk.id)) if self._wait_operation(op['id']): return True return False - def ex_detach_disk(self, disk, node): + def ex_node_detach_disk(self, node, disk): """Specific method to detach a disk from a node""" - op = self.connection.request('vm.disk_detach', int(node.id), int(disk.id)) + op = self.connection.request('vm.disk_detach', + int(node.id), int(disk.id)) if self._wait_operation(op['id']): return True return False - def ex_attach_interface(self, iface, node): + def ex_node_attach_interface(self, node, iface): """Specific method to attach an interface to a node""" - op = self.connection.request('vm.iface_attach', int(node.id), int(iface.id)) + op = self.connection.request('vm.iface_attach', + int(node.id), int(iface.id)) if self._wait_operation(op['id']): return True return False - def ex_detach_interface(self, iface, node): + def ex_node_detach_interface(self, node, iface): """Specific method to detach an interface from a node""" - op = self.connection.request('vm.iface_detach', int(node.id), int(iface.id)) + op = self.connection.request('vm.iface_detach', + int(node.id), int(iface.id)) + if self._wait_operation(op['id']): + return True + return False + + def ex_snapshot_disk(self, disk, name=None): + """Specific method to make a snapshot of a disk""" + if not disk.extra.get('can_snapshot'): + raise GandiException(1021, "Disk %s can't snapshot" % disk.id) + if not name: + suffix = datetime.today().strftime("%Y%m%d") + name = "%s_%s" % (disk.name, suffix) + op = self.connection.request('disk.create_from', + { + 'name': name, + 'type': 'snapshot', + }, + int(disk.id), + ) + if self._wait_operation(op['id']): + return True + return False + + def ex_update_disk(self, disk, new_size=None, new_name=None): + """Specific method to update size or name of a disk + WARNING: if a server is attached it'll be rebooted + """ + params = {} + if new_size: + params.update({'size': new_size}) + if new_name: + params.update({'name': new_name}) + op = self.connection.request('disk.update', + int(disk.id), + params) if self._wait_operation(op['id']): return True return False diff --git a/test/compute/fixtures/gandi/disk_create_from.xml b/test/compute/fixtures/gandi/disk_create_from.xml new file mode 100644 index 0000000..4d34d0e --- /dev/null +++ b/test/compute/fixtures/gandi/disk_create_from.xml @@ -0,0 +1,53 @@ + + + + + + +iface_id + + + +date_updated +20110921T14:20:56 + + +vm_id + + + +date_start + + + +disk_id +35288 + + +source +AB3917-GANDI + + +step +WAIT + + +ip_id + + + +date_created +20110921T14:20:56 + + +type +disk_create + + +id +657985 + + + + + \ No newline at end of file diff --git a/test/compute/fixtures/gandi/disk_update.xml b/test/compute/fixtures/gandi/disk_update.xml new file mode 100644 index 0000000..9dcd73e --- /dev/null +++ b/test/compute/fixtures/gandi/disk_update.xml @@ -0,0 +1,53 @@ + + + + + + +iface_id + + + +date_updated +20110921T14:23:10 + + +vm_id + + + +date_start + + + +disk_id +34951 + + +source +AB3917-GANDI + + +step +WAIT + + +ip_id + + + +date_created +20110921T14:23:10 + + +type +disk_update + + +id +657987 + + + + + \ No newline at end of file diff --git a/test/compute/test_gandi.py b/test/compute/test_gandi.py index 6c53d65..84d273a 100644 --- a/test/compute/test_gandi.py +++ b/test/compute/test_gandi.py @@ -21,6 +21,7 @@ import httplib import xmlrpclib from libcloud.compute.drivers.gandi import GandiNodeDriver as Gandi +from libcloud.common.gandi import GandiException from libcloud.compute.types import NodeState from xml.etree import ElementTree as ET @@ -116,27 +117,37 @@ class GandiTests(unittest.TestCase): def test_ex_attach_interface(self): ifaces = self.driver.ex_list_interfaces() nodes = self.driver.list_nodes() - res = self.driver.ex_attach_interface(ifaces[0], nodes[0]) + res = self.driver.ex_node_attach_interface(nodes[0], ifaces[0]) self.assertTrue(res) def test_ex_detach_interface(self): ifaces = self.driver.ex_list_interfaces() nodes = self.driver.list_nodes() - res = self.driver.ex_detach_interface(ifaces[0], nodes[0]) + res = self.driver.ex_node_detach_interface(nodes[0], ifaces[0]) self.assertTrue(res) def test_ex_attach_disk(self): disks = self.driver.ex_list_disks() nodes = self.driver.list_nodes() - res = self.driver.ex_attach_disk(disks[0], nodes[0]) + res = self.driver.ex_node_attach_disk(nodes[0], disks[0]) self.assertTrue(res) def test_ex_detach_disk(self): disks = self.driver.ex_list_disks() nodes = self.driver.list_nodes() - res = self.driver.ex_detach_disk(disks[0], nodes[0]) + res = self.driver.ex_node_detach_disk(nodes[0], disks[0]) self.assertTrue(res) + def test_ex_snapshot_disk(self): + disks = self.driver.ex_list_disks() + self.assertTrue(self.driver.ex_snapshot_disk(disks[2])) + self.assertRaises(GandiException, + self.driver.ex_snapshot_disk, disks[0]) + + def test_ex_update_disk(self): + disks = self.driver.ex_list_disks() + self.assertTrue(self.driver.ex_update_disk(disks[0], new_size=4096)) + class GandiMockHttp(MockHttp): @@ -210,5 +221,13 @@ class GandiMockHttp(MockHttp): body = self.fixtures.load('disk_detach.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + def _xmlrpc_2_0__disk_create_from(self, method, url, body, headers): + body = self.fixtures.load('disk_create_from.xml') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + + def _xmlrpc_2_0__disk_update(self, method, url, body, headers): + body = self.fixtures.load('disk_update.xml') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + if __name__ == '__main__': sys.exit(unittest.main()) -- 1.7.6.1