From: Vipin Balachandran Date: Fri, 8 Aug 2014 05:23:02 +0000 (+0530) Subject: VMware: Check snapshot and rename backing X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=11c2dcc0428768a457b18bbbfb465284422054c4;p=openstack-build%2Fcinder-build.git VMware: Check snapshot and rename backing 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 --- diff --git a/cinder/tests/test_vmware_volumeops.py b/cinder/tests/test_vmware_volumeops.py index 8547b02e9..cb09a03ae 100644 --- a/cinder/tests/test_vmware_volumeops.py +++ b/cinder/tests/test_vmware_volumeops.py @@ -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 diff --git a/cinder/volume/drivers/vmware/volumeops.py b/cinder/volume/drivers/vmware/volumeops.py index abca18cdd..c3a4c68f1 100644 --- a/cinder/volume/drivers/vmware/volumeops.py +++ b/cinder/volume/drivers/vmware/volumeops.py @@ -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.