Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge]: Bringing back provider registration during template deployment #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Common/Commands.Common.Test/Mocks/MockClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,10 @@ public override HttpClient CreateHttpClient(string serviceUrl, HttpMessageHandle

return client;
}

public override void RegisterCustomProviders(IEnumerable<string> providers)
{
throw new NotImplementedException();
}
}
}
36 changes: 36 additions & 0 deletions src/Common/Commands.Common/Common/AzurePowerShellClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -44,6 +45,41 @@ public override TClient CreateClient<TClient>(AzureContext context, AzureEnviron

return client;
}

public override void RegisterCustomProviders(IEnumerable<string> providers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should be in ResourcesClient.cs it has nothing to do with client creation.

{
var context = AzureSession.CurrentContext;
var registeredProviders = context.Subscription.GetPropertyAsArray(
AzureSubscription.Property.RegisteredResourceProviders);
var successfullyRegisteredProvider = new List<string>();
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<ResourceManagementClient>(
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);
}
}

/// <summary>
/// Registers resource providers for Sparta.
Expand Down
4 changes: 3 additions & 1 deletion src/Common/Commands.Common/Factories/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace Microsoft.WindowsAzure.Commands.Common.Factories
{
public class ClientFactory : IClientFactory
public abstract class ClientFactory : IClientFactory
{
private static readonly char[] uriPathSeparator = { '/' };

Expand Down Expand Up @@ -136,5 +136,7 @@ public static HttpClientHandler CreateHttpClientHandler(string endpoint, ICreden
// Our handler is ready
return clientHandler;
}

public abstract void RegisterCustomProviders(IEnumerable<string> providers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be in this interface

}
}
3 changes: 3 additions & 0 deletions src/Common/Commands.Common/Interfaces/IClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -31,5 +32,7 @@ public interface IClientFactory
HttpClient CreateHttpClient(string endpoint, ICredentials credentials);

HttpClient CreateHttpClient(string endpoint, HttpMessageHandler effectiveHandler);

void RegisterCustomProviders(IEnumerable<string> providers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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))
Expand Down