1

I am trying to use the Microsoft.Azure.Management.Consumption 3.0.2 package to access the usage and consumption data.

However in the call to UsageDetails.List I get the following error:

Subscription scope usage is not supported for current api version. Please use api version after 2019-10-01

Is there a new version of the package (or is there expected to be) that supports this version?

What alternatives do I have in the meantime?

I can directly use a GET against

https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.Consumption/usageDetails?api-version=2019-10-01

but is there any other option?

UPDATED WITH CODE

Request code:

AuthenticationResult result = GetToken();
Microsoft.Rest.TokenCredentials tokenCredentials = new 
Microsoft.Rest.TokenCredentials(result.AccessToken);
ConsumptionManagementClient client = new 
ConsumptionManagementClient(tokenCredentials);
client.SubscriptionId = "SubscriptionId I would like to check"; 

var usage = client.UsageDetails.List(); // Exception here with API Version

Auth Code sample:

private static AuthenticationResult GetToken()
{
    string clientId = "MyappId";

    string[] scopes = new string[] { "https://management.azure.com/.default" };
    var app = PublicClientApplicationBuilder
        .Create(clientId)
        .WithRedirectUri("https://localhost")
        .WithTenantId("TenantId I want to check")
        .Build();
    var task = app.GetAccountsAsync();
    task.Wait();
    var accounts = task.Result;
    try
    {
        var task1 = app.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
        task1.Wait();
        return task1.Result;
    }
    catch (MsalUiRequiredException)
    {
        var task2 = app.AcquireTokenInteractive(scopes).ExecuteAsync();
        task2.Wait();
        return task2.Result;
    }
}
exvdev365
  • 13
  • 3
  • Have you referred to https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/consumption/Microsoft.Azure.Management.Consumption/tests/ScenarioTests/SubscriptionUsagesTests.cs? – Jim Xu Aug 27 '20 at 00:58
  • I have just created an issue asking in https://github.com/Azure/azure-sdk-for-net – exvdev365 Aug 27 '20 at 06:34
  • Could you please provide your code? – Jim Xu Aug 27 '20 at 07:09
  • Code provided. Thanks! – exvdev365 Aug 27 '20 at 07:50
  • Could you please tell me your subscription type? – Jim Xu Aug 27 '20 at 08:38
  • Regarding `ConsumptionManagementClient`, we have no way to set `api version` and we just can get `api version`: https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.consumption.consumptionmanagementclient.apiversion?view=azure-dotnet#Microsoft_Azure_Management_Consumption_ConsumptionManagementClient_ApiVersion – Jim Xu Aug 27 '20 at 09:04
  • Sorry for the delay: Subscription type - CSP : 0145P – exvdev365 Aug 28 '20 at 08:09

1 Answers1

0

According to my research, we can initialize ServiceClientCredentials to create ConsumptionManagementClient and the ServiceClientCredentials has method ProcessHttpRequestAsync which we can use to recreate request

For example

  1. Implement ServiceClientCredentials
class CustomLoginCredentials : ServiceClientCredentials
    {
       
        private string AuthenticationToken { get; set; }
        public override void InitializeServiceClient<T>(ServiceClient<T> client)
        {
           string clientId = "MyappId";

    string[] scopes = new string[] { "https://management.azure.com/.default" };
    var app = PublicClientApplicationBuilder
        .Create(clientId)
        .WithRedirectUri("https://localhost")
        .WithTenantId("TenantId I want to check")
        .Build();
            var task = app.GetAccountsAsync();
            task.Wait();
            var accounts = task.Result;
            try
            {
                var task1 = app.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
                task1.Wait();
                AuthenticationToken= task1.Result.AccessToken;
            }
            catch (MsalUiRequiredException)
            {
                var task2 = app.AcquireTokenInteractive(scopes).ExecuteAsync();
                task2.Wait();
                AuthenticationToken= task2.Result.AccessToken;
            }
            
        }
        public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (AuthenticationToken == null)
            {
                throw new InvalidOperationException("Token Provider Cannot Be Null");
            }

            var url = request.RequestUri.ToString().Split('?')[0] + "?";
            var querys = request.RequestUri.ToString().Split('?')[1].Split('&');
            for (var i = 1; i <= querys.Length; i++)
            {

                if (querys[i - 1].StartsWith("api-version"))
                {
                    url += "api-version=2019-10-01";
                }
                else
                {
                    url += querys[i - 1];
                }


                if (i < querys.Length)
                {

                    url += "&";
                }



            }

            Console.WriteLine(url);

            request.RequestUri = new Uri(url);

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);



            await base.ProcessHttpRequestAsync(request, cancellationToken);

        }



    }
  1. Get details
 var cred = new CustomLoginCredentials();
ConsumptionManagementClient client = new ConsumptionManagementClient(cred);
client.SubscriptionId = "SubscriptionId I would like to check"; 
var usage = client.UsageDetails.List();
Jim Xu
  • 21,610
  • 2
  • 19
  • 39