首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure Python获取区域信息

Azure Python获取区域信息
EN

Stack Overflow用户
提问于 2020-01-01 04:57:38
回答 1查看 507关注 0票数 0

有人知道如何使用Python获取区域信息吗?我找到了 technet帖子,它向您展示了如何使用powershell按区域转储受支持的计算机类型,而Azure命令行工具等效如下:

代码语言:javascript
复制
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()不返回区域信息,只返回图像。

代码语言:javascript
复制
{
  '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非常容易:

代码语言:javascript
复制
boto3.client('ec2').describe_availability_zones()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-02 01:13:57

根据我的测试,我们可以使用下面的代码来获得区域。有关更多细节,请参阅问题

代码语言:javascript
复制
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,请参阅文档穗状花序

更新

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

代码语言:javascript
复制
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('-----------------')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59550061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档