I'd like to fully automate my resource creation process. Unfortunately brand new subscriptions don't have a few resource providers registered by default. E.g. SQL.
That's easily solved as described here: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-common-deployment-errors#noregisteredproviderfound
However this only outlines methods using either manually using the portal or using Powershell.
I'm looking for a solution using Microsoft.Azure.Management.ResourceManager.Fluent
It seems inconsistent that I wouldn't be able to do it using C#, but I can't seem to be able to find the functionality anywhere.
How to register resource providers for a new subscription using Azure fluent resource management?
How to register resource providers for a new subscription using Azure fluent resource management?
We could use the following function in the Azure fluent resource management to registry providers.
var result=resourceManagementClient.Providers.Register("provider name");
I also do a demo for it.
Preparetion:
Registry Azure Active Directory application and assign Role
Then we can get the tenantId,clientId,clientSecretKey
Steps:
1.Create a C# console project and reference Microsoft.Azure.Management.ResourceManager.Fluent
2.Get accesstoken
public static async Task<string> GetAccessToken(string tenantId, string clientId, string clientSecretKey)
{
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(clientId, clientSecretKey);
var tokenResponse = await context.AcquireTokenAsync("https://management.azure.com/", clientCredential);
var accessToken = tokenResponse.AccessToken;
return accessToken;
}
3. Initialize resourceManagementClient Object
TokenCredentials ccCredentials = new TokenCredentials(GetAccessToken(tenantId, appId, secretKey).Result);
var resourceManagementClient = new ResourceManagementClient(ccCredentials)
{
SubscriptionId = subscriptionId
};
4.Registry the resource provider
We also can list the unregistied providers
var resources = resourceManagementClient.Providers.List().ToList(x => x.RegistrationState.Equals("NotRegistered"));
var result=resourceManagementClient.Providers.Register("provider name");
5.Check it from the Azure portal.
Code:
static string appId = "Registried Azure AD Appliction Id";
static string secretKey = "Client secret Key";
static string tenantId = "tenant Id ";
private static string subscriptionId = "subscription Id ";
public static async Task<string> GetAccessToken(string azureTenantId, string azureAppId, string azureSecretKey)
{
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(appId, secretKey);
var tokenResponse = await context.AcquireTokenAsync("https://management.azure.com/", clientCredential);
var accessToken = tokenResponse.AccessToken;
return accessToken;
}
static void Main(string[] args)
{
TokenCredentials ccCredentials = new TokenCredentials(GetAccessToken(tenantId, appId, secretKey).Result);
var resourceManagementClient = new ResourceManagementClient(ccCredentials)
{
SubscriptionId = subscriptionId
};
var list1 = resourceManagementClient.Providers.List().ToList();
var resource = resourceManagementClient.Providers.List().ToList().FirstOrDefault(x => x.NamespaceProperty.Equals("TrendMicro.DeepSecurity") && x.RegistrationState.Equals("NotRegistered"));
var registry =resourceManagementClient.Providers.Register(resource?.NamespaceProperty);
}
Packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.Management.ResourceManager.Fluent" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.5" targetFramework="net452" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.5" targetFramework="net452" />
<package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.2.12" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
</packages>
Provider Auto registration feature is added in the upcoming release of Azure Management SDK (Fluent) v. 1.1.