課金用サービスプリンシパル作成
課金用サービスプリンシパルに権限を付与する
Azure Active DirectoryテナントID取得
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 時間"
},
・・・・・略・・・・・