Esempio n. 1
0
        public async Task <ExecutionResult> ConnectAsync(IChannel channel,
                                                         string subscriptionId,
                                                         string resourceGroupName,
                                                         string workspaceName,
                                                         string storageAccountConnectionString,
                                                         string location,
                                                         bool refreshCredentials = false)
        {
            var connectionResult = await ConnectToWorkspaceAsync(channel, subscriptionId, resourceGroupName, workspaceName, location, refreshCredentials);

            if (connectionResult.Status != ExecuteStatus.Ok)
            {
                return(connectionResult);
            }

            if (ActiveWorkspace == null)
            {
                return(AzureClientError.WorkspaceNotFound.ToExecutionResult());
            }

            ConnectionString = storageAccountConnectionString;
            ActiveTarget     = null;
            MostRecentJobId  = string.Empty;

            channel.Stdout($"Connected to Azure Quantum workspace {ActiveWorkspace.Name} in location {ActiveWorkspace.Location}.");

            if (ValidExecutionTargets.Count() == 0)
            {
                channel.Stderr($"No valid Q# execution targets found in Azure Quantum workspace {ActiveWorkspace.Name}.");
            }

            return(ValidExecutionTargets.ToExecutionResult());
        }
Esempio n. 2
0
        public async Task <ExecutionResult> GetConnectionStatusAsync(IChannel channel)
        {
            if (ActiveWorkspace == null || AvailableProviders == null)
            {
                return(AzureClientError.NotConnected.ToExecutionResult());
            }

            channel.Stdout($"Connected to Azure Quantum workspace {ActiveWorkspace.Name}.");

            return(ValidExecutionTargets.ToExecutionResult());
        }
Esempio n. 3
0
        public async Task <ExecutionResult> ConnectAsync(IChannel channel,
                                                         string subscriptionId,
                                                         string resourceGroupName,
                                                         string workspaceName,
                                                         string storageAccountConnectionString,
                                                         bool refreshCredentials = false)
        {
            var             azureEnvironment = AzureEnvironment.Create(subscriptionId);
            IAzureWorkspace?workspace        = null;

            try
            {
                workspace = await azureEnvironment.GetAuthenticatedWorkspaceAsync(channel, resourceGroupName, workspaceName, refreshCredentials);
            }
            catch (Exception e)
            {
                channel.Stderr($"The connection to the Azure Quantum workspace could not be completed. Please check the provided parameters and try again.");
                channel.Stderr($"Error details: {e.Message}");
                return(AzureClientError.WorkspaceNotFound.ToExecutionResult());
            }

            if (workspace == null)
            {
                return(AzureClientError.AuthenticationFailed.ToExecutionResult());
            }

            var providers = await workspace.GetProvidersAsync();

            if (providers == null)
            {
                return(AzureClientError.WorkspaceNotFound.ToExecutionResult());
            }

            ActiveWorkspace    = workspace;
            AvailableProviders = providers;
            ConnectionString   = storageAccountConnectionString;
            ActiveTarget       = null;
            MostRecentJobId    = string.Empty;

            channel.Stdout($"Connected to Azure Quantum workspace {ActiveWorkspace.Name}.");

            if (ValidExecutionTargets.Count() == 0)
            {
                channel.Stderr($"No valid Q# execution targets found in Azure Quantum workspace {ActiveWorkspace.Name}.");
            }

            return(ValidExecutionTargets.ToExecutionResult());
        }
Esempio n. 4
0
        public async Task <ExecutionResult> GetConnectionStatusAsync(IChannel channel)
        {
            if (ActiveWorkspace == null || AvailableProviders == null)
            {
                return(AzureClientError.NotConnected.ToExecutionResult());
            }

            var connectionResult = await RefreshConnectionAsync(channel);

            if (connectionResult.Status != ExecuteStatus.Ok)
            {
                return(connectionResult);
            }

            channel.Stdout($"Connected to Azure Quantum workspace {ActiveWorkspace.Name} in location {ActiveWorkspace.Location}.");

            return(ValidExecutionTargets.ToExecutionResult());
        }
Esempio n. 5
0
        public async Task <ExecutionResult> ConnectAsync(IChannel channel,
                                                         string subscriptionId,
                                                         string resourceGroupName,
                                                         string workspaceName,
                                                         string storageAccountConnectionString,
                                                         string location,
                                                         CredentialType credentialType,
                                                         CancellationToken?cancellationToken = null)
        {
            var             duration = Stopwatch.StartNew();
            ExecutionResult?result   = null;

            try
            {
                // Capture the console output, specifically for the case the user is trying to use DeviceCode credentials
                // so they can get the message for auth.
                var currentOut = channel?.CaptureConsole();
                try
                {
                    var credential = CredentialFactory.CreateCredential(credentialType, subscriptionId);

                    var connectionResult = await ConnectToWorkspaceAsync(channel, subscriptionId, resourceGroupName, workspaceName, location, credential);

                    if (connectionResult.Status != ExecuteStatus.Ok)
                    {
                        result = connectionResult;
                        return(result.Value);
                    }

                    if (ActiveWorkspace == null)
                    {
                        result = AzureClientError.WorkspaceNotFound.ToExecutionResult();
                        return(result.Value);
                    }

                    Credential = credential;
                }
                finally
                {
                    System.Console.SetOut(currentOut);
                }

                StorageConnectionString = storageAccountConnectionString;
                ActiveTarget            = null;
                MostRecentJobId         = string.Empty;

                channel?.Stdout($"Connected to Azure Quantum workspace {ActiveWorkspace.WorkspaceName} in location {ActiveWorkspace.Location}.");

                if (ValidExecutionTargets.Count() == 0)
                {
                    channel?.Stderr($"No valid quantum computing execution targets found in Azure Quantum workspace {ActiveWorkspace.WorkspaceName}.");
                }

                result = ValidExecutionTargets.ToExecutionResult();
                return(result.Value);
            }
            finally
            {
                duration.Stop();

                ExecuteStatus    status           = result?.Status ?? ExecuteStatus.Error;
                AzureClientError?error            = result?.Output as AzureClientError?;
                bool             useCustomStorage = !string.IsNullOrWhiteSpace(StorageConnectionString);

                ConnectToWorkspace?.Invoke(this, new ConnectToWorkspaceEventArgs(status, error, location, useCustomStorage, credentialType, duration.Elapsed));
            }
        }