Skip to content

Commit

Permalink
Merge pull request #19 from QualiSystems/feature/apiddubny_costya_ale…
Browse files Browse the repository at this point in the history
…xey_fix_save_sandbox

Feature, fix save sandbox
  • Loading branch information
Costya-Y authored May 26, 2021
2 parents 056c6d2 + fe0bc67 commit 478a0b0
Show file tree
Hide file tree
Showing 54 changed files with 1,575 additions and 1,315 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
- repo: https://github.com/timothycrosley/isort
rev: 5.6.4
hooks:
- id: isort
language_version: python3.7
Expand Down
43 changes: 38 additions & 5 deletions cloudshell/cp/vcenter/commands/DeleteInstance.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from cloudshell.api.cloudshell_api import CloudShellAPISession

from cloudshell.cp.vcenter import constants
from cloudshell.cp.vcenter.common.utilites.common_utils import (
get_error_message_from_exception,
)
from cloudshell.cp.vcenter.constants import VM_CUSTOMIZATION_SPEC_CUSTOM_PARAM
from cloudshell.cp.vcenter.common.vcenter.vm_location import VMLocation


class DestroyVirtualMachineCommand(object):
""" Command to Destroy a VM """

def __init__(self, pv_service, resource_remover, disconnector):
def __init__(self, pv_service, folder_manager, resource_remover, disconnector):
"""
:param pv_service: pv_service Instance
:type pv_service: cloudshell.cp.vcenter.common.vcenter.vmomi_service.pyVmomiService
:param resource_remover: CloudshellResourceRemover
"""
self.pv_service = pv_service
self.folder_manager = folder_manager
self.resource_remover = resource_remover
self.disconnector = disconnector

Expand Down Expand Up @@ -63,15 +66,28 @@ def destroy(
)
return result

def DeleteInstance(self, si, logger, session, vcenter_data_model, resource_model):
def DeleteInstance(
self,
si,
logger,
session,
vcenter_data_model,
reservation_id,
resource_model,
):
"""
:param si:
:param logger:
:param CloudShellAPISession session:
:param session:
:param vcenter_data_model:
:param reservation_id:
:param resource_model:
:return:
"""
# find vm
vm = self.pv_service.find_by_uuid(si, resource_model.vm_uuid)

if vm is not None:
# destroy vm
result = self.pv_service.destroy_vm(vm=vm, logger=logger)
Expand All @@ -86,12 +102,29 @@ def DeleteInstance(self, si, logger, session, vcenter_data_model, resource_model

vm_customization_spec = self._get_custom_param(
params=resource_model.vm_custom_params,
name=VM_CUSTOMIZATION_SPEC_CUSTOM_PARAM,
name=constants.VM_CUSTOMIZATION_SPEC_CUSTOM_PARAM,
)

if vm_customization_spec:
self.pv_service.delete_customization_spec(si=si, name=vm_customization_spec)

vm_folder_path = VMLocation.combine(
[
resource_model.app_request_model.vm_location
or vcenter_data_model.vm_location,
constants.DEPLOYED_APPS_FOLDER,
reservation_id,
]
)

self.folder_manager.delete_folder_if_empty(
si=si,
folder_full_path=VMLocation.combine(
[vcenter_data_model.default_datacenter, vm_folder_path]
),
logger=logger,
)

return result

@staticmethod
Expand Down
41 changes: 38 additions & 3 deletions cloudshell/cp/vcenter/commands/command_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from datetime import datetime

import jsonpickle
from cloudshell.cp.core.models import DeployApp, DeployAppResult, SaveApp, SaveAppResult
from pyVim.connect import Disconnect, SmartConnect

from cloudshell.cp.core.models import DeployApp, DeployAppResult, SaveApp, SaveAppResult

from cloudshell.cp.vcenter import constants
from cloudshell.cp.vcenter.commands.cluster_usage import GetClusterUsageCommand
from cloudshell.cp.vcenter.commands.connect_dvswitch import VirtualSwitchConnectCommand
from cloudshell.cp.vcenter.commands.connect_orchestrator import (
Expand Down Expand Up @@ -123,6 +125,13 @@ def __init__(self):
resource_remover = CloudshellResourceRemover()
ovf_service = OvfImageDeployerService(self.resource_model_parser)

self.deployment_path_map = {
constants.VM_FROM_VM_DEPLOYMENT_PATH: vCenterCloneVMFromVMResourceModel,
constants.VM_FROM_TEMPLATE_DEPLOYMENT_PATH: vCenterVMFromTemplateResourceModel,
constants.VM_FROM_LINKED_CLONE_DEPLOYMENT_PATH: VCenterDeployVMFromLinkedCloneResourceModel,
constants.VM_FROM_IMAGE_DEPLOYMENT_PATH: vCenterVMFromImageResourceModel,
}

self.vm_loader = VMLoader(pv_service)

self.cluster_usage_command = GetClusterUsageCommand(pv_service)
Expand Down Expand Up @@ -195,6 +204,7 @@ def __init__(self):
# Destroy VM Command
self.destroy_virtual_machine_command = DestroyVirtualMachineCommand(
pv_service=pv_service,
folder_manager=self.folder_manager,
resource_remover=resource_remover,
disconnector=self.virtual_switch_disconnect_command,
)
Expand Down Expand Up @@ -281,6 +291,7 @@ def save_sandbox(self, context, save_actions, cancellation_context):
connection = self.command_wrapper.execute_command_with_connection(
context,
self.save_app_command.save_app,
AppResourceModel(),
save_actions,
cancellation_context,
)
Expand All @@ -299,6 +310,7 @@ def delete_saved_sandbox(self, context, delete_saved_apps, cancellation_context)
context,
self.delete_saved_sandbox_command.delete_sandbox,
delete_saved_apps,
AppResourceModel(),
cancellation_context,
)
delete_saved_apps_results = connection
Expand Down Expand Up @@ -576,14 +588,37 @@ def _parse_remote_model(self, context):
)
resource = context.remote_endpoints[0]

dictionary = jsonpickle.decode(resource.app_context.deployed_app_json)
holder = DeployDataHolder(dictionary)
deployed_app = jsonpickle.decode(resource.app_context.deployed_app_json)
app_request = jsonpickle.decode(resource.app_context.app_request_json)

holder = DeployDataHolder(deployed_app)
app_resource_detail = GenericDeployedAppResourceModel()
app_resource_detail.vm_uuid = holder.vmdetails.uid
app_resource_detail.cloud_provider = context.resource.fullname
app_resource_detail.fullname = resource.fullname
app_resource_detail.deployment_path = app_request["deploymentService"]["model"]

if app_resource_detail.deployment_path not in self.deployment_path_map:
raise Exception(
f"Invalid deployment path '{app_resource_detail.deployment_path}'. "
f"Possible values are: {self.deployment_path_map.keys()}"
)

app_resource_detail.app_request_model = (
self.resource_model_parser.convert_to_resource_model(
attributes={
attribute["name"]: attribute["value"]
for attribute in app_request["deploymentService"]["attributes"]
},
resource_model_type=self.deployment_path_map[
app_resource_detail.deployment_path
],
)
)

if hasattr(holder.vmdetails, "vmCustomParams"):
app_resource_detail.vm_custom_params = holder.vmdetails.vmCustomParams

return app_resource_detail

def power_on_not_roemote(self, context, vm_uuid, resource_fullname):
Expand Down
36 changes: 5 additions & 31 deletions cloudshell/cp/vcenter/commands/delete_saved_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

from cloudshell.cp.core.models import ActionResultBase

from cloudshell.cp.vcenter.common.utilites.savers.artifact_saver import (
ArtifactHandler,
UnsupportedArtifactHandler,
)
from cloudshell.cp.vcenter.common.utilites.savers.artifact_saver import ArtifactHandler
from cloudshell.cp.vcenter.common.vcenter.task_waiter import SynchronousTaskWaiter
from cloudshell.cp.vcenter.common.vcenter.vmomi_service import pyVmomiService

Expand Down Expand Up @@ -49,8 +46,10 @@ def delete_sandbox(
self,
si,
logger,
session,
vcenter_data_model,
delete_sandbox_actions,
app_resource_model,
cancellation_context,
):
"""
Expand Down Expand Up @@ -84,6 +83,8 @@ def delete_sandbox(
vcenter_data_model,
si,
logger,
session,
app_resource_model,
self.deployer,
None,
self.resource_model_parser,
Expand All @@ -96,10 +97,6 @@ def delete_sandbox(
for k, g in actions_grouped_by_save_types
}

self._validate_save_deployment_models(
artifactHandlersToActions, delete_sandbox_actions, results
)

error_results = [r for r in results if not r.success]
if not error_results:
results = self._execute_delete_saved_sandbox(
Expand All @@ -108,29 +105,6 @@ def delete_sandbox(

return results

def _validate_save_deployment_models(
self, artifactHandlersToActions, delete_sandbox_actions, results
):
unsupported_save_deployment_models = [
a.unsupported_save_type
for a in list(artifactHandlersToActions.keys())
if isinstance(a, UnsupportedArtifactHandler)
]
if unsupported_save_deployment_models:
for action in delete_sandbox_actions:
unsupported_msg = "Unsupported save deployment models: {0}".format(
", ".join(unsupported_save_deployment_models)
)
results.append(
ActionResultBase(
type="DeleteSavedAppResult",
actionId=action.actionId,
success=False,
infoMessage=unsupported_msg,
errorMessage=unsupported_msg,
)
)

def _execute_delete_saved_sandbox(
self, artifactHandlersToActions, cancellation_context, logger, results
):
Expand Down
Loading

0 comments on commit 478a0b0

Please sign in to comment.