]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
VMware: Check snapshot and rename backing
authorVipin Balachandran <vbala@vmware.com>
Fri, 8 Aug 2014 05:23:02 +0000 (10:53 +0530)
committerVipin Balachandran <vbala@vmware.com>
Wed, 13 Aug 2014 12:00:38 +0000 (17:30 +0530)
This patch adds methods for: a) checking existence of backing VM snapshots;
and b) rename backing.

Change-Id: Iede168e28d9569a5a3331479c8096c3cca5140e9
Partial-Bug: #1275682
Implements: blueprint vmdk-backup

cinder/tests/test_vmware_volumeops.py
cinder/volume/drivers/vmware/volumeops.py

index 8547b02e9f5e462b028fbe6e74527d7ec5cb0b64..cb09a03aeca2bddfc2d0e3e3cebb67a60fc952f7 100644 (file)
@@ -799,6 +799,26 @@ class VolumeOpsTestCase(test.TestCase):
                                                    backing,
                                                    'snapshot')
 
+    def test_snapshot_exists(self):
+        backing = mock.sentinel.backing
+        invoke_api = self.session.invoke_api
+        invoke_api.return_value = None
+
+        self.assertFalse(self.vops.snapshot_exists(backing))
+        invoke_api.assert_called_once_with(vim_util,
+                                           'get_object_property',
+                                           self.session.vim,
+                                           backing,
+                                           'snapshot')
+
+        snapshot = mock.Mock()
+        invoke_api.return_value = snapshot
+        snapshot.rootSnapshotList = None
+        self.assertFalse(self.vops.snapshot_exists(backing))
+
+        snapshot.rootSnapshotList = [mock.Mock()]
+        self.assertTrue(self.vops.snapshot_exists(backing))
+
     def test_delete_snapshot(self):
         backing = mock.sentinel.backing
         snapshot_name = mock.sentinel.snapshot_name
@@ -967,6 +987,20 @@ class VolumeOpsTestCase(test.TestCase):
                                                         spec=reconfig_spec)
         self.session.wait_for_task.assert_called_once_with(task)
 
+    def test_rename_backing(self):
+        task = mock.sentinel.task
+        self.session.invoke_api.return_value = task
+
+        backing = mock.sentinel.backing
+        new_name = mock.sentinel.new_name
+        self.vops.rename_backing(backing, new_name)
+
+        self.session.invoke_api.assert_called_once_with(self.session.vim,
+                                                        "Rename_Task",
+                                                        backing,
+                                                        newName=new_name)
+        self.session.wait_for_task.assert_called_once_with(task)
+
     def test_delete_file(self):
         file_mgr = mock.sentinel.file_manager
         self.session.vim.service_content.fileManager = file_mgr
index abca18cddf2028055b08541c3361a5bcd05ab919..c3a4c68f1f7c41da1511a42843f173bc4faa0352 100644 (file)
@@ -937,6 +937,15 @@ class VMwareVolumeOps(object):
         for root in snapshot.rootSnapshotList:
             return VMwareVolumeOps._get_snapshot_from_tree(name, root)
 
+    def snapshot_exists(self, backing):
+        """Check if the given backing contains snapshots."""
+        snapshot = self._session.invoke_api(vim_util, 'get_object_property',
+                                            self._session.vim, backing,
+                                            'snapshot')
+        if snapshot is None or snapshot.rootSnapshotList is None:
+            return False
+        return len(snapshot.rootSnapshotList) != 0
+
     def delete_snapshot(self, backing, name):
         """Delete a given snapshot from volume backing.
 
@@ -1065,6 +1074,25 @@ class VMwareVolumeOps(object):
         self._session.wait_for_task(reconfig_task)
         LOG.debug("Backing VM: %s reconfigured with new disk.", backing)
 
+    def rename_backing(self, backing, new_name):
+        """Rename backing VM.
+
+        :param backing: VM to be renamed
+        :param new_name: new VM name
+        """
+        LOG.info(_("Renaming backing VM: %(backing)s to %(new_name)s."),
+                 {'backing': backing,
+                  'new_name': new_name})
+        rename_task = self._session.invoke_api(self._session.vim,
+                                               "Rename_Task",
+                                               backing,
+                                               newName=new_name)
+        LOG.debug("Task: %s created for renaming VM.", rename_task)
+        self._session.wait_for_task(rename_task)
+        LOG.info(_("Backing VM: %(backing)s renamed to %(new_name)s."),
+                 {'backing': backing,
+                  'new_name': new_name})
+
     def delete_file(self, file_path, datacenter=None):
         """Delete file or folder on the datastore.