天蓝色武器消耗:获取资源组的消耗 [英] azure-arm-consumption: get consumption of a resource group
问题描述
在我的Node.js项目中,我尝试使用 azure-arm-consumption软件包,以获取资源组的当前消耗/计费.我的意思是,到目前为止,在这个资源组上花了多少钱.
In my Node.js project I am trying to use azure-arm-consumption package to get the current consumption/billing of a resource group. I mean, how much money was spent on this resource group until now.
在接口下,从AggregatedCost到UsageDetails,所有这些接口都包含方法,但我只是设法找到读取特定资源组已消耗多少钱的方法.
Under Interfaces, from AggregatedCost to UsageDetails, all of these interfaces contain methods, but I just don't manage to find the method for reading how much money a specific resource group has consumed.
我的代码:
const MsRest = require('ms-rest-azure');
const credentials = MsRest.loginWithServicePrincipalSecret(keys.appId, keys.pass, keys.tenantId);
const { ConsumptionManagementClient } = require('azure-arm-consumption');
const client = new ConsumptionManagementClient (credentials, subscriptionId);
const cost = client.forecasts.list(subscriptionId);
它检索我的订阅的消费除以日期.现在的问题是,我不希望将其按日期划分,而是按资源组划分. 该API 可以做到吗?
It retrieves the consumption of my subscription divided by date. Now the problem is that I don't want it to be divided by date, but by resource group. Is there any method in this API that can do that?
推荐答案
根据文档,没有可用的方法/端点来按资源组获取数据.您必须下载所有数据并自行处理.
Accoring the documentation, there is no available method/endpoint for getting data by resource group. You have to download all data and process them by yourself.
这是将使用REST下载数据(使用ConsumptionManagementClient几乎相同)并按资源组汇总使用量的脚本.以它为起点.
Here is script that will download data using REST (using ConsumptionManagementClient will be almost the same) and sum usage by resource group. Take it as starting point.
const axios = require('axios');
function processUsage(data) {
let usage = [];
for (index in data.value) {
const properties = data.value[index].properties;
const cost = properties.pretaxCost;
let resourceGroup = properties.instanceId.replace(`/subscriptions/${properties.subscriptionGuid}/resourceGroups/`, "");
resourceGroup = resourceGroup.substring(0, resourceGroup.indexOf("/"));
let foundUsage = usage.filter(x => x.rg === resourceGroup);
if (foundUsage.length > 0) {
foundUsage[0].cost += cost
} else {
usage.push({
rg: resourceGroup,
cost: cost
});
}
}
usage.map(x => console.log(`${x.rg} | ${x.cost}`));
// Example result display list of resource groups and costs in EUR (in my case)
// myresourcegroup | 1.2234748382055878
// DevTestLab | 0.0004314997440000002
}
function getUsage(subscriptionId, accessToken) {
const url = `https://management.azure.com/subscriptions/${subscriptionId}/providers/Microsoft.Consumption/usageDetails?api-version=2018-10-01`
const options = {
headers: {
Authorization: `Bearer ${accessToken}`
}
}
axios.get(url, options).then(response => {
processUsage(response.data);
}).catch(error => {
console.log(error);
});
}
getUsage(
"subscription id",
"access token"
);
参考:
https://docs.microsoft.com/en-us/rest/api/consumption/usagedetails https://docs.microsoft .com/en-us/javascript/api/azure-arm-consumption/usagedetails#list-object-
https://docs.microsoft.com/zh-我们/javascript/api/azure-arm-consumption/usagedetail
https://azure.microsoft.com/cs -cz/blog/azure-consumption-usage-details-api/
Reference:
https://docs.microsoft.com/en-us/rest/api/consumption/usagedetails https://docs.microsoft.com/en-us/javascript/api/azure-arm-consumption/usagedetails#list-object-
https://docs.microsoft.com/en-us/javascript/api/azure-arm-consumption/usagedetail
https://azure.microsoft.com/cs-cz/blog/azure-consumption-usage-details-api/
这篇关于天蓝色武器消耗:获取资源组的消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!