From eff0c2f7fb80019f964ef525d1c37f680bc95dad Mon Sep 17 00:00:00 2001 From: Hovsep Mkrtchyan Date: Mon, 15 Dec 2014 15:05:26 -0800 Subject: [PATCH] Bringing back provider registration during template deployment --- .../Mocks/MockClientFactory.cs | 5 +++ .../Common/AzurePowerShellClientFactory.cs | 36 +++++++++++++++++++ .../Factories/ClientFactory.cs | 4 ++- .../Interfaces/IClientFactory.cs | 3 ++ .../ResourceClient.ResourceManager.cs | 6 +++- 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Common/Commands.Common.Test/Mocks/MockClientFactory.cs b/src/Common/Commands.Common.Test/Mocks/MockClientFactory.cs index 12ede8e14c8d..3df32371002b 100644 --- a/src/Common/Commands.Common.Test/Mocks/MockClientFactory.cs +++ b/src/Common/Commands.Common.Test/Mocks/MockClientFactory.cs @@ -102,5 +102,10 @@ public override HttpClient CreateHttpClient(string serviceUrl, HttpMessageHandle return client; } + + public override void RegisterCustomProviders(IEnumerable providers) + { + throw new NotImplementedException(); + } } } diff --git a/src/Common/Commands.Common/Common/AzurePowerShellClientFactory.cs b/src/Common/Commands.Common/Common/AzurePowerShellClientFactory.cs index 4f0c95b568d9..96bb62d75f47 100644 --- a/src/Common/Commands.Common/Common/AzurePowerShellClientFactory.cs +++ b/src/Common/Commands.Common/Common/AzurePowerShellClientFactory.cs @@ -21,6 +21,7 @@ using System.Linq; using System.Collections.Generic; using Microsoft.WindowsAzure.Management; +using Microsoft.Azure.Management.Resources.Models; namespace Microsoft.WindowsAzure.Commands.Common.Common { @@ -44,6 +45,41 @@ public override TClient CreateClient(AzureContext context, AzureEnviron return client; } + + public override void RegisterCustomProviders(IEnumerable providers) + { + var context = AzureSession.CurrentContext; + var registeredProviders = context.Subscription.GetPropertyAsArray( + AzureSubscription.Property.RegisteredResourceProviders); + var successfullyRegisteredProvider = new List(); + var creds = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(context); + + var requiredProviders = providers.Where(p => !registeredProviders.Contains(p)) + .ToList(); + + if (requiredProviders.Count > 0) + { + using (var client = AzureSession.ClientFactory.CreateCustomClient( + creds, + context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) + { + foreach (var provider in requiredProviders) + { + try + { + client.Providers.Register(provider); + successfullyRegisteredProvider.Add(provider); + } + catch + { + // Ignore this as the user may not have access to Sparta endpoint or the provider is already registered + } + } + } + + UpdateSubscriptionRegisteredProviders(context.Subscription, successfullyRegisteredProvider); + } + } /// /// Registers resource providers for Sparta. diff --git a/src/Common/Commands.Common/Factories/ClientFactory.cs b/src/Common/Commands.Common/Factories/ClientFactory.cs index f5aca60d816d..5244fa8d74aa 100644 --- a/src/Common/Commands.Common/Factories/ClientFactory.cs +++ b/src/Common/Commands.Common/Factories/ClientFactory.cs @@ -23,7 +23,7 @@ namespace Microsoft.WindowsAzure.Commands.Common.Factories { - public class ClientFactory : IClientFactory + public abstract class ClientFactory : IClientFactory { private static readonly char[] uriPathSeparator = { '/' }; @@ -136,5 +136,7 @@ public static HttpClientHandler CreateHttpClientHandler(string endpoint, ICreden // Our handler is ready return clientHandler; } + + public abstract void RegisterCustomProviders(IEnumerable providers); } } diff --git a/src/Common/Commands.Common/Interfaces/IClientFactory.cs b/src/Common/Commands.Common/Interfaces/IClientFactory.cs index 00aad993a9a9..1ff0ca34d7bc 100644 --- a/src/Common/Commands.Common/Interfaces/IClientFactory.cs +++ b/src/Common/Commands.Common/Interfaces/IClientFactory.cs @@ -17,6 +17,7 @@ using Microsoft.WindowsAzure.Commands.Common.Models; using Microsoft.WindowsAzure.Common; using System; +using System.Collections.Generic; namespace Microsoft.WindowsAzure.Commands.Common { @@ -31,5 +32,7 @@ public interface IClientFactory HttpClient CreateHttpClient(string endpoint, ICredentials credentials); HttpClient CreateHttpClient(string endpoint, HttpMessageHandler effectiveHandler); + + void RegisterCustomProviders(IEnumerable providers); } } diff --git a/src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourceClient.ResourceManager.cs b/src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourceClient.ResourceManager.cs index 999112c845b5..515853ec3f8d 100644 --- a/src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourceClient.ResourceManager.cs +++ b/src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourceClient.ResourceManager.cs @@ -22,6 +22,9 @@ using Microsoft.Azure.Management.Resources.Models; using Microsoft.WindowsAzure; using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources; +using Microsoft.WindowsAzure.Commands.Common.Factories; +using Microsoft.WindowsAzure.Commands.Common; +using Microsoft.WindowsAzure.Commands.Common.Models; namespace Microsoft.Azure.Commands.Resources.Models { @@ -321,13 +324,14 @@ public virtual PSResourceGroupDeployment ExecuteDeployment(CreatePSResourceGroup WriteWarning("The StorageAccountName parameter is no longer used and will be removed in a future release. Please update scripts to remove this parameter."); } + AzureSession.ClientFactory.RegisterCustomProviders(validationInfo.RequiredProviders.Select(p => p.Namespace.ToLower())); ResourceManagementClient.Deployments.CreateOrUpdate(parameters.ResourceGroupName, parameters.DeploymentName, deployment); WriteVerbose(string.Format("Create template deployment '{0}'.", parameters.DeploymentName)); Deployment result = ProvisionDeploymentStatus(parameters.ResourceGroupName, parameters.DeploymentName, deployment); return result.ToPSResourceGroupDeployment(parameters.ResourceGroupName); } - + private string GenerateDeploymentName(CreatePSResourceGroupDeploymentParameters parameters) { if (!string.IsNullOrEmpty(parameters.DeploymentName))