Allow the arm baseURI to be set dynamically (#920)

* Allow the arm baseURI to be set dynamically

* Defensive programming
This commit is contained in:
Amir Omidi
2020-03-23 14:41:45 -07:00
committed by GitHub
parent 6920c34570
commit 50a666f794
5 changed files with 111 additions and 21 deletions

View File

@@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.SqlTools.ResourceProvider.Core.Contracts;
namespace Microsoft.SqlTools.ResourceProvider.Core.Authentication namespace Microsoft.SqlTools.ResourceProvider.Core.Authentication
{ {
@@ -12,6 +13,11 @@ namespace Microsoft.SqlTools.ResourceProvider.Core.Authentication
/// </summary> /// </summary>
public interface IAzureUserAccount : IEquatable<IAzureUserAccount>, IUserAccount public interface IAzureUserAccount : IEquatable<IAzureUserAccount>, IUserAccount
{ {
Account UnderlyingAccount
{
get;
set;
}
/// <summary> /// <summary>
/// User Account Display Info /// User Account Display Info
/// </summary> /// </summary>

View File

@@ -58,9 +58,57 @@ namespace Microsoft.SqlTools.ResourceProvider.Core.Contracts
get; get;
set; set;
} }
/// <summary>
/// Information about the auth provider
/// </summary>
public ProviderSettings ProviderSettings;
} }
public class ProviderSettings
{
/// <summary>
/// Display name of the provider
/// </summary>
public string DisplayName;
/// <summary>
/// ID of the provider
/// </summary>
public string Id;
/// <summary>
/// Settings for the provider itself
/// </summary>
public ProviderSettingsObject Settings;
}
public class ProviderSettingsObject
{
public ResourceSetting ArmResource;
public ResourceSetting GraphResource;
public ResourceSetting OssRdbmsResource;
public ResourceSetting SqlResource;
/// <summary>
/// Actual sign in link
/// </summary>
public string Host;
/// <summary>
/// ClientID used
/// </summary>
public string ClientId;
}
public class ResourceSetting
{
/// <summary>
/// Endpoint of the resource
/// </summary>
public string Endpoint;
public string Id;
}
/// <summary> /// <summary>
/// Represents a key that identifies an account. /// Represents a key that identifies an account.
/// </summary> /// </summary>
@@ -87,7 +135,6 @@ namespace Microsoft.SqlTools.ResourceProvider.Core.Contracts
/// <summary> /// <summary>
/// A display name that offers context for the account, such as "Contoso". /// A display name that offers context for the account, such as "Contoso".
/// </summary> /// </summary>
public string ContextualDisplayName { get; set; } public string ContextualDisplayName { get; set; }
// Note: ignoring ContextualLogo as it's not needed // Note: ignoring ContextualLogo as it's not needed

View File

@@ -97,6 +97,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
userAccount.DisplayInfo = ToDisplayInfo(account); userAccount.DisplayInfo = ToDisplayInfo(account);
userAccount.NeedsReauthentication = account.IsStale; userAccount.NeedsReauthentication = account.IsStale;
userAccount.AllTenants = ProcessTenants(accountTokenWrapper, account); userAccount.AllTenants = ProcessTenants(accountTokenWrapper, account);
userAccount.UnderlyingAccount = account;
return userAccount; return userAccount;
} }

View File

@@ -55,12 +55,26 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
CommonUtil.CheckForNull(subscriptionContext, "subscriptionContext"); CommonUtil.CheckForNull(subscriptionContext, "subscriptionContext");
try try
{ {
string armEndpoint = subscriptionContext.UserAccount.UnderlyingAccount.Properties.ProviderSettings?.Settings?.ArmResource?.Endpoint;
Uri armUri = null;
if (armEndpoint != null)
{
try
{
armUri = new Uri(armEndpoint);
}
catch (Exception e)
{
Console.WriteLine($"Exception while parsing URI: {e.Message}");
}
}
ServiceClientCredentials credentials = CreateCredentials(subscriptionContext); ServiceClientCredentials credentials = CreateCredentials(subscriptionContext);
SqlManagementClient sqlManagementClient = new SqlManagementClient(credentials) SqlManagementClient sqlManagementClient = new SqlManagementClient(armUri ?? _resourceManagementUri, credentials)
{ {
SubscriptionId = subscriptionContext.Subscription.SubscriptionId SubscriptionId = subscriptionContext.Subscription.SubscriptionId
}; };
ResourceManagementClient resourceManagementClient = new ResourceManagementClient(credentials)
ResourceManagementClient resourceManagementClient = new ResourceManagementClient(armUri ?? _resourceManagementUri, credentials)
{ {
SubscriptionId = subscriptionContext.Subscription.SubscriptionId SubscriptionId = subscriptionContext.Subscription.SubscriptionId
}; };
@@ -128,7 +142,8 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
SR.FailedToGetAzureSqlServersWithError); SR.FailedToGetAzureSqlServersWithError);
if (servers != null) if (servers != null)
{ {
sqlServers.AddRange(servers.Select(server => { sqlServers.AddRange(servers.Select(server =>
{
var serverResource = new SqlAzureResource(server); var serverResource = new SqlAzureResource(server);
// TODO ResourceGroup name // TODO ResourceGroup name
return serverResource; return serverResource;
@@ -239,12 +254,25 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
if (azureTenant != null) if (azureTenant != null)
{ {
ServiceClientCredentials credentials = CreateCredentials(azureTenant); ServiceClientCredentials credentials = CreateCredentials(azureTenant);
using (SubscriptionClient client = new SubscriptionClient(_resourceManagementUri, credentials)) string armEndpoint = userAccount.UnderlyingAccount.Properties.ProviderSettings?.Settings?.ArmResource?.Endpoint;
Uri armUri = null;
if (armEndpoint != null)
{
try
{
armUri = new Uri(armEndpoint);
}
catch (Exception e)
{
Console.WriteLine($"Exception while parsing URI: {e.Message}");
}
}
using (SubscriptionClient client = new SubscriptionClient(armUri ?? _resourceManagementUri, credentials))
{ {
IEnumerable<Subscription> subs = await GetSubscriptionsAsync(client); IEnumerable<Subscription> subs = await GetSubscriptionsAsync(client);
return new ServiceResponse<IAzureUserAccountSubscriptionContext>(subs.Select(sub => return new ServiceResponse<IAzureUserAccountSubscriptionContext>(subs.Select(sub =>
{ {
AzureSubscriptionIdentifier subId = new AzureSubscriptionIdentifier(userAccount, azureTenant.TenantId, sub.SubscriptionId, _resourceManagementUri); AzureSubscriptionIdentifier subId = new AzureSubscriptionIdentifier(userAccount, azureTenant.TenantId, sub.SubscriptionId, armUri ?? _resourceManagementUri);
AzureUserAccountSubscriptionContext context = new AzureUserAccountSubscriptionContext(subId, credentials); AzureUserAccountSubscriptionContext context = new AzureUserAccountSubscriptionContext(subId, credentials);
return context; return context;
})); }));

View File

@@ -6,6 +6,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.SqlTools.ResourceProvider.Core; using Microsoft.SqlTools.ResourceProvider.Core;
using Microsoft.SqlTools.ResourceProvider.Core.Authentication; using Microsoft.SqlTools.ResourceProvider.Core.Authentication;
using Microsoft.SqlTools.ResourceProvider.Core.Contracts;
namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{ {
@@ -39,6 +40,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
this.TenantId = azureUserAccount.TenantId; this.TenantId = azureUserAccount.TenantId;
this.AllTenants = azureUserAccount.AllTenants; this.AllTenants = azureUserAccount.AllTenants;
this.UniqueId = azureUserAccount.UniqueId; this.UniqueId = azureUserAccount.UniqueId;
this.UnderlyingAccount = azureUserAccount.UnderlyingAccount;
AzureUserAccount account = azureUserAccount as AzureUserAccount; AzureUserAccount account = azureUserAccount as AzureUserAccount;
} }
/// <summary> /// <summary>
@@ -99,5 +101,11 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
get; get;
set; set;
} }
public Account UnderlyingAccount
{
get;
set;
}
} }
} }