【问题标题】:Azure Python SDK get zone infoAzure Python SDK 获取区域信息
【发布时间】:2020-04-20 08:01:59
【问题描述】:

有人知道如何使用 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()

【问题讨论】:

  • 关于问题,请尝试使用代码results=compute_client.resource_skus.list()。更多详情请参考docs.microsoft.com/en-us/python/api/azure-mgmt-compute/…
  • 您还有其他顾虑吗?如果您没有其他顾虑,可以请accept the answer吗?它可能会帮助更多的人。
  • 嗨,Jim 感谢您的回答,但代码不太好用(NameError: name 'location' is not defined),您是说这个吗? if(result.resource_type=='virtualMachines' and result.locations[0].lower() == result.location_info[0].location.lower() ): result.location_info[0].location 始终等于 result.locations[0] ,因此无需归档。如果有人感兴趣,我还使用len(result.location_info[0].zones) > 0 获取冗余区域,并使用result.location_info[0].location.lower().endswith('euap') 过滤早期更新访问计划图像。
  • 我犯了一个错误。我们需要手动提供位置。对此感到抱歉。更多详情请参考我的更新。

标签: python azure azure-virtual-machine


【解决方案1】:

根据我的测试,我们可以使用下面的代码来获取区域。更多详情请参考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的使用方法请参考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('-----------------')

              

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2018-10-14
    • 1970-01-01
    • 2017-10-23
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多