0
votes

Anyone know how to get zone info using the Python SDK? I found this technet post that shows you how to dump supported machine types by zone using powershell, and the Azure command line tools equivalent seems to be as follows:

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:~$

But I haven't found the right SDK method yet after trawling through the docs for quite a while.

compute_client.virtual_machine_images.list_skus() does not return zone info, just image e.g.

{
  '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
}

This is super easy using the AWS SDK:

boto3.client('ec2').describe_availability_zones()
1
Regarding the issue, please try to use the code results=compute_client.resource_skus.list(). For more details, please refer to docs.microsoft.com/en-us/python/api/azure-mgmt-compute/…Jim Xu
Do you have any other concerns? If you have no other concerns, could you please accept the answer? It may help more people.Jim Xu
Hi Jim thanks for answering but the code doesn't quite work (NameError: name 'location' is not defined), did you mean this? if(result.resource_type=='virtualMachines' and result.locations[0].lower() == result.location_info[0].location.lower() ): result.location_info[0].location is always equal to result.locations[0] anyway so no need to filer. I am also using len(result.location_info[0].zones) > 0 to get redundant regions, and result.location_info[0].location.lower().endswith('euap') to filter Early Updates Access Program images if anyone is interested.woOt
I made a mistake. We need to provide a location manually. Sorry for that. For more details, please refer to my update.Jim Xu

1 Answers

0
votes

According to my test, we can use the following code to get the zones. For more details, please refer to the 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 Regarding how to use the sdk, please refer to the document and the actricle


Update

We need to provide a location in the code

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