Jan Bahlmann
Jan Bahlmann

Reputation: 1

How to query our Azure consumption programmatically?

I am trying to access our Azure Consumption Data programmatically. I did this before, but I want to use the newest libraries: Azure.Identity(1.8.2), Azure.ResourceManager (1.4.0) and Azure.ResourceManager.Consumption (1.0.1).

I am unsure, how to use them. I can query all our Resources, but when I am trying to get Informations about Consumption, I do not see any dedicated methods, like GetConsumption(). All I see is subscription.GetPriceSheet(), but this gives me a strange error, complaining about an old API version, though I am indeed on the newest nugets.

I searched the documentation, but information, on how to archive this, is sparse or I didn't look in the right place.

I extracted the relevant code into a test program and was able to query all Resources in our subscription (it's on Microsoft Azure Plan for Dev/Test, which is supported as stated here).

ClientSecretCredential cred = new ClientSecretCredential(
               tenantId,
               clientId,
               clientSecret);
var armClient = new ArmClient(cred);

TenantCollection tenants = armClient.GetTenants();
SubscriptionResource subscription = armClient.GetSubscriptionResource(new Azure.Core.ResourceIdentifier($"/subscriptions/{subscriptionId}"));
ResourceGroupResource resourceGroup = subscription.GetResourceGroup(resourceGroupName);

// check, that we have access to azure
var resourcesInSubscription = subscription.GetGenericResources();
int amountResourcesInSubscription= resourcesInSubscription.Count();
var resourcesInResourceGroup = resourceGroup.GetGenericResources();
int amountResourcesInResourceGroup = resourcesInSubscription.Count();

The only method, I could find, that has anything to do with consumption is

// now try to get some information about consumption/pricing
var prices=subscription.GetPriceSheet();

But this gives a strange error

Cost management data is not supported for subscription(s) ---- in the provided api-version. Please use api-version 2019-10-01 or later. (Request ID: ----) Status: 404 (Not Found) ErrorCode: 404

I have the feeling, that the Api-Version is not really the problem, as the nuget libraries are all published at least after February 8 2023. Might I miss some Api-Permissions for my Service Principal or some Roles, I have to give it in my Subscription, after all it is a newer subscription and service principal?

Help on that problem is greatly appreaciated!

UPDATE I also tried the following:

var managementGroups = armClient.GetManagementGroups();
var cost=managementGroups.First().GetAggregatedCost();

This gives another error: The client '----' with object id '----' does not have authorization to perform action 'Microsoft.Management/managementGroups/read' over scope '/providers/Microsoft.Management' or the scope is invalid. If access was recently granted, please refresh your credentials. Status: 403 (Forbidden) ErrorCode: AuthorizationFailed

Do I need access to Management Groups? And if, how, as the Service Principal has Owner-Rights on the Subscription, as well as the roles "Management Group Reader" and "Cost Management Contributor"

Upvotes: 0

Views: 528

Answers (0)

Related Questions