0

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"

  • Shouldn’t you be querying the values in `resourcesInResourceGroup` to get the consumption for each? (I did this years ago with the web API, must have that code somewhere) – stuartd Mar 21 '23 at 20:17
  • For the error message, see [here](https://stackoverflow.com/a/66855830/43846) – stuartd Mar 21 '23 at 20:18
  • @stuartd: Thanks for the hints! I have no Extention Method for the class ResourceGroupResource, that would give access to cost information. However I found an Extention method, which uses ManagementGroups. I have updated the Question with an updated try to gain access, but this gave me another strange error message. As to the subscription type, I have to clear that problem tomorrow morning. – Jan Bahlmann Mar 21 '23 at 21:27
  • It appears, the subscription is on **Microsoft Azure Plan for Dev/Test**, which should support the Cost Management as stated [here](https://learn.microsoft.com/en-us/azure/cost-management-billing/costs/understand-cost-mgt-data#supported-microsoft-azure-offers) – Jan Bahlmann Mar 22 '23 at 11:30

0 Answers0