访问 Azure 计费 API

Access azure billing API

我想创建一个仪表板,其中包含有关我的 Azure 资源成本的图表(尽可能详细)。意思是,月度发票清单是不够的(但如果我能做到这一点,我已经很高兴了!!)

无论如何,我注意到的第一件事是,如果您找到一个示例,端点 urls 看起来像这样

 https://management.azure.com/subscriptions/${subscriptionId}/resourcegroups?api-version=2016-09-01

检查 url 2016-09-01 的末尾,看起来不是很新。 This 中 post 是我能找到的最好的文章,但它也使用了这些 url。 此外,我无法按照描述的步骤进行操作,首先它使用 postman 检索 access_token (对我来说不是很有用,因为我需要它自动化),其次,在中间的某个地方 access_token 已检索但从未使用过。

所以,我找到了一个像 [azure-arm-billing][2] 这样的 npm 包,我可以从中编写以下程序(主要是复制粘贴):

const msRestAzure = require('ms-rest-azure');
const BillingManagement = require('azure-arm-billing')

const clientId = process.env['CLIENT_ID'];
const secret = process.env['APPLICATION_SECRET'];
const domain = process.env['DOMAIN']; 
const subscriptionId = process.env['AZURE_SUBSCRIPTION_ID'];

// Retrieve access_token
const app = new msRestAzure.ApplicationTokenCredentials(clientId, domain, secret);
app.getToken((err, token) => {
    console.log(token.accessToken);
});

// =======
msRestAzure
  .interactiveLogin( { domain }) // The argument here is nowhere documented
  .then(credentials => {
      console.log(credentials);
      let client = new BillingManagement(credentials, subscriptionId);
      return client.invoices.list();
  })
  .then(invoices => {
      console.log('List of invoices:');
      console.dir(invoices, { depth: null, colors: true });
  });

运行 这显示了一个不错的 access_token 和发票

...
List of invoices:
[
  {
    id: '/subscriptions/../providers/Microsoft.Billing/invoices/....',
    name: '...',
    type: 'Microsoft.Billing/invoices',
    invoicePeriodStartDate: 2019-08-25T00:00:00.000Z,
    invoicePeriodEndDate: 2019-09-24T00:00:00.000Z,
    billingPeriodIds: [
      '/subscriptions/.../pr..s/Micro..ing/bill..ods/201910-1'
    ]
  },
  {
    id: '/subscriptions/9ea...3d/providers/Microsoft.Billing/invoices/201909-...',
    name: '....',
    type: 'Microsoft.Billing/invoices',
    invoicePeriodStartDate: 2019-07-25T00:00:00.000Z,
    invoicePeriodEndDate: 2019-08-24T00:00:00.000Z,
    billingPeriodIds: [
      '/subscriptions/..../providers/Microsoft.Billing/billingPeriods/201909-1...'
    ]
  }
]

虽然我有发票,但没有数字。我想检索每种资源的成本。

所以文档似乎已经过时,甚至不存在我想要的(看起来)。我的问题是是否有人能够检索到这样的信息?我真的很想知道怎么做!!

更新 这似乎是一个权限问题。因此,下面我分享了一些屏幕截图,显示了我现在拥有的内容。也许从这些可以清楚地看出我错过了什么或设置不正确。首先,这是我最新的 nodejs 应用程序:

const msRestAzure = require("ms-rest-azure");
const ConsumptionManagementClient = require("azure-arm-consumption");

const clientId = '76d79....';          // App registration ID
const secret = '****...';              // App registration secret
const domain =  'dc36...';             // tenantId
const subscriptionId = '9ea2d...';     // subscription ID

const AzureServiceClient = msRestAzure.AzureServiceClient;

//an example to list resource groups in a subscription
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain).then((creds) => {
    const client = new ConsumptionManagementClient(creds, subscriptionId);
    const expand = '';
    const filter = '';
    const skiptoken = '';
    const top = 1000;
    const apply = '';
    return client.usageDetails.list(expand, filter, skiptoken, top, apply).then(result => {
    console.log('The result is:', result);
  });
}).catch((err) => {
  console.log('An error occurred:');
  console.dir(err, { depth: null, colors: true });
});

输出状态码401

Error: Unauthorized. Request ID: e6b127...
...

所以,我在 AD 中注册了一个 App

它的 API 权限是

终于,我只有一个订阅了

使用以下 IAM 设置

有什么可疑的吗?

如果您正在寻找资源成本,我建议您看一下 Consumption API - List Usage Details。这将为您提供所有资源的消耗。

您将需要安装 azure-arm-consumption 包。

示例代码如下:

const msRestAzure = require("ms-rest-azure");
const ConsumptionManagementClient = require("azure-arm-consumption");
msRestAzure.interactiveLogin().then((creds) => {
    const subscriptionId = "<your subscription id>";
    const client = new ConsumptionManagementClient(creds, subscriptionId);
    const expand = "";
    const filter = "";
    const skiptoken = "";
    const top = 1000;
    const apply = "";
    return client.usageDetails.list(expand, filter, skiptoken, top, apply).then((result) => {
      console.log("The result is:");
      console.log(result);
    });
}).catch((err) => {
  console.log('An error occurred:');
  console.dir(err, {depth: null, colors: true});
});

这是从这里截取的:https://github.com/Azure/azure-sdk-for-node/tree/master/lib/services/consumptionManagement