概要
Azure SDK for Python を利用して、とある サブスクリプションのリソースグループ一覧の取得と、「list_by_resource_group()」を利用して、指定したリソースグループにあるリソース一覧を取得する Python プログラムです。
実行環境
- macOS Monterey 12.1
- python 3.8.3
- Azure CLI 2.28.0
前提条件
- Azure環境がすでに用意されていること(テナント/サブスクリプション)
- ローカル環境に「azure cli」がインストールされていること
- この記事の環境がAzure上に構築されていること
実行プログラム
ListbyResourceGroup.py
import os
import time
import argparse
from azure.identity import AzureCliCredential, DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
# Retrieve subscription ID from environment variable.
SUBSCRIPTION_ID = os.environ["ARM_SUBSCRIPTION_ID"]
# リソースの管理オブジェクトの取得
def GetResourceManagementClient():
resource_client = ResourceManagementClient(
# credential=DefaultAzureCredential()
credential=AzureCliCredential(),
subscription_id=SUBSCRIPTION_ID
)
return resource_client
# サブスクリプションのリソースグループ一覧の取得
def GetResourceGroup():
# Obtain the management object for resources.
resource_client = GetResourceManagementClient()
# Retrieve the list of resource groups
group_list = resource_client.resource_groups.list()
# for group in list(group_list):
# print("Get ResourceGroup:\n{}\n".format(group))
# Show the groups in formatted output
column_width = 50
print("Resource Group".ljust(column_width) + "Location")
print("-" * (column_width * 2))
for group in list(group_list):
print(f"{group.name:<{column_width}}{group.location}")
# 指定したリソースグループ内のリソース一覧の取得
def Get_list_by_resource_group(rg_name):
# Obtain the management object for resources.
resource_client = GetResourceManagementClient()
# Retrieve the list of Resource_Group
resource_list = resource_client.resources.list_by_resource_group(rg_name)
# for resource in list(resource_list):
# print("■ Get Resource : {}\n{}".format(resource.name, resource))
# print("----> Resource sku:\n{}\n".format(resource.sku))
# Show the groups in formatted output
column_width2 = 15
column_width = 60
print("Location".ljust(column_width2) + "Resource".ljust(column_width) + "Type")
print("-" * (column_width2 * 9))
for resource in list(resource_list):
print(f"{resource.location:<{column_width2}}{resource.name:<{column_width}}{resource.type}")
# メイン
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='引数なし:リソースグループ一覧の取得、 引数あり:指定したリソースグループ内のリソース一覧の取得')
parser.add_argument('-g', '--rg', type=str, help='リソースグループ名')
args = parser.parse_args()
start = time.time()
if args.rg == None :
GetResourceGroup()
else :
Get_list_by_resource_group(args.rg)
generate_time = time.time() - start
print("\n 取得時間:{0}".format(generate_time) + " [sec] \n")
プログラムのHELP表示
## HELPの表示
$ python ListbyResourceGroup.py -h
usage: ListbyResourceGroup.py [-h] [-g RG]
引数なし:リソースグループ一覧の取得、 引数あり:指定したリソースグループ内のリソース一覧の取得
optional arguments:
-h, --help show this help message and exit
-g RG, --rg RG リソースグループ名
プログラムの実行
リソースグループ一覧の取得
$ python ListbyResourceGroup.py
Resource Group Location
----------------------------------------------------------------------------------------------------
rg_AzureStackHCI eastus
rg_NetworkWatcher japaneast
:
省略
:
rg_ituru_vm02 japaneast
rg-sh-dup-01 japaneast
取得時間:0.4982631206512451 [sec]
指定したリソースグループのリソース一覧の取得
$ python ListbyResourceGroup.py -g rg_ituru_vm02
Location Resource Type
---------------------------------------------------------------------------------------------------------------------------------------
japaneast vm-ituru-ubuntu_OsDisk_1_95c70351f059403ab53b184dc7a54270 Microsoft.Compute/disks
japaneast vm-ituru-ubuntu Microsoft.Compute/virtualMachines
japaneast nic_ituru_vm02 Microsoft.Network/networkInterfaces
japaneast nsg_ituru_vm02 Microsoft.Network/networkSecurityGroups
japaneast pip_ituru_vm02 Microsoft.Network/publicIPAddresses
japaneast vnet_ituru_vm02 Microsoft.Network/virtualNetworks
取得時間:1.0337650775909424 [sec]
まとめ
このプログラムで、指定するリソースグループにあるリソース一覧をサクッと手元で入手することが可能となりました。