Azure Python SDK 获取区域信息

Azure Python SDK get zone info

有人知道如何使用 Python SDK 获取区域信息吗?我发现 this technet post 向您展示了如何使用 powershell 按区域转储支持的机器类型,而等效的 Azure 命令行工具似乎如下所示:

user@server:~$ az vm list-skus -l southeastasia --zone | wc -l
   12350
user@server:~$ az vm list-skus -l southeastasia --zone | head -n 120 | grep family -A 18
    "family": "standardNVFamily",
    "kind": null,
    "locationInfo": [
      {
        "location": "southeastasia",
        "zoneDetails": [],
        "zones": [
          "3"
        ]
      }
    ],
    "locations": [
      "southeastasia"
    ],
    "name": "Standard_NV6",
    "resourceType": "virtualMachines",
    "restrictions": [],
    "size": "NV6",
    "tier": "Standard"
user@server:~$

可是翻了半天文档,还没找到合适的SDK方法。

compute_client.virtual_machine_images.list_skus() 没有 return 区域信息,只有图像,例如

{
  'additional_properties': {
    'properties': {
      'automaticOSUpgradeProperties': {
        'automaticOSUpgradeSupported': False
      }
    }
  },
  'id': '/Subscriptions/f03687b3-57b3-43c9-9734-6fb36e0de268/Providers/Microsoft.Compute/Locations/southeastasia/Publishers/Debian/ArtifactTypes/VMImage/Offers/debian-10/Skus/10-backports',
  'name': '10-backports',
  'location': 'southeastasia',
  'tags': None
}

使用 AWS 开发工具包非常简单:

boto3.client('ec2').describe_availability_zones()

根据我的测试,我们可以使用下面的代码来获取区域。详情请参考issue

from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute.models import ResourceSkuLocationInfo

AZURE_TENANT_ID= ''
AZURE_CLIENT_ID=''
AZURE_CLIENT_SECRET='' 
AZURE_SUBSCRIPTION_ID=''

credentials = ServicePrincipalCredentials(client_id=AZURE_CLIENT_ID,secret=AZURE_CLIENT_SECRET,tenant=AZURE_TENANT_ID)
compute_client = ComputeManagementClient(credentials,AZURE_SUBSCRIPTION_ID)
results=compute_client.resource_skus.list()
for result in results :
  
       if(result.resource_type=='virtualMachines' and result.locations[0].lower()==location.lower()):
          for r in result.location_info:
              for x in r.zones:
                  print('name'+result.name+' zone:' +x)
                  print('-----------------')

关于sdk的使用方法,请参考document and the actricle


更新

我们需要在代码中提供一个位置

from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute.models import ResourceSkuLocationInfo
def _match_location(l, locations):
    return next((x for x in locations if x.lower() == l.lower()), None)
AZURE_TENANT_ID= 'e4c9ab4e-bd27-40d5-8459-230ba2a757fb'
AZURE_CLIENT_ID='42e0d080-b1f3-40cf-8db6-c4c522d988c4'
AZURE_CLIENT_SECRET='pMbSCzttaDh=-WE@g*32TiX5hBcBhY2@' 
AZURE_SUBSCRIPTION_ID='e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68'
location='southeastasia' # the location you need to use
credentials = ServicePrincipalCredentials(client_id=AZURE_CLIENT_ID,secret=AZURE_CLIENT_SECRET,tenant=AZURE_TENANT_ID)
compute_client = ComputeManagementClient(credentials,AZURE_SUBSCRIPTION_ID)
results=compute_client.resource_skus.list()

for result in results :
  
       if(result.resource_type=='virtualMachines' and result.locations[0].lower()==location.lower()):
          for r in result.location_info:
              for x in r.zones:
                  print('name'+result.name+' zone:' +x)
                  print('-----------------')