按资源获取订阅的使用情况数据

适用于:合作伙伴中心 | Microsoft Cloud for US Government 合作伙伴中心

本文介绍如何获取 ResourceUsageRecord 资源。 此资源表示 Azure 计划中预配的单个资源的当月聚合总计。 可以使用此资源获取客户在当前计费周期内特定 Azure 服务或资源的资源使用情况记录。 此 API 返回以前无法通过 Azure 支出 API 获取的数据。

此路由不支持 Microsoft Azure (MS-AZR-0145P) 订阅。

必备条件

  • 合作伙伴中心身份验证中所述的凭据。 此方案只支持使用应用凭据和用户凭据进行身份验证。

  • 客户 ID (customer-tenant-id)。 如果不知道客户的 ID,可以通过选择“客户”工作区、客户列表中的“客户”和“帐户”,在合作伙伴中心中查找该 ID。 在客户的“帐户”页上的“客户帐户信息” 部分查找 Microsoft ID。 Microsoft ID 与客户 ID (customer-tenant-id) 相同。

  • 订阅标识符

C#

若要在当前计费周期内获取特定 Azure 服务或资源的客户的资源使用情况记录,请执行以下操作:

  1. 使用 IAggregatePartner.Customers 集合调用 ById () 方法。

  2. 调用 Subscriptions 属性和 UsageRecords,然后调用 Resources 属性。 通过调用 Get () 或 GetAsync () 方法完成。

    // IAggregatePartner partnerOperations;
    // var selectedCustomerId as string;
    // var selectedSubscriptionId as string;
    
    var usageRecords = partnerOperations.Customers.ById(selectedCustomerId).Subscriptions.ById(selectedSubscriptionId).UsageRecords.Resources.Get();
    

有关示例,请参阅以下内容:

  • 示例: 控制台测试应用
  • 项目: PartnerSDK.FeatureSamples
  • 类: GetSubscriptionUsageRecordsByResource.cs

REST 请求

请求语法

方法 请求 URI
GET {baseURL}/v1/customers/{customer-tenant-id}/subscriptions/{subscription-id}/resourceusagerecords HTTP/1.1

URI 参数

下表列出了获取客户的分级使用情况信息所需的查询参数。

名称 类型 必须 说明
customer-tenant-id guid Y 与客户对应的 GUID。
subscription-id guid Y 对应于合作伙伴中心 订阅资源的标识符的 GUID,它表示 Microsoft Azure (MS-AZR-0145P) 订阅或 Azure 计划。 对于 Azure 计划订阅资源,请在此路由中提供 plan-id 作为 订阅 ID

请求标头

有关详细信息,请参阅合作伙伴中心 REST 标头

请求正文

无。

请求示例

GET https://api.partnercenter.microsoft.com/v1/customers/{customer-tenant-id}/subscriptions/{subscription-id}/resourceusagerecords HTTP/1.1
Authorization: Bearer <token>
Accept: application/json
MS-RequestId: e128c8e2-4c33-4940-a3e2-2e59b0abdc67
MS-CorrelationId: 47c36033-af5d-4457-80a4-512c1626fac4

REST 响应

如果成功,此方法在响应正文中返回 PagedResourceCollection<ResourceUsageRecord> 资源。

响应的成功和错误代码

每个响应都带有一个 HTTP 状态代码,用于指示成功或失败以及其他调试信息。 请使用网络跟踪工具来读取此代码、错误类型和其他参数。 有关完整列表,请参阅 错误代码

响应示例

HTTP/1.1 200 OK
Content-Length: 1120
Content-Type: application/json
MS-CorrelationId: 47c36033-af5d-4457-80a4-512c1626fac4
MS-RequestId: e128c8e2-4c33-4940-a3e2-2e59b0abdc67
Date: Tue, 17 Sep 2019 20:31:45 GMT

{
    "totalCount": 3,
    "items": [
        {
            "subscriptionId": "{subscription-id}",
            "resourceUri": "/subscriptions/{subscription-id}/resourceGroups/TESTRG1/providers/Microsoft.Compute/disks/testVM1_OsDisk_1_531d3c99534b4649ae025d485370143e",
            "resourceType": "Microsoft.Compute",
            "entitlementId": "{entitlemen-id}",
            "entitlementName": "Partner Subscription",
            "resourceGroupName": "TESTRG1",
            "name": "testVM1_OsDisk_1_531d3c99534b4649ae025d485370143e",
            "resourceName": "testVM1_OsDisk_1_531d3c99534b4649ae025d485370143e",
            "totalCost": 2.0211938955034572,
            "currencyCode": "GBP",
            "usdTotalCost": 2.4700000000000001,
            "lastModifiedDate": "2019-09-17T21:08:44.2566667+00:00",
            "attributes": {
                "objectType": "ResourceUsageRecord"
            }
        },
        {
            "subscriptionId": "{subscription-id}",
            "resourceUri": "/subscriptions/{subscription-id}/resourceGroups/TESTRG1/providers/Microsoft.Compute/virtualMachines/testVM1",
            "resourceType": "Microsoft.Compute",
            "entitlementId": "{entitlement-id}",
            "entitlementName": "Partner Subscription",
            "resourceGroupName": "TESTRG1",
            "name": "testVM1",
            "resourceName": "testVM1",
            "totalCost": 80.3322286322163563,
            "currencyCode": "GBP",
            "usdTotalCost": 98.1699999999999985,
            "lastModifiedDate": "2019-09-17T21:08:44.2566667+00:00",
            "attributes": {
                "objectType": "ResourceUsageRecord"
            }
        },
        {
            "subscriptionId": "{subscription-id}",
            "resourceUri": "/subscriptions/{subscription-id}/resourceGroups/testrg1/providers/Microsoft.Storage/storageAccounts/testrg1diag153",
            "resourceType": "Microsoft.Storage",
            "entitlementId": "{entitlemen-id}",
            "entitlementName": "Partner Subscription",
            "resourceGroupName": "testrg1",
            "name": "testrg1diag153",
            "resourceName": "testrg1diag153",
            "totalCost": 0.0081829712368561032,
            "currencyCode": "GBP",
            "usdTotalCost": 0.0099999999999999997,
            "lastModifiedDate": "2019-09-17T21:08:44.2566667+00:00",
            "attributes": {
                "objectType": "ResourceUsageRecord"
            }
        }
    ],
    "links": {
        "self": {
            "uri": "/customers/<customer-tenant-id>/subscriptions/<subscription-id>/resourceusagerecords",
            "method": "GET",
            "headers": []
        }
    },
    "attributes": {
        "objectType": "Collection"
    }
}