There’s a pretty comprehensive set of APIs available for interacting with Azure resources but unfortunately these seem to be so new that there’s not really much documentation around on how to use them.

For an example of the information that can be returned by these APIs you can use the Azure Resource Explorer preview to explore the resources that are available to you.

There’s also a .NET library provided by Microsoft to make interacting with these APIs easier, this can be used via the following NuGet packages.

Microsoft.Azure.Management.Fluent
Microsoft.Azure.Management.ResourceManager.Fluent

The following code retrieves details of all web apps in all subscriptions belonging to a given tenant. This initially gets the web app and then gets the configuration for it and maps it to the web app.

public async Task GetWebAppDetails()
{
	string clientId = "clientId";
	string clientSecret = "clientSecret";
	string tenantId = "tenantId";
	string subscriptionId = "subscriptionId";

	AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
		clientId, 
		clientSecret, 
		tenantId, 
		AzureEnvironment.AzureGlobalCloud).WithDefaultSubscription(subscriptionId);

	RestClient restClient = RestClient
		.Configure()
		.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
		.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
		.WithCredentials(credentials)
		.Build();

	var creds = new AzureCredentialsFactory().FromServicePrincipal(
		clientId, 
		clientSecret,
		tenantId, 
		AzureEnvironment.AzureGlobalCloud
		);

	var azure = Azure.Authenticate(creds);

	// Get all subscriptions in tenant
	foreach (var subscription in await azure.Subscriptions.ListAsync())
	{
		// Get all resource groups in subscription
		ResourceManagementClient resourceManagementClient = new ResourceManagementClient(restClient);
		resourceManagementClient.SubscriptionId = subscription.SubscriptionId;

		foreach (var resourceGroup in await resourceManagementClient.ResourceGroups.ListAsync())
		{
			// Get web app details
			WebSiteManagementClient webSiteManagementClient = new WebSiteManagementClient(restClient);
			webSiteManagementClient.SubscriptionId = subscription.SubscriptionId;

			foreach (var webApp in await azure.WithSubscription(subscription.SubscriptionId).WebApps.ListByResourceGroupAsync(resourceGroup.Name))
			{
				SiteConfigResourceInner siteConfigResourceInner = await webSiteManagementClient.WebApps.GetConfigurationAsync(resourceGroup.Name, webApp.Name);
				webApp.Inner.SiteConfig = new SiteConfig();

				foreach (PropertyInfo propertyInfo in webApp.Inner.SiteConfig.GetType().GetProperties())
				{
					var value = siteConfigResourceInner.GetType().GetProperty(propertyInfo.Name).GetValue(siteConfigResourceInner, null);
					propertyInfo.SetValue(webApp.Inner.SiteConfig, siteConfigResourceInner.GetType().GetProperty(propertyInfo.Name).GetValue(siteConfigResourceInner));
				}
			}
		}
	}
}

Strangely, the SiteConfig object returned as part of the IWebApp object by the management API is always null so if you want to retrieve any application settings such as minimum TLS version then you need to make a separate call to get the configuration. This configuration is of type SiteConfigResourceInner rather than SiteConfig although they appear to be identical, hence the foreach loop in the code above to match properties from the SiteConfigResourceInner to the SiteConfig.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *