gpt4 book ai didi

node.js - 访问 Azure 计费 API

转载 作者:行者123 更新时间:2023-12-03 02:47:24 29 4
gpt4 key购买 nike

我想创建一个仪表板,其中包含有关我的 Azure 资源成本的图表(尽可能详细)。这意味着,每月发票 list 是不够的(但如果我能实现这一点,我会非常高兴!!)

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

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

检查网址的末尾2016-09-01,看起来不太最新。 This中等帖子是我能找到的最好的文章,但它也使用了这些网址。此外,我无法按照描述的步骤进行操作,首先它使用 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 中有一个应用程序注册

enter image description here

其API权限为

enter image description here

最后,我只有一个订阅

enter image description here

使用以下 IAM 设置

enter image description here

有任何可疑之处吗?

最佳答案

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

您需要安装azure-arm-conspiration包。

这是示例代码:

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 .

关于node.js - 访问 Azure 计费 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58135692/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com