Azure SDK for Python を試す機会があったので備忘録として。
ちなみにPythonはVisual Studio上でプロジェクト作ってデバッグして実行できて便利になってますが、その辺りのセットアップ方法は以下。
SDK関連は以下です。
- GitHub - Azure/azure-sdk-for-python: Microsoft Azure SDK for Python
- Azure SDK for Python — Azure SDK for Python 2.0.0 RC1 documentation
他のSDKと同様、Python SDKも過渡期です。旧来のSDKからAutoRestを使ったフレームワークに移行中です。以下は、2.0.0a1 というAutoRest のα版を使っています。調べるドキュメントが古かったりすると、全然違うので、お気をつけてください。
インストール
pip にて、バージョン指定してインストールします。パッケージ情報は以下の通りです。
> pip install azure=2.0.0a1 ... ... > pip freeze adal==0.1.0 azure==2.0.0a1 azure-common==1.0.0 azure-graphrbac==0.30.0a1 azure-mgmt==0.30.0a1 azure-mgmt-authorization==0.30.0a1 azure-mgmt-cdn==0.30.0a1 azure-mgmt-compute==0.30.0a1 azure-mgmt-logic==0.30.0a1 azure-mgmt-network==0.30.0a1 azure-mgmt-notificationhubs==0.30.0a1 azure-mgmt-nspkg==1.0.0 azure-mgmt-redis==0.30.0a1 azure-mgmt-resource==0.30.0a1 azure-mgmt-scheduler==0.30.0a1 azure-mgmt-storage==0.30.0a1 azure-mgmt-web==0.30.0a1 azure-nspkg==1.0.0 azure-servicebus==0.20.1 azure-servicemanagement-legacy==0.20.2 azure-storage==0.30.0
VMの開始とか
詳細は省きますが、コード的には他言語のAutoRestとほぼ同じです。操作は非同期っぽく(JavaやNETはSyncとAsyncメソッド両方用意されている)、戻り値をwaitしてやってください。
#!/usr/bin/env python # -*- coding: utf-8 -*- from msrestazure.azure_active_directory import ServicePrincipalCredentials from azure.mgmt.compute import ComputeManagementClient, ComputeManagementClientConfiguration from azure.mgmt.network import NetworkManagementClient, NetworkManagementClientConfiguration from msrestazure.azure_operation import AzureOperationPoller sub = 'your subscription id' ten = 'your tenant id' app = 'your application id' pwd = 'your application password' credentials = ServicePrincipalCredentials(app, pwd, tenant=ten) compute_client = ComputeManagementClient(ComputeManagementClientConfiguration(credentials, sub)) resoureGroup = 'hogerg' vmName = 'fugavm' res = compute_client.virtual_machines.start(resoureGroup, vmName) res.wait()
インテリセンスの効きがresまで効いてくれないので、少し面倒臭いです。なんか設定で回避できるのかもしれませんが。
以上