I am trying to get a list of SQL servers and SQL databases using the SqlManagementClient from .NET SDK. I have the following requirement:
List the number of SQL servers in a single Azure Subscription
List the number of SQL Databases in each of the SQL Servers listed above
In order to do so, we are authenticating using service principal. Following is the snippet of the function that we are using to achieve this.
var azureCredentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
Constants.ApplicationId,
Constants.ClientSecret,
Constants.TenantId,
AzureEnvironment.AzureGlobalCloud)
.WithDefaultSubscription(Constants.SubscriptionId);
RestClient restClient = RestClient.Configure()
.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithCredentials(azureCredentials)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Build();
SqlManagementClient sqlManagementClient = new SqlManagementClient(restClient);
sqlManagementClient.SubscriptionId = Constants.SubscriptionId;
var response = await SubscriptionUsagesOperationsExtensions.ListByLocationAsync(sqlManagementClient.SubscriptionUsages, Constants.Location);
Now the problem part. There are a number of SQL servers that are active under the subscription. Although the above returns an empty list. However if we use a bearer token and list the servers using the same REST API that the above method uses it returns valid results and accurate ones as well. It would be really helpful if you could point out what the issue is with regards to the above example.
