LoginSignup
8
7

More than 5 years have passed since last update.

Azureの課金系REST APIをcurlで実行する方法

Posted at

課金用サービスプリンシパル作成

  • Azure ADを選択して、[アプリの登録]→[新しいアプリケーションの登録]の順に選択します。
    image.png

  • アプリ名を入れます。サインオンURLは使わないので適当で。
    image.png

  • 作成されたらアプリケーションIDをメモっておきます。
    image.png

  • [設定]を選択します。
    image.png

  • [キー]を選択します。
    image.png

  • 説明に[token]を、有効期限は要件次第ですが今回は[期限なし]を選択して、[保存]を押します。
    image.png

  • キーが表示されるのでメモしておきます。
    image.png

課金用サービスプリンシパルに権限を付与する

  • サブスクリプションを選択して、[アクセス制御(IAM)]→[ロールの割り当て]→[ロールの割り当ての追加]の順に選択します。
    image.png

  • 役割に[課金データ閲覧者]を選び、先程作ったサービスプリンシパルに権限を与えます。
    image.png

Azure Active DirectoryテナントID取得

  • AADの[プロパティ]を選択し、[ディレクトリID]をメモしておきます。 image.png

Azure Token取得

  • Azure Tokenを取得して、変数tokenに代入します。
token=`curl -X POST https://login.microsoftonline.com/[AAD Tenant ID(Directory ID)]/oauth2/token -F grant_type=client_credentials -F resource=https://management.core.windows.net/ -F client_id=[Clinet ID(Application ID)] -F client_secret=[Client Secret ID(token)] | jq -r .access_token`

Azure Invoice Download API

請求書のダウンロードURLをJSONで得ることができるAPIです。

実行コマンド

curl -X GET -L -H "Content-Type: application/json" -H "Authorization: Bearer $token" "https://management.azure.com/subscriptions/[Subscription ID]/providers/Microsoft.Billing/invoices/latest?api-version=2017-04-24-preview | jq ."

JSON出力イメージ

  • このURLにアクセスすると、最新の請求書を確認することができます。
{
  "id": "/subscriptions/xxxxx/providers/Microsoft.Billing/invoices/201812-12345",
  "type": "Microsoft.Billing/invoices",
  "name": "201812-12345",
  "properties": {
    "downloadUrl": {
      "expiryTime": "2018-12-03T13:18:25Z",
      "url": "https://billinginsightsstore04.blob.core.windows.net/invoices/xxxxx.pdf?sv=2014-02-14&sr=b&sig=n%xxxxx%3D&se=2018-12-03T13%3A18%3A25Z&sp=r"
    },
    "invoicePeriodEndDate": "2018-11-01",
    "invoicePeriodStartDate": "2018-10-02",
    "billingPeriodIds": [
      "/subscriptions/xxxxx/providers/Microsoft.Billing/billingPeriods/201812-1"
    ]
  }
}

Azure Resource Usage API

使用した各Azureリソースの使用量をJSONで返します。使用料金ではなく、消費データ量であることに注意が必要です。
以下のコマンド例では2018/9/1~2018/10/1のリソース使用量を出力しています。

実行コマンド

curl -X GET -L -H "Content-Type: application/json" -H "Authorization: Bearer $token" "https://management.azure.com/subscriptions/[Subscription ID]/providers/Microsoft.Commerce/UsageAggregates?api-version=2015-06-01-preview&reportedStartTime=2018-09-01&reportedEndTime=2018-10-01&showDetails=f" > ResourceUsage.json

JSON出力イメージ

  • quantityが各リソースの消費量を表します。
  • nextLinkに続きのJSONが格納されているので、nextLinkの表示がなくなるまで確認する必要があります。
{
  "value": [
    {
      "id": "/subscriptions/xxxxx/providers/Microsoft.Commerce/UsageAggregates/Daily_BRSDT_20180901_0000",
      "name": "Daily_BRSDT_20180901_0000",
      "type": "Microsoft.Commerce/UsageAggregate",
      "properties": {
        "subscriptionId": “xxxxx",
        "usageStartTime": "2018-09-03T00:00:00+00:00",
        "usageEndTime": "2018-09-04T00:00:00+00:00",
        "meterName": "Pay-as-you-go Data at Rest",
        "meterCategory": "Data Lake Store",
        "unit": "1 GB/Month",
        "instanceData": "{\"Microsoft.Resources\":{\"resourceUri\":\"/subscriptions/xxxxx/resourceGroups/testadl/providers/
Microsoft.DataLakeStore/accounts/testadl\",\"location\":\"eastus2\"}}",
        "meterId": "c7929966-0a48-4f73-a86b-30f4e39d1995",
        "infoFields": {},
        "quantity": 0.011361
      }
    },
・・・・・略・・・・・
 "meterId": "3f2b1e1c-c886-4ec6-ad6f-dd0ef38819c9",
        "infoFields": {},
        "quantity": 7.2e-05
      }
    }
  ],
  "nextLink": "https://management.azure.com:443/subscriptions/xxxxx/providers/Microsoft.Commerce/UsageAggregates?
api-version=2015-06-01-preview&reportedStartTime=2018-09-01&reportedEndTime=2018-10-01&showDetails=f&continuationToken=token%3d%3d"
}

Azure Resource RateCard API

Azure全リージョン、全リソースの料金表をJSONで返します。
OfferDurableIdに課金プランIDを指定します。以下コマンド例では従量課金プランを対象とした「MS-AZR-0003P」を指定しています。
OfferDurableId一覧

実行コマンド

curl -X GET -L -H "Content-Type: application/json" -H "Authorization: Bearer $token" "https://management.azure.com/subscriptions/[Subscription ID]/providers/Microsoft.Commerce/RateCard?api-version=2016-08-31-preview&%24filter=OfferDurableId+eq+'MS-AZR-0003P'+and+Currency+eq+'JPY'+and+Locale+eq+'ja-JP'+and+RegionInfo+eq+'JP'" > RateCard.json

JSONイメージ

  • この条件のVirtual Machinesは、1時間あたり8.624円ということが分かります。
{
  "OfferTerms": [],
  "Meters": [
    {
      "EffectiveDate": "2018-01-01T00:00:00Z",
      "IncludedQuantity": 0,
      "MeterCategory": "Virtual Machines",
      "MeterId": "d0bf9053-17c4-4fec-8502-4eb8376343a7",
      "MeterName": "F2/F2s 低優先度",
      "MeterRates": {
        "0": 8.624
      },
      "MeterRegion": "米国西部 2",
      "MeterStatus": "Active",
      "MeterSubCategory": "F/FS シリーズ Windows",
      "MeterTags": [],
      "Unit": "1 時間"
    },
・・・・・略・・・・・
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7