mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Merge pull request #43969 from fishhead108/add-cloud.vmware.remove_snapshot-feature
Remove snapshot by name feature
This commit is contained in:
commit
f7a0979449
@ -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.
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user