[Kimchi-devel] [PATCH 04/10] snapshot: List domain snapshots

Crístian Viana vianac at linux.vnet.ibm.com
Wed Nov 12 13:08:42 UTC 2014


A new command is added to list all existing snapshots on a virtual
machine:

GET /vms/<vm-name>/snapshots

It returns a list of snapshot structures (as returned by 'GET
/vms/<vm-name>/snapshots/<snapshot-name>') which exist on a virtual
machine.

Signed-off-by: Crístian Viana <vianac at linux.vnet.ibm.com>
---
 docs/API.md                     |  1 +
 src/kimchi/i18n.py              |  1 +
 src/kimchi/mockmodel.py         |  4 ++++
 src/kimchi/model/vmsnapshots.py | 11 +++++++++++
 tests/test_rest.py              | 18 ++++++++++++++++++
 5 files changed, 35 insertions(+)

diff --git a/docs/API.md b/docs/API.md
index 616958a..38c8a59 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -193,6 +193,7 @@ Represents a snapshot of the Virtual Machine's primary monitor.
 * **POST**: Create a new snapshot on a VM.
     * name: The snapshot name (optional, defaults to a value based on the
             current time).
+* **GET**: Retrieve a list of snapshots on a VM.
 
 ### Sub-resource: Snapshot
 **URI:** /vms/*:name*/snapshots/*:snapshot*
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 159a2d2..0a6fee3 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -315,4 +315,5 @@ messages = {
     "KCHSNAP0002E": _("Unable to create snapshot '%(name)s' on virtual machine '%(vm)s'. Details: %(err)s"),
     "KCHSNAP0003E": _("Snapshot '%(name)s' does not exist on virtual machine '%(vm)s'."),
     "KCHSNAP0004E": _("Unable to retrieve snapshot '%(name)s' on virtual machine '%(vm)s'. Details: %(err)s"),
+    "KCHSNAP0005E": _("Unable to list snapshots on virtual machine '%(vm)s'. Details: %(err)s"),
 }
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index cb52750..d4d53fc 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -1000,6 +1000,10 @@ class MockModel(object):
 
         cb('OK', True)
 
+    def vmsnapshots_get_list(self, vm_name):
+        vm = self._get_vm(vm_name)
+        return sorted(vm.snapshots.keys(), key=unicode.lower)
+
     def vmsnapshot_lookup(self, vm_name, name):
         vm = self._get_vm(vm_name)
 
diff --git a/src/kimchi/model/vmsnapshots.py b/src/kimchi/model/vmsnapshots.py
index 27fd052..adc785f 100644
--- a/src/kimchi/model/vmsnapshots.py
+++ b/src/kimchi/model/vmsnapshots.py
@@ -95,6 +95,17 @@ class VMSnapshotsModel(object):
 
         cb('OK', True)
 
+    def get_list(self, vm_name):
+        vir_dom = VMModel.get_vm(vm_name, self.conn)
+
+        try:
+            vir_snaps = vir_dom.listAllSnapshots(0)
+            return sorted([s.getName().decode('utf-8') for s in vir_snaps],
+                          key=unicode.lower)
+        except libvirt.libvirtError, e:
+            raise OperationFailed('KCHSNAP0005E',
+                                  {'vm': vm_name, 'err': e.message})
+
 
 class VMSnapshotModel(object):
     def __init__(self, **kargs):
diff --git a/tests/test_rest.py b/tests/test_rest.py
index e6ce715..6af0d72 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -408,6 +408,24 @@ class RestTests(unittest.TestCase):
         self.assertEquals(u'', snap['parent'])
         self.assertEquals(u'shutoff', snap['state'])
 
+        resp = self.request('/vms/test-vm/snapshots', '{}', 'GET')
+        self.assertEquals(200, resp.status)
+        snaps = json.loads(resp.read())
+        self.assertEquals(1, len(snaps))
+
+        resp = self.request('/vms/test-vm/snapshots', '{}', 'POST')
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
+        resp = self.request('/tasks/%s' % task['id'], '{}', 'GET')
+        task = json.loads(resp.read())
+        self.assertEquals('finished', task['status'])
+
+        resp = self.request('/vms/test-vm/snapshots', '{}', 'GET')
+        self.assertEquals(200, resp.status)
+        snaps = json.loads(resp.read())
+        self.assertEquals(2, len(snaps))
+
         # Delete the VM
         resp = self.request('/vms/test-vm', '{}', 'DELETE')
         self.assertEquals(204, resp.status)
-- 
1.9.3




More information about the Kimchi-devel mailing list