|
1 | 1 | import time |
2 | | - |
| 2 | +from datetime import date |
3 | 3 | import jsonpickle |
| 4 | +from cloudshell.cp.vcenter.models.OrchestrationSaveResult import OrchestrationSaveResult |
| 5 | +from cloudshell.cp.vcenter.models.OrchestrationSavedArtifactsInfo import OrchestrationSavedArtifactsInfo |
| 6 | +from cloudshell.cp.vcenter.models.OrchestrationSavedArtifact import OrchestrationSavedArtifact |
4 | 7 | from pyVim.connect import SmartConnect, Disconnect |
5 | 8 |
|
6 | 9 | from cloudshell.cp.vcenter.commands.connect_dvswitch import VirtualSwitchConnectCommand |
|
17 | 20 | from cloudshell.cp.vcenter.common.cloud_shell.driver_helper import CloudshellDriverHelper |
18 | 21 | from cloudshell.cp.vcenter.common.cloud_shell.resource_remover import CloudshellResourceRemover |
19 | 22 | from cloudshell.cp.vcenter.common.model_factory import ResourceModelParser |
20 | | -from cloudshell.cp.vcenter.common.utilites.command_result import set_command_result |
| 23 | +from cloudshell.cp.vcenter.common.utilites.command_result import set_command_result, get_result_from_command_output |
21 | 24 | from cloudshell.cp.vcenter.common.utilites.common_name import generate_unique_name |
22 | 25 | from cloudshell.cp.vcenter.common.utilites.common_utils import back_slash_to_front_converter |
23 | 26 | from cloudshell.cp.vcenter.common.utilites.context_based_logger_factory import ContextBasedLoggerFactory |
@@ -406,10 +409,11 @@ def save_snapshot(self, context, snapshot_name): |
406 | 409 | :return: |
407 | 410 | """ |
408 | 411 | resource_details = self._parse_remote_model(context) |
409 | | - self.command_wrapper.execute_command_with_connection(context, |
410 | | - self.snapshot_saver.save_snapshot, |
411 | | - resource_details.vm_uuid, |
412 | | - snapshot_name) |
| 412 | + created_snapshot_path = self.command_wrapper.execute_command_with_connection(context, |
| 413 | + self.snapshot_saver.save_snapshot, |
| 414 | + resource_details.vm_uuid, |
| 415 | + snapshot_name) |
| 416 | + return set_command_result(created_snapshot_path) |
413 | 417 |
|
414 | 418 | def restore_snapshot(self, context, snapshot_name): |
415 | 419 | """ |
@@ -439,3 +443,50 @@ def get_snapshots(self, context): |
439 | 443 | self.snapshots_retriever.get_snapshots, |
440 | 444 | resource_details.vm_uuid) |
441 | 445 | return set_command_result(result=res, unpicklable=False) |
| 446 | + |
| 447 | + def orchestration_save(self, context, mode="shallow", custom_params=None): |
| 448 | + """ |
| 449 | + Creates a snapshot with a unique name and returns SavedResults as JSON |
| 450 | + :param context: resource context of the vCenterShell |
| 451 | + :param mode: Snapshot save mode, default shallow. Currently not it use |
| 452 | + :param custom_params: Set of custom parameter to be supported in the future |
| 453 | + :return: SavedResults serialized as JSON |
| 454 | + :rtype: SavedResults |
| 455 | + """ |
| 456 | + resource_details = self._parse_remote_model(context) |
| 457 | + created_date = date.today() |
| 458 | + snapshot_name = created_date.strftime('%y_%m_%d %H_%M_%S_%f') |
| 459 | + created_snapshot_path = self.save_snapshot(context=context, snapshot_name=snapshot_name) |
| 460 | + |
| 461 | + created_snapshot_path = self._strip_double_quotes(created_snapshot_path) |
| 462 | + |
| 463 | + orchestration_saved_artifact = OrchestrationSavedArtifact() |
| 464 | + orchestration_saved_artifact.artifact_type = 'vcenter_snapshot' |
| 465 | + orchestration_saved_artifact.identifier = created_snapshot_path |
| 466 | + |
| 467 | + saved_artifacts_info = OrchestrationSavedArtifactsInfo( |
| 468 | + resource_name=resource_details.cloud_provider, |
| 469 | + created_date=created_date, |
| 470 | + restore_rules={'requires_same_resource': True}, |
| 471 | + saved_artifact=orchestration_saved_artifact) |
| 472 | + |
| 473 | + orchestration_save_result = OrchestrationSaveResult(saved_artifacts_info) |
| 474 | + |
| 475 | + return set_command_result(result=orchestration_save_result, unpicklable=False) |
| 476 | + |
| 477 | + @staticmethod |
| 478 | + def _strip_double_quotes(created_snapshot_path): |
| 479 | + if created_snapshot_path.startswith('"') and created_snapshot_path.endswith('"'): |
| 480 | + created_snapshot_path = created_snapshot_path[1:-1] |
| 481 | + return created_snapshot_path |
| 482 | + |
| 483 | + def orchestration_restore(self, context, saved_details): |
| 484 | + """ |
| 485 | +
|
| 486 | + :param context: |
| 487 | + :param saved_details: |
| 488 | + :return: |
| 489 | + """ |
| 490 | + saved_artifacts_info = get_result_from_command_output(saved_details) |
| 491 | + snapshot_name = saved_artifacts_info['saved_artifacts_info']['saved_artifact']['identifier'] |
| 492 | + return self.restore_snapshot(context=context, snapshot_name=snapshot_name) |
0 commit comments