Azure Python SDK获取区域信息

时间:2020-01-01 04:57:38

标签: python azure azure-virtual-machine

有人知道如何使用Python SDK获取区域信息吗?我发现this Technet帖子向您展示了如何使用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()不返回区域信息,仅返回例如图像

{
  '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()

1 个答案:

答案 0 :(得分:0)

根据我的测试,我们可以使用以下代码来获取区域。有关更多详细信息,请参阅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('-----------------')

enter image description here 关于sdk的使用方法,请参阅documentactricle


更新

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

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('-----------------')