Azure REST API - 在哪里可以找到资源成本?

时间:2018-02-01 09:59:37

标签: azure azure-api-management

我正在查看其余API的文档,我正在寻找我的资源成本。

查看REST API和SDK for .NET,有一个Consumption端点执行此操作,但在进一步测试时,这似乎只提供了最新的可用结算周期(24小时窗口)。

示例代码:

[{a:1,b:2},{a:5,b:6}]

var filterString = $"properties/instanceName eq \'{vm.VirtualMachineName}\'"; var vmConsumptionData = await consumptionClient.Item1.UsageDetails.ListAsync( scope: $"/subscriptions/{consumptionClient.subscriptionGuid}", filter: filterString ); 是在其他地方作为元组的一部分返回的consumeClient的实例。

结果是昨天的最后一次结算金额。

是否还有其他任何端点可以提供Azure中我的资源的最新成本概览? I.E.每分钟或每小时

我认为可以做到的另一种方法是检索资源的“使用数量”,并根据每小时的资源成本进行处理。

1 个答案:

答案 0 :(得分:1)

截至Microsoft.CostManagement/QueryMar-06-2020文档开始有效:

POST https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.CostManagement/query?api-version=2019-11-01&$top=10000
Content-Type: application/json
{
    "type": "ActualCost",
    "dataSet": {
        "granularity": "None",
        "aggregation": {
            "totalCost": {
                "name": "PreTaxCost",
                "function": "Sum"
            },
            "totalCostUSD": {
                "name": "PreTaxCostUSD",
                "function": "Sum"
            }
        },
        "filter": {
            "Dimensions": {
                "Name": "ResourceId",
                "Operator": "In",
                "Values": [
                    "{resourceId}"
                ]
            }
        }
    },
    "timeframe": "Custom",
    "timePeriod": {
        "from": "2020-02-29T00:00:00+00:00",
        "to": "2020-03-06T23:59:59+00:00"
    }
}

有回应:

{
    "id": "subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.CostManagement/query/XXXXXXXX-9ab4-XXXX-83fe-XXXXXXXXXXXX",
    "name": "XXXXXXXX-9ab4-XXXX-83fe-XXXXXXXXXXXX",
    "type": "Microsoft.CostManagement/query",
    "location": null,
    "sku": null,
    "eTag": null,
    "properties": {
        "nextLink": null,
        "columns": [
            {
                "name": "PreTaxCost",
                "type": "Number"
            },
            {
                "name": "Currency",
                "type": "String"
            }
        ],
        "rows": [
            [
                1234.5678910,
                "CAD"
            ]
        ]
    }
}

我发现这为我的特定资源返回了准确的值。

注意:唯一要担心的是,此API端点在documentation中被标记为-legacy。不确定是否会长期支持它,但现在可以使用。