Hot questions for Using Azure in azure billing api

Top Java Programmings / Azure / azure billing api

Question:


Answer:

I am not aware of any Billing and Usage SDK for .net or Java. I think there is only the following options:

Azure Usage API – A REST API that customers and partners can use to get their usage data for an Azure subscription.

Azure RateCard API – A REST API that customers and partners can use to get the list of the available resources they can use, along with metadata and price information about them.

http://weblogs.asp.net/scottgu/new-azure-billing-apis-available

https://msdn.microsoft.com/en-us/library/azure/mt218998.aspx

So yes, you would have to make your own HTTP client requests.

Question:

I am trying to get the usage and rate card information from Microsoft Azure using a java application and I came to understand that I can use the Management certificate to authenticate for making calls to Microsoft Azure.

I got the Management Certificate from the .publishsettings file I got from here

However, in AuthenticationContext, I don't see any method that utilizes this certificate to get the access token required for making usage and rate API calls.

I tried referring to this answer, but I don't see any clients available for usage and rate card and the answer refers to ManagementClient, which isn't the one for my usecase. I referred to this blog as well, which makes a reference to ClientAssertionCertificate , which I don't see in the java library for adal.

NB: I am able to make REST API calls to Azure for getting usage and rate card information using the username, password & client ID based authentication mechanism, but I wanted to make use of this management certificate mechanism since the users of my application may not trust this application with their credentials and this certificate based mechanism seems more easier to use from a user-point of view.


Answer:

However, in AuthenticationContext, I don't see any method that utilizes this certificate to get the access token required for making usage and rate API calls.

I referred to this blog as well, which makes a reference to ClientAssertionCertificate , which I don't see in the java library for adal.

As Gaurav said, We just only can call Usage & Rate Card API using Azure Active Directory for authentication. You can use AuthenticationContext to acquire the the access_token as following code. You need provide client ID and Client Secret(key).

private AuthenticationResult getAccessTokenFromClientCredentials()
            throws Throwable {
        AuthenticationContext context = null;
        AuthenticationResult result = null;
        ExecutorService service = null;
        try {
            service = Executors.newFixedThreadPool(1);
            context = new AuthenticationContext(authority + tenant + "/", true,
                    service);
            Future<AuthenticationResult> future = context.acquireToken(
                    "https://graph.windows.net", new ClientCredential(clientId,
                            clientSecret), null);
            result = future.get();
        } catch (ExecutionException e) {
            throw e.getCause();
        } finally {
            service.shutdown();
        }

        if (result == null) {
            throw new ServiceUnavailableException(
                    "authentication result was null");
        }
        return result;
    }

NB: I am able to make REST API calls to Azure for getting usage and rate card information using the username, password & client ID based authentication mechanism,.....

It seems that we can't use Management certificate mechanism to call Usage & Rate Card API. Because these calling user or the service principal is a member of the Owner, Contributor or Reader role in the Azure AD tenant for the requested subscription (see this document). I recommend you refer to this document about how to authenticate Azure Resource Management.

Question:

The rate card API for Microsoft Azure's billing API:

https://management.azure.com/subscriptions/{subscription-Id}/providers/Microsoft.Commerce/RateCard?api-version={api-version}&$filter=OfferDurableId eq ’{OfferDurableId}’ and Currency lt ’{Currency}’ and Locale eq ’{Locale}’ and RegionInfo eq ’{RegionInfo}’’

requires the regionInfo to be provided for each request. The regionInfo, according to the documentation, refers to the location where the offer was purchased (offer referring to the OfferDurableId). The OfferDurableId can be obtained from the subscription and subsequently from here. However, I didn't find the regionInfo anywhere.

How to get the regionInfo for a subscription?

N.B.: I have raised an issue on the API's github repository.


Answer:

Find out the purchase location as follows:

Login in the Azure Portal. In the top right corner, click on your username and then choose "View my bill." This will open account.windowsazure.com. Choose profile. Your country code will be there.

Also, you an look up the two digit ISO region code here: https://www.iso.org/obp/ui/#search For instance, Canada is CA in the ISO list

Question:

Using Microsoft Azure's billing and usage API, I see that we can get the usage quantity of a resource for a project and the rate for the resource consumption from the rate card API. However, the rate card API, in some cases, has a list of key-value pairs.

For ex, consider the following MeterRates:

"MeterRates": {
                "0": 0.0832,
                "1024": 0.0819,
                "51200": 0.0806,
                "512000": 0.0794,
                "1024000": 0.0775,
                "5120000": 0.0775
            },

Here, if the usage quantity is, say, 102400; does it mean the cost of using the resource will be 102400 * 0.0784 or (102400 - 1023) * 0.0832 + (102400 - 1023 - 51200) * 0.0819?

N.B.: I have an issue on the github repository for the billing and usage API regarding the same.


Answer:

Here, if the usage quantity is, say, 102400; does it mean the cost of using the resource will be 102400 * 0.0784 or (102400 - 1023) * 0.0832 + (102400 - 1023 - 51200) * 0.0819?

It will actually be more like: 1023 * 0.0832 + 50176 * 0.0819 + 460800 * 0.0794 + 512000 * 0.0775

Essentially these are pricing tiers so the way you read them is from 0 to 1023, it would be 0.0832 / unit. From 1024 - 51200, it would be 0.0819 units and so on.