Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudstack driver improvements LIBCLOUD-616 #363

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 111 additions & 1 deletion libcloud/compute/drivers/cloudstack.py
Expand Up @@ -24,7 +24,7 @@
from libcloud.compute.providers import Provider
from libcloud.common.cloudstack import CloudStackDriverMixIn
from libcloud.compute.base import Node, NodeDriver, NodeImage, NodeLocation
from libcloud.compute.base import NodeSize, StorageVolume
from libcloud.compute.base import NodeSize, StorageVolume, VolumeSnapshot
from libcloud.compute.base import KeyPair
from libcloud.compute.types import NodeState, LibcloudError
from libcloud.compute.types import KeyPairDoesNotExistError
Expand Down Expand Up @@ -193,6 +193,10 @@
'transform_func': int
},
'instance_id': {
'key_name': 'virtualmachineid',
'transform_func': str
},
'serviceoffering_id': {
'key_name': 'serviceofferingid',
'transform_func': str
},
Expand Down Expand Up @@ -2253,6 +2257,112 @@ def ex_delete_tags(self, resource_ids, resource_type, tag_keys):

return True

def list_snapshots(self):
"""
Describe all snapshots.

:rtype: ``list`` of :class:`VolumeSnapshot`
"""
snapshots = self._sync_request('listSnapshots',
method='GET')
list_snapshots = []

for snap in snapshots['snapshot']:
list_snapshots.append(self._to_snapshot(snap))
return list_snapshots

def create_volume_snapshot(self, volume):
"""
Create snapshot from volume

:param volume: Instance of ``StorageVolume``
:type volume: ``StorageVolume``

:rtype: :class:`VolumeSnapshot`
"""
snapshot = self._async_request(command='createSnapshot',
params={'volumeid': volume.id},
method='GET')
return self._to_snapshot(snapshot['snapshot'])

def destroy_volume_snapshot(self, snapshot):
"""
Destroy snapshot

:param snapshot: Instance of ``VolumeSnapshot``
:type volume: ``VolumeSnapshot``

:rtype: ``bool``
"""
self._async_request(command='deleteSnapshot',
params={'id': snapshot.id},
method='GET')
return True

def ex_create_snapshot_template(self, snapshot, name, ostypeid,
displaytext=None):
"""
Create a template from a snapshot

:param snapshot: Instance of ``VolumeSnapshot``
:type volume: ``VolumeSnapshot``

:param name: the name of the template
:type name: ``str``

:param name: the os type id
:type name: ``str``

:param name: the display name of the template
:type name: ``str``

:rtype: :class:`NodeImage`
"""
if not displaytext:
displaytext = name
resp = self._async_request(command='createTemplate',
params={
'displaytext': displaytext,
'name': name,
'ostypeid': ostypeid,
'snapshotid': snapshot.id})
img = resp.get('template')
extra = {
'hypervisor': img['hypervisor'],
'format': img['format'],
'os': img['ostypename'],
'displaytext': img['displaytext']
}
return NodeImage(id=img['id'],
name=img['name'],
driver=self.connection.driver,
extra=extra)

def ex_list_os_types(self):
"""
List all registered os types (needed for snapshot creation)

:rtype: ``list``
"""
ostypes = self._sync_request('listOsTypes')
return ostypes['ostype']

def _to_snapshot(self, data):
"""
Create snapshot object from data

:param data: Node data object.
:type data: ``dict``

:rtype: :class:`VolumeSnapshot`
"""
extra = {
'tags': data.get('tags', None),
'name': data.get('name', None),
'volume_id': data.get('volumeid', None),
}
return VolumeSnapshot(data['id'], driver=self, extra=extra)

def _to_node(self, data, public_ips=None):
"""
:param data: Node data object.
Expand Down
@@ -0,0 +1 @@
{ "createsnapshotresponse" : {"jobid":1300001,"id":190547} }
@@ -0,0 +1 @@
{ "createtemplateresponse" : {"jobid":1300003,"id":10260} }
@@ -0,0 +1 @@
{ "deletesnapshotresponse" : {"jobid":1300002} }
@@ -0,0 +1 @@
{ "listostypesresponse" : { "count":146 ,"ostype" : [ {"id":69,"oscategoryid":7,"description":"Asianux 3(32-bit)"}, {"id":70,"oscategoryid":7,"description":"Asianux 3(64-bit)"}, {"id":1,"oscategoryid":1,"description":"CentOS 4.5 (32-bit)"}, {"id":2,"oscategoryid":1,"description":"CentOS 4.6 (32-bit)"}, {"id":3,"oscategoryid":1,"description":"CentOS 4.7 (32-bit)"}, {"id":4,"oscategoryid":1,"description":"CentOS 4.8 (32-bit)"}, {"id":5,"oscategoryid":1,"description":"CentOS 5.0 (32-bit)"}, {"id":6,"oscategoryid":1,"description":"CentOS 5.0 (64-bit)"}, {"id":7,"oscategoryid":1,"description":"CentOS 5.1 (32-bit)"}, {"id":8,"oscategoryid":1,"description":"CentOS 5.1 (64-bit)"}, {"id":9,"oscategoryid":1,"description":"CentOS 5.2 (32-bit)"}, {"id":10,"oscategoryid":1,"description":"CentOS 5.2 (64-bit)"}, {"id":11,"oscategoryid":1,"description":"CentOS 5.3 (32-bit)"}, {"id":12,"oscategoryid":1,"description":"CentOS 5.3 (64-bit)"}, {"id":13,"oscategoryid":1,"description":"CentOS 5.4 (32-bit)"}, {"id":14,"oscategoryid":1,"description":"CentOS 5.4 (64-bit)"}, {"id":111,"oscategoryid":1,"description":"CentOS 5.5 (32-bit)"}, {"id":112,"oscategoryid":1,"description":"CentOS 5.5 (64-bit)"}, {"id":73,"oscategoryid":2,"description":"Debian GNU/Linux 4(32-bit)"}, {"id":74,"oscategoryid":2,"description":"Debian GNU/Linux 4(64-bit)"}, {"id":72,"oscategoryid":2,"description":"Debian GNU/Linux 5(64-bit)"}, {"id":15,"oscategoryid":2,"description":"Debian GNU/Linux 5.0 (32-bit)"}, {"id":132,"oscategoryid":2,"description":"Debian GNU/Linux 6(32-bit)"}, {"id":133,"oscategoryid":2,"description":"Debian GNU/Linux 6(64-bit)"}, {"id":102,"oscategoryid":6,"description":"DOS"}, {"id":118,"oscategoryid":4,"description":"Fedora 10"}, {"id":117,"oscategoryid":4,"description":"Fedora 11"}, {"id":116,"oscategoryid":4,"description":"Fedora 12"}, {"id":115,"oscategoryid":4,"description":"Fedora 13"}, {"id":120,"oscategoryid":4,"description":"Fedora 8"}, {"id":119,"oscategoryid":4,"description":"Fedora 9"}, {"id":83,"oscategoryid":9,"description":"FreeBSD (32-bit)"}, {"id":84,"oscategoryid":9,"description":"FreeBSD (64-bit)"}, {"id":92,"oscategoryid":6,"description":"Microsoft Small Bussiness Server 2003"}, {"id":138,"oscategoryid":7,"description":"None"}, {"id":78,"oscategoryid":8,"description":"Novell Netware 5.1"}, {"id":77,"oscategoryid":8,"description":"Novell Netware 6.x"}, {"id":68,"oscategoryid":7,"description":"Open Enterprise Server"}, {"id":16,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.0 (32-bit)"}, {"id":17,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.0 (64-bit)"}, {"id":18,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.1 (32-bit)"}, {"id":19,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.1 (64-bit)"}, {"id":20,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.2 (32-bit)"}, {"id":21,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.2 (64-bit)"}, {"id":22,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.3 (32-bit)"}, {"id":23,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.3 (64-bit)"}, {"id":24,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.4 (32-bit)"}, {"id":25,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.4 (64-bit)"}, {"id":134,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.5 (32-bit)"}, {"id":135,"oscategoryid":3,"description":"Oracle Enterprise Linux 5.5 (64-bit)"}, {"id":104,"oscategoryid":7,"description":"OS/2"}, {"id":60,"oscategoryid":7,"description":"Other (32-bit)"}, {"id":103,"oscategoryid":7,"description":"Other (64-bit)"}, {"id":75,"oscategoryid":7,"description":"Other 2.6x Linux (32-bit)"}, {"id":76,"oscategoryid":7,"description":"Other 2.6x Linux (64-bit)"}, {"id":200,"oscategoryid":1,"description":"Other CentOS (32-bit)"}, {"id":201,"oscategoryid":1,"description":"Other CentOS (64-bit)"}, {"id":98,"oscategoryid":7,"description":"Other Linux (32-bit)"}, {"id":99,"oscategoryid":7,"description":"Other Linux (64-bit)"}, {"id":139,"oscategoryid":7,"description":"Other PV (32-bit)"}, {"id":140,"oscategoryid":7,"description":"Other PV (64-bit)"}, {"id":202,"oscategoryid":5,"description":"Other SUSE Linux(32-bit)"}, {"id":203,"oscategoryid":5,"description":"Other SUSE Linux(64-bit)"}, {"id":59,"oscategoryid":10,"description":"Other Ubuntu (32-bit)"}, {"id":100,"oscategoryid":10,"description":"Other Ubuntu (64-bit)"}, {"id":131,"oscategoryid":10,"description":"Red Hat Enterprise Linux 2"}, {"id":66,"oscategoryid":4,"description":"Red Hat Enterprise Linux 3(32-bit)"}, {"id":67,"oscategoryid":4,"description":"Red Hat Enterprise Linux 3(64-bit)"}, {"id":106,"oscategoryid":4,"description":"Red Hat Enterprise Linux 4(64-bit)"}, {"id":26,"oscategoryid":4,"description":"Red Hat Enterprise Linux 4.5 (32-bit)"}, {"id":27,"oscategoryid":4,"description":"Red Hat Enterprise Linux 4.6 (32-bit)"}, {"id":28,"oscategoryid":4,"description":"Red Hat Enterprise Linux 4.7 (32-bit)"}, {"id":29,"oscategoryid":4,"description":"Red Hat Enterprise Linux 4.8 (32-bit)"}, {"id":30,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.0 (32-bit)"}, {"id":31,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.0 (64-bit)"}, {"id":32,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.1 (32-bit)"}, {"id":33,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.1 (64-bit)"}, {"id":34,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.2 (32-bit)"}, {"id":35,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.2 (64-bit)"}, {"id":36,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.3 (32-bit)"}, {"id":37,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.3 (64-bit)"}, {"id":38,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.4 (32-bit)"}, {"id":39,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.4 (64-bit)"}, {"id":113,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.5 (32-bit)"}, {"id":114,"oscategoryid":4,"description":"Red Hat Enterprise Linux 5.5 (64-bit)"}, {"id":136,"oscategoryid":4,"description":"Red Hat Enterprise Linux 6.0 (32-bit)"}, {"id":137,"oscategoryid":4,"description":"Red Hat Enterprise Linux 6.0 (64-bit)"}, {"id":85,"oscategoryid":9,"description":"SCO OpenServer 5"}, {"id":86,"oscategoryid":9,"description":"SCO UnixWare 7"}, {"id":79,"oscategoryid":9,"description":"Sun Solaris 10(32-bit)"}, {"id":80,"oscategoryid":9,"description":"Sun Solaris 10(64-bit)"}, {"id":142,"oscategoryid":9,"description":"Sun Solaris 11 (32-bit)"}, {"id":141,"oscategoryid":9,"description":"Sun Solaris 11 (64-bit)"}, {"id":82,"oscategoryid":9,"description":"Sun Solaris 8(Experimental)"}, {"id":81,"oscategoryid":9,"description":"Sun Solaris 9(Experimental)"}, {"id":109,"oscategoryid":5,"description":"SUSE Linux Enterprise 10(32-bit)"}, {"id":110,"oscategoryid":5,"description":"SUSE Linux Enterprise 10(64-bit)"}, {"id":96,"oscategoryid":5,"description":"SUSE Linux Enterprise 8(32-bit)"}, {"id":97,"oscategoryid":5,"description":"SUSE Linux Enterprise 8(64-bit)"}, {"id":107,"oscategoryid":5,"description":"SUSE Linux Enterprise 9(32-bit)"}, {"id":108,"oscategoryid":5,"description":"SUSE Linux Enterprise 9(64-bit)"}, {"id":41,"oscategoryid":5,"description":"SUSE Linux Enterprise Server 10 SP1 (32-bit)"}, {"id":42,"oscategoryid":5,"description":"SUSE Linux Enterprise Server 10 SP1 (64-bit)"}, {"id":43,"oscategoryid":5,"description":"SUSE Linux Enterprise Server 10 SP2 (32-bit)"}, {"id":44,"oscategoryid":5,"description":"SUSE Linux Enterprise Server 10 SP2 (64-bit)"}, {"id":45,"oscategoryid":5,"description":"SUSE Linux Enterprise Server 10 SP3 (64-bit)"}, {"id":46,"oscategoryid":5,"description":"SUSE Linux Enterprise Server 11 (32-bit)"}, {"id":47,"oscategoryid":5,"description":"SUSE Linux Enterprise Server 11 (64-bit)"}, {"id":40,"oscategoryid":5,"description":"SUSE Linux Enterprise Server 9 SP4 (32-bit)"}, {"id":121,"oscategoryid":10,"description":"Ubuntu 10.04 (32-bit)"}, {"id":126,"oscategoryid":10,"description":"Ubuntu 10.04 (64-bit)"}, {"id":125,"oscategoryid":10,"description":"Ubuntu 8.04 (32-bit)"}, {"id":130,"oscategoryid":10,"description":"Ubuntu 8.04 (64-bit)"}, {"id":124,"oscategoryid":10,"description":"Ubuntu 8.10 (32-bit)"}, {"id":129,"oscategoryid":10,"description":"Ubuntu 8.10 (64-bit)"}, {"id":123,"oscategoryid":10,"description":"Ubuntu 9.04 (32-bit)"}, {"id":128,"oscategoryid":10,"description":"Ubuntu 9.04 (64-bit)"}, {"id":122,"oscategoryid":10,"description":"Ubuntu 9.10 (32-bit)"}, {"id":127,"oscategoryid":10,"description":"Ubuntu 9.10 (64-bit)"}, {"id":95,"oscategoryid":6,"description":"Windows 2000 Advanced Server"}, {"id":105,"oscategoryid":6,"description":"Windows 2000 Professional"}, {"id":61,"oscategoryid":6,"description":"Windows 2000 Server"}, {"id":55,"oscategoryid":6,"description":"Windows 2000 Server SP4 (32-bit)"}, {"id":65,"oscategoryid":6,"description":"Windows 3.1"}, {"id":48,"oscategoryid":6,"description":"Windows 7 (32-bit)"}, {"id":49,"oscategoryid":6,"description":"Windows 7 (64-bit)"}, {"id":63,"oscategoryid":6,"description":"Windows 95"}, {"id":62,"oscategoryid":6,"description":"Windows 98"}, {"id":64,"oscategoryid":6,"description":"Windows NT 4"}, {"id":204,"oscategoryid":6,"description":"Windows PV"}, {"id":87,"oscategoryid":6,"description":"Windows Server 2003 DataCenter Edition(32-bit)"}, {"id":88,"oscategoryid":6,"description":"Windows Server 2003 DataCenter Edition(64-bit)"}, {"id":50,"oscategoryid":6,"description":"Windows Server 2003 Enterprise Edition(32-bit)"}, {"id":51,"oscategoryid":6,"description":"Windows Server 2003 Enterprise Edition(64-bit)"}, {"id":89,"oscategoryid":6,"description":"Windows Server 2003 Standard Edition(32-bit)"}, {"id":90,"oscategoryid":6,"description":"Windows Server 2003 Standard Edition(64-bit)"}, {"id":91,"oscategoryid":6,"description":"Windows Server 2003 Web Edition"}, {"id":52,"oscategoryid":6,"description":"Windows Server 2008 (32-bit)"}, {"id":53,"oscategoryid":6,"description":"Windows Server 2008 (64-bit)"}, {"id":54,"oscategoryid":6,"description":"Windows Server 2008 R2 (64-bit)"}, {"id":56,"oscategoryid":6,"description":"Windows Vista (32-bit)"}, {"id":101,"oscategoryid":6,"description":"Windows Vista (64-bit)"}, {"id":93,"oscategoryid":6,"description":"Windows XP (32-bit)"}, {"id":94,"oscategoryid":6,"description":"Windows XP (64-bit)"}, {"id":57,"oscategoryid":6,"description":"Windows XP SP2 (32-bit)"}, {"id":58,"oscategoryid":6,"description":"Windows XP SP3 (32-bit)"} ] } }
@@ -0,0 +1 @@
{"listsnapshotsresponse" : { "count":3 ,"snapshot" : [ {"id":188402,"account":"User1","domainid":2345,"domain":"70000001000","snapshottype":"MANUAL","volumeid":89341,"volumename":"ROOT-12344","volumetype":"ROOT","created":"2014-09-17T19:55:48+0900","name":"i-123-87654-VM_ROOT-12344_20140917105548","intervaltype":"MANUAL","state":"BackedUp"}, {"id":188401,"account":"User1","domainid":2345,"domain":"70000001100","snapshottype":"MANUAL","volumeid":89342,"volumename":"ROOT-12345","volumetype":"ROOT","created":"2014-09-17T19:44:41+0900","name":"i-123-87654-VM_ROOT-12345_20140917104441","intervaltype":"MANUAL","state":"BackedUp"}, {"id":188400,"account":"User1","domainid":2345,"domain":"70000001100","snapshottype":"MANUAL","volumeid":89343,"volumename":"ROOT-71128","volumetype":"ROOT","created":"2014-09-17T19:34:58+0900","name":"i-882-71128-VM_ROOT-71128_20140917103458","intervaltype":"MANUAL","state":"BackedUp"}]}}
@@ -0,0 +1 @@
{ "queryasyncjobresultresponse" : {"jobid":1300001,"jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"snapshot":{"id":190547,"account":"User1","domainid":2345,"domain":"70000001000","snapshottype":"MANUAL","volumeid":"fe1ada16-57a0-40ae-b577-01a153690fb4","volumename":"ROOT-12345","volumetype":"ROOT","created":"2014-09-22T19:09:47+0900","name":"i-123-87654-VM_ROOT-23456_20140917105548","intervaltype":"MANUAL","state":"CreatedOnPrimary"}}} }
@@ -0,0 +1 @@
{ "queryasyncjobresultresponse" : {"jobid":1300002,"jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"success":true}} }
@@ -0,0 +1 @@
{ "queryasyncjobresultresponse" : {"jobid":1300003,"jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"template":{"id":10260,"name":"test-libcloud-template","displaytext":"test-libcloud-template","ispublic":false,"created":"2014-09-22T19:14:28+0900","isready":true,"passwordenabled":false,"format":"OVA","isfeatured":false,"crossZones":false,"ostypeid":99,"ostypename":"Other Linux (64-bit)","account":"User1","zoneid":2,"zonename":"zone1","status":"Download Complete","size":16106127360,"templatetype":"USER","hypervisor":"VMware","domain":"70000001100","domainid":1105,"isextractable":true,"checksum":"d8fc299515d138b13680014d05031d8b","sourcetemplateid":10259}}} }
42 changes: 42 additions & 0 deletions libcloud/test/compute/test_cloudstack.py
Expand Up @@ -706,6 +706,48 @@ def test_ex_delete_tags(self):
resp = self.driver.ex_delete_tags([node.id], 'UserVm', tag_keys)
self.assertTrue(resp)

def test_list_snapshots(self):
snapshots = self.driver.list_snapshots()
self.assertEqual(len(snapshots), 3)

snap = snapshots[0]
self.assertEqual(snap.id, 188402)
self.assertEqual(snap.extra['name'], "i-123-87654-VM_ROOT-12344_20140917105548")
self.assertEqual(snap.extra['volume_id'], 89341)

def test_create_volume_snapshot(self):
volume = self.driver.list_volumes()[0]
snapshot = self.driver.create_volume_snapshot(volume)

self.assertEqual(snapshot.id, 190547)
self.assertEqual(snapshot.extra['name'], "i-123-87654-VM_ROOT-23456_20140917105548")
self.assertEqual(snapshot.extra['volume_id'], "fe1ada16-57a0-40ae-b577-01a153690fb4")

def test_destroy_volume_snapshot(self):
snapshot = self.driver.list_snapshots()[0]
resp = self.driver.destroy_volume_snapshot(snapshot)
self.assertTrue(resp)

def test_ex_create_snapshot_template(self):
snapshot = self.driver.list_snapshots()[0]

template = self.driver.ex_create_snapshot_template(snapshot, "test-libcloud-template", 99)

self.assertEqual(template.id, '10260')
self.assertEqual(template.name, "test-libcloud-template")
self.assertEqual(template.extra['displaytext'], "test-libcloud-template")
self.assertEqual(template.extra['hypervisor'], "VMware")
self.assertEqual(template.extra['os'], "Other Linux (64-bit)")

def test_ex_list_os_types(self):
os_types = self.driver.ex_list_os_types()

self.assertEqual(len(os_types), 146)

self.assertEqual(os_types[0]['id'], 69)
self.assertEqual(os_types[0]['oscategoryid'], 7)
self.assertEqual(os_types[0]['description'], "Asianux 3(32-bit)")


class CloudStackTestCase(CloudStackCommonTestCase, unittest.TestCase):
def test_driver_instantiation(self):
Expand Down