def GetAccessKey(self, context, ports):
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Get Access Key command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_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,
            )

            access_key_flow = AzureGetAccessKeyFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                logger=logger,
            )

            return access_key_flow.get_access_key()
    def GetAvailablePrivateIP(self, context, subnet_cidr, owner):
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Get Available Private IP command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_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,
            )

            get_available_ip_flow = AzureGetAvailablePrivateIPFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                cs_ip_pool_manager=cs_ip_pool_manager,
                reservation_info=reservation_info,
                logger=logger,
            )

            return get_available_ip_flow.get_available_private_ip(
                subnet_cidr=subnet_cidr, owner=owner
            )
    def reconfigure_vm(
        self,
        context,
        ports,
        cancellation_context,
        vm_size,
        os_disk_size,
        os_disk_type,
        data_disks,
    ):
        """Reconfigure VM Size and Data Disks."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Reconfigure VM command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_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,
            )

            for deployed_app_cls in (
                AzureVMFromMarketplaceDeployedApp,
                AzureVMFromCustomImageDeployedApp,
                AzureVMFromSharedGalleryImageDeployedApp,
            ):
                DeployedVMActions.register_deployment_path(deployed_app_cls)

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            reconfigure_vm_flow = AzureReconfigureVMFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                cs_api=api,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                logger=logger,
            )

            return reconfigure_vm_flow.reconfigure(
                deployed_app=deployed_vm_actions.deployed_app,
                vm_size=vm_size,
                os_disk_size=os_disk_size,
                os_disk_type=os_disk_type,
                data_disks=data_disks,
            )
    def remote_refresh_ip(self, context, ports, 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 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:
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Remote Refresh IP command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_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,
            )

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

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            refresh_ip_flow = AzureRefreshIPFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                cs_api=api,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                logger=logger,
            )

            return refresh_ip_flow.refresh_ip(
                deployed_app=deployed_vm_actions.deployed_app
            )
    def DeleteInstance(self, context, ports):
        """Called 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:
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Delete Instance command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_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 (
                AzureVMFromMarketplaceDeployedApp,
                AzureVMFromCustomImageDeployedApp,
                AzureVMFromSharedGalleryImageDeployedApp,
            ):
                GetVMDetailsRequestActions.register_deployment_path(deploy_app_cls)

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            delete_flow = AzureDeleteInstanceFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                cs_ip_pool_manager=cs_ip_pool_manager,
                lock_manager=self.lock_manager,
                logger=logger,
            )

            delete_flow.delete_instance(deployed_app=deployed_vm_actions.deployed_app)
    def PowerOff(self, context, ports):
        """Called during sandbox's teardown.

        Can also be run manually by the sandbox end-user from the deployed
        App's commands pane. Method shuts down (or powers off) the VM instance.
        If the operation fails, method should raise an exception.
        :param ResourceRemoteCommandContext context:
        :param ports:
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Power Off command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_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,
            )

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

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            power_mgmt_flow = AzurePowerManagementFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                logger=logger,
            )

            return power_mgmt_flow.power_off(
                deployed_app=deployed_vm_actions.deployed_app
            )
    def GetApplicationPorts(self, context, ports):
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Get Application Ports command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_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,
            )

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

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            application_ports_flow = AzureGetApplicationPortsFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                logger=logger,
            )

            return application_ports_flow.get_application_ports(
                deployed_app=deployed_vm_actions.deployed_app
            )