This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Add remaining AzureClient magic commands #150
Merged
rmshaffer
merged 8 commits into
feature/azure-quantum-preview
from
rmshaffer/azure-jobs
May 28, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
94e26eb
Add more magics and clean up IAzureClient
rmshaffer 9a12114
Update magic behaviors, add dogfood support
rmshaffer cc28124
Minor documentation fix
rmshaffer a166c21
Update tests to match new functionality
rmshaffer e3063bb
Clean up AzureEnvironment class
rmshaffer b40fd13
Work around syntax highlighting error
rmshaffer b95a7ab
Improve exception handling and documentation
rmshaffer 4dbb37e
Use Enum.TryParse() in AzureEnvironment.Create()
rmshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using Microsoft.Quantum.Simulation.Common; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Text; | ||
|
|
||
| namespace Microsoft.Quantum.IQSharp.AzureClient | ||
| { | ||
| internal enum AzureEnvironmentType { Production, Canary, Dogfood }; | ||
|
|
||
| internal class AzureEnvironment | ||
| { | ||
| public string ClientId { get; private set; } = string.Empty; | ||
| public string Authority { get; private set; } = string.Empty; | ||
| public List<string> Scopes { get; private set; } = new List<string>(); | ||
| public Uri? BaseUri { get; private set; } | ||
|
|
||
| private AzureEnvironment() | ||
| { | ||
| } | ||
|
|
||
| public static AzureEnvironment Create(string environment, string subscriptionId) | ||
| { | ||
| if (Enum.TryParse(environment, true, out AzureEnvironmentType environmentType)) | ||
| { | ||
| switch (environmentType) | ||
| { | ||
| case AzureEnvironmentType.Production: | ||
| return Production(); | ||
| case AzureEnvironmentType.Canary: | ||
| return Canary(); | ||
| case AzureEnvironmentType.Dogfood: | ||
| return Dogfood(subscriptionId); | ||
| default: | ||
| throw new InvalidOperationException("Unexpected EnvironmentType value."); | ||
| } | ||
| } | ||
|
|
||
| return Production(); | ||
| } | ||
|
|
||
| private static AzureEnvironment Production() => | ||
| new AzureEnvironment() | ||
| { | ||
| ClientId = "84ba0947-6c53-4dd2-9ca9-b3694761521b", // QDK client ID | ||
| Authority = "https://login.microsoftonline.com/common", | ||
| Scopes = new List<string>() { "https://quantum.microsoft.com/Jobs.ReadWrite" }, | ||
| BaseUri = new Uri("https://app-jobscheduler-prod.azurewebsites.net/"), | ||
| }; | ||
|
|
||
| private static AzureEnvironment Dogfood(string subscriptionId) => | ||
| new AzureEnvironment() | ||
| { | ||
| ClientId = "46a998aa-43d0-4281-9cbb-5709a507ac36", // QDK dogfood client ID | ||
| Authority = GetDogfoodAuthority(subscriptionId), | ||
| Scopes = new List<string>() { "api://dogfood.azure-quantum/Jobs.ReadWrite" }, | ||
| BaseUri = new Uri("https://app-jobscheduler-test.azurewebsites.net/"), | ||
| }; | ||
|
|
||
| private static AzureEnvironment Canary() | ||
| { | ||
| var canary = Production(); | ||
| canary.BaseUri = new Uri("https://app-jobs-canarysouthcentralus.azurewebsites.net/"); | ||
| return canary; | ||
| } | ||
|
|
||
| private static string GetDogfoodAuthority(string subscriptionId) | ||
| { | ||
| try | ||
| { | ||
| var armBaseUrl = "https://api-dogfood.resources.windows-int.net"; | ||
| var requestUrl = $"{armBaseUrl}/subscriptions/{subscriptionId}?api-version=2018-01-01"; | ||
|
|
||
| WebResponse? response = null; | ||
| try | ||
| { | ||
| response = WebRequest.Create(requestUrl).GetResponse(); | ||
| } | ||
| catch (WebException webException) | ||
| { | ||
| response = webException.Response; | ||
| } | ||
|
|
||
| var authHeader = response.Headers["WWW-Authenticate"]; | ||
| var headerParts = authHeader.Substring("Bearer ".Length).Split(','); | ||
| foreach (var headerPart in headerParts) | ||
rmshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var parts = headerPart.Split("=", 2); | ||
| if (parts[0] == "authorization_uri") | ||
| { | ||
| var quotedAuthority = parts[1]; | ||
| return quotedAuthority[1..^1]; | ||
rmshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| throw new InvalidOperationException($"Dogfood authority not found in ARM header response for subscription ID {subscriptionId}."); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| throw new InvalidOperationException($"Failed to construct dogfood authority for subscription ID {subscriptionId}.", ex); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.