def CreateRouteTables(self, context, request):
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Create Route Tables command...")
            api = CloudShellSessionContext(context).get_api()

            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            request_actions = CreateRouteTablesRequestActions.from_request(request)
            reservation_info = AzureReservationInfo.from_resource_context(context)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            route_table_flow = CreateRouteTablesFlow(
                resource_config=resource_config,
                reservation_info=reservation_info,
                azure_client=azure_client,
                cs_api=api,
                logger=logger,
            )

            return route_table_flow.create_route_tables(request_actions=request_actions)
Beispiel #2
0
    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
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Deploy command...")
            logger.debug(f"Request: {request}")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api)

            cancellation_manager = CancellationContextManager(
                cancellation_context)
            reservation_info = AzureReservationInfo.from_resource_context(
                context)
            cs_ip_pool_manager = CSIPPoolManager(cs_api=api, logger=logger)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger)

            for deploy_app_cls in (AzureVMFromMarketplaceDeployApp,
                                   AzureVMFromCustomImageDeployApp):
                DeployVMRequestActions.register_deployment_path(deploy_app_cls)

            request_actions = DeployVMRequestActions.from_request(
                request=request, cs_api=api)

            if isinstance(request_actions.deploy_app,
                          AzureVMFromMarketplaceDeployApp):
                deploy_flow_class = AzureDeployMarketplaceVMFlow
            else:
                deploy_flow_class = AzureDeployCustomVMFlow

            deploy_flow = deploy_flow_class(
                resource_config=resource_config,
                azure_client=azure_client,
                cs_api=api,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                cs_ip_pool_manager=cs_ip_pool_manager,
                lock_manager=self.lock_manager,
                logger=logger)

            return deploy_flow.deploy(request_actions=request_actions)
    def PrepareSandboxInfra(self, context, request, cancellation_context):
        """Called in the beginning of the orchestration flow (preparation stage).

        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
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Prepare Sandbox Infra command...")
            logger.debug(f"Request: {request}")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            request_actions = PrepareSandboxInfraRequestActions.from_request(request)
            reservation_info = AzureReservationInfo.from_resource_context(context)
            cancellation_manager = CancellationContextManager(cancellation_context)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            prepare_sandbox_flow = AzurePrepareSandboxInfraFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                logger=logger,
            )

            return prepare_sandbox_flow.prepare(request_actions=request_actions)
    def GetVmDetails(self, context, requests, cancellation_context):
        """Called when reserving a sandbox during setup.

        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:
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Get VM Details command...")
            logger.debug(f"Requests: {requests}")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            for deploy_app_cls in (
                AzureVMFromMarketplaceDeployedApp,
                AzureVMFromCustomImageDeployedApp,
                AzureVMFromSharedGalleryImageDeployedApp,
            ):
                GetVMDetailsRequestActions.register_deployment_path(deploy_app_cls)

            request_actions = GetVMDetailsRequestActions.from_request(
                request=requests, cs_api=api
            )

            cancellation_manager = CancellationContextManager(cancellation_context)
            reservation_info = AzureReservationInfo.from_resource_context(context)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            vm_details_flow = AzureGetVMDetailsFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                logger=logger,
            )

            return vm_details_flow.get_vm_details(request_actions=request_actions)
    def SetAppSecurityGroups(self, context, request):
        """Called via cloudshell API call.

        Programmatically set which ports will be open on each of the apps
        in the sandbox, and from where they can be accessed. This is an
        optional command that may be implemented. Normally, all outbound
        traffic from a deployed app should be allowed.  For inbound traffic,
        we may use this method to specify the allowed traffic. An app may have
        several networking interfaces in the sandbox. For each such interface,
        this command allows to set which ports may be opened, the protocol and
        the source CIDR. If operation fails, return a "success false" action result.
        :param ResourceCommandContext context:
        :param str request:
        :return:
        :rtype: str
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Set App Security Groups command...")
            logger.debug(f"Request: {request}")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            request_actions = SetAppSecurityGroupsRequestActions.from_request(request)
            reservation_info = AzureReservationInfo.from_resource_context(context)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            app_security_groups_flow = AzureAppSecurityGroupsFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                lock_manager=self.lock_manager,
                logger=logger,
            )

            return app_security_groups_flow.set_app_security_groups(
                request_actions=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
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Cleanup Sandbox Infra command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            request_actions = CleanupSandboxInfraRequestActions.from_request(request)
            reservation_info = AzureReservationInfo.from_resource_context(context)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            cleanup_flow = AzureCleanupSandboxInfraFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                lock_manager=self.lock_manager,
                logger=logger,
            )

            return cleanup_flow.cleanup(request_actions=request_actions)