Skip to content

Commit

Permalink
support save snapshot with memory state
Browse files Browse the repository at this point in the history
  • Loading branch information
nahumtimerman committed Feb 5, 2018
1 parent 3732549 commit 72cc32c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,20 +411,23 @@ def get_vm_details(self, context,cancellation_context, requests_json):
cancellation_context)
return set_command_result(result=res, unpicklable=False)

def save_snapshot(self, context, snapshot_name):
def save_snapshot(self, context, snapshot_name, save_memory='No'):
"""
Saves virtual machine to a snapshot
:param context: resource context of the vCenterShell
:type context: models.QualiDriverModels.ResourceCommandContext
:param snapshot_name: snapshot name to save to
:type snapshot_name: str
:param save_memory: Snapshot the virtual machine's memory. Lookup, Yes / No
:type save_memory: str
:return:
"""
resource_details = self._parse_remote_model(context)
created_snapshot_path = self.command_wrapper.execute_command_with_connection(context,
self.snapshot_saver.save_snapshot,
resource_details.vm_uuid,
snapshot_name)
snapshot_name,
save_memory)
return set_command_result(created_snapshot_path)

def restore_snapshot(self, context, snapshot_name):
Expand Down
20 changes: 16 additions & 4 deletions package/cloudshell/cp/vcenter/commands/save_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, pyvmomi_service, task_waiter):
self.pyvmomi_service = pyvmomi_service
self.task_waiter = task_waiter

def save_snapshot(self, si, logger, vm_uuid, snapshot_name):
def save_snapshot(self, si, logger, vm_uuid, snapshot_name, save_memory):
"""
Creates a snapshot of the current state of the virtual machine
Expand All @@ -31,21 +31,33 @@ def save_snapshot(self, si, logger, vm_uuid, snapshot_name):
:type vm_uuid: str
:param snapshot_name: Snapshot name to save the snapshot to
:type snapshot_name: str
:param save_memory: Snapshot the virtual machine's memory. Lookup, Yes / No
:type save_memory: str
"""
vm = self.pyvmomi_service.find_by_uuid(si, vm_uuid)

snapshot_path_to_be_created = SaveSnapshotCommand._get_snapshot_name_to_be_created(snapshot_name, vm)

save_vm_memory_to_snapshot = SaveSnapshotCommand._get_save_vm_memory_to_snapshot(save_memory)

SaveSnapshotCommand._verify_snapshot_uniquness(snapshot_path_to_be_created, vm)

task = self._create_snapshot(logger, snapshot_name, vm)
task = self._create_snapshot(logger, snapshot_name, vm, save_vm_memory_to_snapshot)

self.task_waiter.wait_for_task(task=task, logger=logger, action_name='Create Snapshot')
return snapshot_path_to_be_created

@staticmethod
def _create_snapshot(logger, snapshot_name, vm):
def _get_save_vm_memory_to_snapshot(save_memory):
return True if save_memory is not None and save_memory.lower() == 'yes' else False

@staticmethod
def _create_snapshot(logger, snapshot_name, vm, save_vm_memory_to_snapshot):
"""
:type save_vm_memory_to_snapshot: bool
"""
logger.info("Create virtual machine snapshot")
dump_memory = False
dump_memory = save_vm_memory_to_snapshot
quiesce = True
task = vm.CreateSnapshot(snapshot_name, 'Created by CloudShell vCenterShell', dump_memory, quiesce)
return task
Expand Down
21 changes: 18 additions & 3 deletions package/cloudshell/tests/test_commands/test_save_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def test_save_snapshot_should_succeed_when_there_is_no_snapshot(self):
save_snapshot_command.save_snapshot(si=si,
logger=Mock(),
vm_uuid='machine1',
snapshot_name='new_snapshot')
snapshot_name='new_snapshot',
save_memory='No')

# Assert
vm.CreateSnapshot.called_with('new_snapshot', 'Created by CloudShell vCenterShell', False, True)
Expand All @@ -50,7 +51,8 @@ def test_save_snapshot_should_succeed_when_snapshot_with_the_same_name_does_not_
save_snapshot_command.save_snapshot(si=si,
logger=Mock(),
vm_uuid='machine1',
snapshot_name='new_snapshot')
snapshot_name='new_snapshot',
save_memory='No')

# Assert
vm.CreateSnapshot.called_with('new_snapshot', 'Created by CloudShell vCenterShell', False, True)
Expand All @@ -74,4 +76,17 @@ def test_save_snapshot_should_fail_if_snaphost_exists(self):
save_snapshot_command.save_snapshot(si=si,
logger=Mock(),
vm_uuid='machine1',
snapshot_name='snapshot2')
snapshot_name='snapshot2',
save_memory='No')

def test_save_memory_yes(self):
save_memory_string = 'Yes'
self.assertTrue(SaveSnapshotCommand._get_save_vm_memory_to_snapshot(save_memory_string))
save_memory_string = 'yEs'
self.assertTrue(SaveSnapshotCommand._get_save_vm_memory_to_snapshot(save_memory_string))

def test_save_memory_no(self):
save_memory_string = 'No'
self.assertTrue(not SaveSnapshotCommand._get_save_vm_memory_to_snapshot(save_memory_string))
save_memory_string = 'nO'
self.assertTrue(not SaveSnapshotCommand._get_save_vm_memory_to_snapshot(save_memory_string))
6 changes: 4 additions & 2 deletions vcentershell_driver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_inventory(self, context):
validator = VCenterAutoModelDiscovery()
return validator.validate_and_discover(context)

def remote_save_snapshot(self, context, ports, snapshot_name):
def remote_save_snapshot(self, context, ports, snapshot_name, save_memory):
"""
Saves virtual machine to a snapshot
:param context: resource context of the vCenterShell
Expand All @@ -95,9 +95,11 @@ def remote_save_snapshot(self, context, ports, snapshot_name):
:type ports: list[string]
:param snapshot_name: snapshot name to save to
:type snapshot_name: str
:param save_memory: Snapshot the virtual machine's memory. Lookup, Yes / No
:type save_memory: str
:return:
"""
return self.command_orchestrator.save_snapshot(context, snapshot_name)
return self.command_orchestrator.save_snapshot(context, snapshot_name, save_memory)

def remote_restore_snapshot(self, context, ports, snapshot_name):
"""
Expand Down
1 change: 1 addition & 0 deletions vcentershell_driver/drivermetadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Command Description="" DisplayName="Save Snapshot" Name="remote_save_snapshot" Tags="remote_connectivity,allow_unreserved">
<Parameters>
<Parameter DefaultValue="" Description="Please enter the VM snapshot name, for example Snapshot1" DisplayName="Snapshot Name" Mandatory="True" Name="snapshot_name" Type="String" />
<Parameter DefaultValue="No" Description="Snapshot the virtual machine's memory" DisplayName="Save Memory" Mandatory="True" Name="save_memory" Type="Lookup" AllowedValues="Yes,No" />
</Parameters>
</Command>
<Command Description="" DisplayName="Restore Snapshot" Name="remote_restore_snapshot" Tags="remote_connectivity,allow_unreserved">
Expand Down

0 comments on commit 72cc32c

Please sign in to comment.