Merge pull request #43969 from fishhead108/add-cloud.vmware.remove_snapshot-feature

Remove snapshot by name feature
This commit is contained in:
Nicole Thomas 2017-12-06 13:08:14 -05:00 committed by GitHub
commit f7a0979449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 0 deletions

View File

@ -3652,6 +3652,65 @@ def revert_to_snapshot(name, kwargs=None, call=None):
return msg
def remove_snapshot(name, kwargs=None, call=None):
'''
Remove a snapshot of the specified virtual machine in this VMware environment
CLI Example:
.. code-block:: bash
salt-cloud -a remove_snapshot vmname snapshot_name="mySnapshot"
salt-cloud -a remove_snapshot vmname snapshot_name="mySnapshot" [remove_children="True"]
'''
if call != 'action':
raise SaltCloudSystemExit(
'The create_snapshot action must be called with '
'-a or --action.'
)
if kwargs is None:
kwargs = {}
snapshot_name = kwargs.get('snapshot_name') if kwargs and 'snapshot_name' in kwargs else None
remove_children = _str_to_bool(kwargs.get('remove_children', False))
if not snapshot_name:
raise SaltCloudSystemExit(
'You must specify snapshot name for the snapshot to be deleted.'
)
vm_ref = salt.utils.vmware.get_mor_by_property(_get_si(), vim.VirtualMachine, name)
if not _get_snapshot_ref_by_name(vm_ref, snapshot_name):
raise SaltCloudSystemExit(
'Сould not find the snapshot with the specified name.'
)
try:
snap_obj = _get_snapshot_ref_by_name(vm_ref, snapshot_name).snapshot
task = snap_obj.RemoveSnapshot_Task(remove_children)
salt.utils.vmware.wait_for_task(task, name, 'remove snapshot', 5, 'info')
except Exception as exc:
log.error(
'Error while removing snapshot of {0}: {1}'.format(
name,
exc
),
# Show the traceback if the debug logging level is enabled
exc_info_on_loglevel=logging.DEBUG
)
return 'failed to remove snapshot'
if vm_ref.snapshot:
return {'Snapshot removed successfully': _get_snapshots(vm_ref.snapshot.rootSnapshotList,
vm_ref.snapshot.currentSnapshot)}
else:
return 'Snapshots removed successfully'
def remove_all_snapshots(name, kwargs=None, call=None):
'''
Remove all the snapshots present for the specified virtual machine.

View File

@ -619,6 +619,30 @@ class VMwareTestCase(ExtendedTestCase):
call='function'
)
def test_remove_snapshot_call(self):
'''
Tests that a SaltCloudSystemExit is raised when trying to call remove_snapshot
with anything other than --action or -a.
'''
self.assertRaises(
SaltCloudSystemExit,
vmware.remove_snapshot,
name=VM_NAME,
kwargs={'snapshot_name': 'mySnapshot'},
call='function'
)
def test_remove_snapshot_call_no_snapshot_name_in_kwargs(self):
'''
Tests that a SaltCloudSystemExit is raised when name is not present in kwargs.
'''
self.assertRaises(
SaltCloudSystemExit,
vmware.remove_snapshot,
name=VM_NAME,
call='action'
)
def test_remove_all_snapshots_call(self):
'''
Tests that a SaltCloudSystemExit is raised when trying to call remove_all_snapshots