😀

Azure リソースの特定の値が取得できない場合は api-version を確認

に公開

Azure REST API を使って何かしらのコードを書いている場合、api-version に指定する値は、ドキュメントサンプルに記載されている値をそのまま使っていたり、Azure ポータルの JSON ビュー を開いたりして api-versin とそのレスポンス JSON の中身を確認していたりすると思います。

今回は、例えば Azure CLI や Azure Resource Graph で仮想マシンの作成日時は取得できるのに、Azure REST API で仮想マシンの作成日時が取得できない場合の確認方法を試してみした。

Azure CLI で仮想マシンの作成日時を取得

bash
az vm show \
  --resource-group mnrwp-rg \
  --name mnrwp-vm \
  --query timeCreated \
  --output tsv

2024-05-02T04:19:24.452556+00:00

Azure Resource Graph で仮想マシンの作成日時を取得

bash
az resource show \
  --resource-group mnrwp-rg \
  --name mnrwp-vm \
  --resource-type "Microsoft.Compute/virtualMachines" \
  --query properties.timeCreated \
  --output tsv

2024-05-02T04:19:24.4525565+00:00

Azure REST API で仮想マシンの作成日時を取得

bash
vmid=$(az vm show \
  --resource-group mnrwp-rg \
  --name mnrwp-vm \
  --query id \
  --output tsv
)

az rest \
  --url "$vmid?api-version=2021-03-01" \
  --query properties.timeCreated \
  --output tsv

作成日時が取得できません。

使用できる api-version を調べる

あえて存在しない値を指定して実行します。

bash
az rest \
  --url "$vmid?api-version=2021-03"

Bad Request({"error":{"code":"NoRegisteredProviderFound","message":"No registered resource provider found for location 'japaneast' and API version '2021-03' for type 'virtualMachines'. The supported api-versions are '2015-05-01-preview, 2015-06-15, 2016-03-30, 2016-04-30-preview, 2016-08-30, 2017-03-30, 2017-12-01, 2018-04-01, 2018-06-01, 2018-10-01, 2019-03-01, 2019-07-01, 2019-12-01, 2020-06-01, 2020-12-01, 2021-03-01, 2021-04-01, 2021-07-01, 2021-11-01, 2022-03-01, 2022-08-01, 2022-11-01, 2023-03-01, 2023-07-01, 2023-09-01, 2024-03-01, 2024-07-01'. The supported locations are 'eastus, eastus2, westus, centralus, northcentralus, southcentralus, northeurope, westeurope, eastasia, southeastasia, japaneast, japanwest, australiaeast, australiasoutheast, australiacentral, brazilsouth, southindia, centralindia, westindia, canadacentral, canadaeast, westus2, westcentralus, uksouth, ukwest, koreacentral, koreasouth, francecentral, southafricanorth, uaenorth, switzerlandnorth, germanywestcentral, norwayeast, jioindiawest, westus3, swedencentral, qatarcentral, polandcentral, italynorth, israelcentral, spaincentral, mexicocentral'."}})

サポートされている api-version で新しいものを見つけます。

最新の api-version で仮想マシンの作成日時を取得

bash
az rest \
  --url "$vmid?api-version=2024-07-01" \
  --query properties.timeCreated \
  --output tsv

2024-05-02T04:19:24.4525565+00:00

参考

https://learn.microsoft.com/ja-jp/cli/azure/vm?view=azure-cli-latest#az-vm-show

https://learn.microsoft.com/ja-jp/cli/azure/resource?view=azure-cli-latest#az-resource-show

https://learn.microsoft.com/ja-jp/rest/api/compute/virtual-machines/get?view=rest-compute-2024-03-01&tabs=HTTP#get-a-virtual-machine

Discussion