def DeleteInstance(self, context, ports):
        """
        Called during sandbox's teardown or when removing a deployed App from the sandbox

        Method deletes the VM from the cloud provider.

        If the operation fails, method should raise an exception.

        :param ResourceRemoteCommandContext context:
        :param ports:
        """
        dump_context("delete-context", context, r"C:\temp\context")
        with LoggingSessionContext(context) as logger:
            api = CloudShellSessionContext(context).get_api()
            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)
            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            service_provider = ServiceProvider(api_clients, logger, None)
            DeployedVMActions.register_deployment_path(KubernetesDeployedApp)
            deployed_app = DeployedVMActions.from_remote_resource(
                context.remote_endpoints[0], api).deployed_app
            DeleteInstanceFlow(logger, resource_config,
                               service_provider).delete_instance(
                                   deployed_app.kubernetes_name,
                                   deployed_app.name, deployed_app.namespace)
    def remote_refresh_ip(self, context, ports, cancellation_context):
        """

        Called when reserving a sandbox during setup, a call for each app in the sandbox can also be run manually by the sandbox end-user from the deployed App's commands pane

        Method retrieves the VM's updated IP address from the cloud provider and sets it on the deployed App resource
        Both private and public IPs are retrieved, as appropriate.

        If the operation fails, method should raise an exception.

        :param ResourceRemoteCommandContext context:
        :param ports:
        :param CancellationContext cancellation_context:
        :return:
        """
        dump_context("refresh-ip-context", context, r"C:\temp\context")
        with LoggingSessionContext(context) as logger:
            api = CloudShellSessionContext(context).get_api()
            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)
            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            service_provider = ServiceProvider(api_clients, logger, None)
            DeployedVMActions.register_deployment_path(KubernetesDeployedApp)
            deployed_app = DeployedVMActions.from_remote_resource(
                context.remote_endpoints[0], api).deployed_app
            RefreshIpFlow(logger, resource_config,
                          service_provider).refresh_ip(deployed_app)
    def PowerOn(self, context, ports):
        """
        Called when reserving a sandbox during setup, a call for each app in the sandbox can also be run manually by the sandbox end-user from the deployed App's commands pane

        Method spins up the VM

        If the operation fails, method should raise an exception.

        :param ResourceRemoteCommandContext context:
        :param ports:
        """

        dump_context("poweron-context", context, r"C:\temp\context")
        with LoggingSessionContext(context) as logger:
            api = CloudShellSessionContext(context).get_api()
            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)
            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            service_provider = ServiceProvider(api_clients, logger, None)
            DeployedVMActions.register_deployment_path(KubernetesDeployedApp)
            deployed_app = DeployedVMActions.from_remote_resource(
                context.remote_endpoints[0], api).deployed_app
            PowerFlow(logger, resource_config,
                      service_provider).power_on(deployed_app)
    def PrepareSandboxInfra(self, context, request, cancellation_context):
        """
        Called in the beginning of the orchestration flow (preparation stage), even before Deploy is called.

        Prepares all of the required infrastructure needed for a sandbox operating with L3 connectivity.
        For example, creating networking infrastructure like VPC, subnets or routing tables in AWS, storage entities such as S3 buckets, or
        keyPair objects for authentication.
        In general, any other entities needed on the sandbox level should be created here.

        Note:
        PrepareSandboxInfra can be called multiple times in a sandbox.
        Setup can be called multiple times in the sandbox, and every time setup is called, the PrepareSandboxInfra method will be called again.
        Implementation should support this use case and take under consideration that the cloud resource might already exist.
        It's recommended to follow the "get or create" pattern when implementing this method.

        When an error is raised or method returns action result with success false
        Cloudshell will fail sandbox creation, so bear that in mind when doing so.

        :param ResourceCommandContext context:
        :param str request:
        :param CancellationContext cancellation_context:
        :return:
        :rtype: str
        """
        '''
        # parse the json strings into action objects
        actions = self.request_parser.convert_driver_request_to_actions(request)
        
        action_results = _my_prepare_connectivity(context, actions, cancellation_context)
        
        return DriverResponse(action_results).to_driver_response_json()    
        '''
        dump_context("prepare-sandbox-infra-context", context,
                     r"C:\temp\context")
        dump_context("prepare-sandbox-infra-request",
                     request,
                     r"C:\temp\context",
                     obj=False)

        with LoggingSessionContext(context) as logger:
            # parse the json strings into action objects
            api = CloudShellSessionContext(context).get_api()

            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)

            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            service_provider = ServiceProvider(
                api_clients, logger,
                CancellationContextManager(cancellation_context))
            tag_service = TagsService(context)

            request_actions = PrepareSandboxInfraRequestActions.from_request(
                request)

            flow = PrepareSandboxInfraFlow(logger, resource_config,
                                           service_provider, tag_service)
            return flow.prepare(request_actions)
    def Deploy(self, context, request, cancellation_context=None):
        """
        Called when reserving a sandbox during setup, a call for each app in the sandbox.

        Method creates the compute resource in the cloud provider - VM instance or container.

        If App deployment fails, return a "success false" action result.

        :param ResourceCommandContext context:
        :param str request: A JSON string with the list of requested deployment actions
        :param CancellationContext cancellation_context:
        :return:
        :rtype: str
        """
        '''
        # parse the json strings into action objects
        actions = self.request_parser.convert_driver_request_to_actions(request)
        
        # extract DeployApp action
        deploy_action = single(actions, lambda x: isinstance(x, DeployApp))
        
        # if we have multiple supported deployment options use the 'deploymentPath' property 
        # to decide which deployment option to use. 
        deployment_name = deploy_action.actionParams.deployment.deploymentPath
                
        deploy_result = _my_deploy_method(context, actions, cancellation_context)
        return DriverResponse(deploy_result).to_driver_response_json()
        '''
        dump_context("deploy-context", context, r"C:\temp\context")
        dump_context("deploy-request", request, r"C:\temp\context", obj=False)

        with LoggingSessionContext(context) as logger:
            api = CloudShellSessionContext(context).get_api()
            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)
            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)

            DeployVMRequestActions.register_deployment_path(
                KubernetesDeployApp)
            request_actions = DeployVMRequestActions.from_request(request, api)
            service_provider = ServiceProvider(
                api_clients, logger,
                CancellationContextManager(cancellation_context))
            tag_service = TagsService(context)
            return DeployFlow(logger, resource_config, service_provider,
                              VmDetailsProvider(),
                              tag_service).deploy(request_actions)
    def CleanupSandboxInfra(self, context, request):
        """
        Called at the end of reservation teardown

        Cleans all entities (beside VMs) created for sandbox, usually entities created in the
        PrepareSandboxInfra command.

        Basically all created entities for the sandbox will be deleted by calling the methods: DeleteInstance, CleanupSandboxInfra

        If a failure occurs, return a "success false" action result.

        :param ResourceCommandContext context:
        :param str request:
        :return:
        :rtype: str
        """
        '''
        # parse the json strings into action objects
        actions = self.request_parser.convert_driver_request_to_actions(request)
        
        action_results = _my_cleanup_connectivity(context, actions)

        return DriverResponse(action_results).to_driver_response_json()    
        '''
        dump_context("cleanup-sandbox-infra-context", context,
                     r"C:\temp\context")
        dump_context("cleanup-sandbox-infra-request",
                     request,
                     r"C:\temp\context",
                     obj=False)
        with LoggingSessionContext(context) as logger:
            # parse the json strings into action objects
            api = CloudShellSessionContext(context).get_api()

            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)

            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            service_provider = ServiceProvider(api_clients, logger, None)

            request_actions = CleanupSandboxInfraRequestActions.from_request(
                request)

            flow = CleanupSandboxInfraFlow(logger, resource_config,
                                           service_provider)
            return flow.cleanup(request_actions)
    def GetVmDetails(self, context, requests, cancellation_context):
        """
        Called when reserving a sandbox during setup, a call for each app in the sandbox can also be run manually by the sandbox
        end-user from the deployed App's VM Details pane

        Method queries cloud provider for instance operating system, specifications and networking information and
        returns that as a json serialized driver response containing a list of VmDetailsData.

        If the operation fails, method should raise an exception.

        :param ResourceCommandContext context:
        :param str requests:
        :param CancellationContext cancellation_context:
        :return:
        """
        dump_context("vmditails-context", context, r"C:\temp\context")
        dump_context("vmditails-request",
                     requests,
                     r"C:\temp\context",
                     obj=False)
        with LoggingSessionContext(context) as logger:
            api = CloudShellSessionContext(context).get_api()
            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)
            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            GetVMDetailsRequestActions.register_deployment_path(
                KubernetesDeployedApp)
            request_actions = GetVMDetailsRequestActions.from_request(
                requests, api)
            service_provider = ServiceProvider(
                api_clients, logger,
                CancellationContextManager(cancellation_context))
            return VmDetialsFlow(
                logger, resource_config, service_provider,
                VmDetailsProvider()).get_vm_details(request_actions)