[Kimchi-devel] [PATCH v2 05/10] snapshot: Delete a domain snapshot

Crístian Viana vianac at linux.vnet.ibm.com
Wed Nov 12 19:12:07 UTC 2014


A new command is added to delete an existing snapshot:

DELETE /vms/<vm-name>/snapshots/<snapshot-name>

It deletes the specified snapshot from the virtual machine.

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

diff --git a/docs/API.md b/docs/API.md
index 590cb6c..9e7365a 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -204,6 +204,8 @@ Represents a snapshot of the Virtual Machine's primary monitor.
     * parent: The name of the parent snapshot, or an empty string if there is
               no parent.
     * state: The corresponding domain's state when the snapshot was created.
+* **DELETE**: Delete snapshot. If the snapshot has any children, they will be
+              merged automatically with the snapshot's parent.
 
 ### Collection: Templates
 
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 0a6fee3..6a1e193 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -316,4 +316,5 @@ messages = {
     "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"),
+    "KCHSNAP0006E": _("Unable to delete snapshot '%(name)s' on virtual machine '%(vm)s'. Details: %(err)s"),
 }
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index 11b4394..480cb8e 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -1008,6 +1008,14 @@ class MockModel(object):
         except KeyError:
             raise NotFoundError('KCHSNAP0003E', {'vm': vm_name, 'name': name})
 
+    def vmsnapshot_delete(self, vm_name, name):
+        vm = self._get_vm(vm_name)
+
+        try:
+            del vm.snapshots[name]
+        except KeyError:
+            raise NotFoundError('KCHSNAP0003E', {'vm': vm_name, 'name': name})
+
     def tasks_get_list(self):
         with self.objstore as session:
             return session.get_list('task')
diff --git a/src/kimchi/model/vmsnapshots.py b/src/kimchi/model/vmsnapshots.py
index b3fb6d5..b66cd99 100644
--- a/src/kimchi/model/vmsnapshots.py
+++ b/src/kimchi/model/vmsnapshots.py
@@ -129,6 +129,15 @@ class VMSnapshotModel(object):
                 'parent': parent,
                 'state': unicode(snap_xml.state)}
 
+    def delete(self, vm_name, name):
+        try:
+            vir_snap = self.get_vmsnapshot(vm_name, name)
+            vir_snap.delete(0)
+        except libvirt.libvirtError, e:
+            raise OperationFailed('KCHSNAP0006E', {'name': name,
+                                                   'vm': vm_name,
+                                                   'err': e.message})
+
     def get_vmsnapshot(self, vm_name, name):
         vir_dom = VMModel.get_vm(vm_name, self.conn)
 
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 6af0d72..ef0fbf3 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -426,6 +426,16 @@ class RestTests(unittest.TestCase):
         snaps = json.loads(resp.read())
         self.assertEquals(2, len(snaps))
 
+        # Delete a snapshot
+        resp = self.request('/vms/test-vm/snapshots/foobar', '{}', 'DELETE')
+        self.assertEquals(404, resp.status)
+        resp = self.request('/vms/test-vm/snapshots/%s' % params['name'],
+                            '{}', 'DELETE')
+        self.assertEquals(204, resp.status)
+        resp = self.request('/vms/test-vm/snapshots/%s' % params['name'],
+                            '{}', 'GET')
+        self.assertEquals(404, resp.status)
+
         # 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