Azure Python SDK 错误

Jay*_*Jay 4 python api azure

我已经编写了 python 代码来使用 azure-python sdk 获取订阅的 azure 资源,列出资源组内所有资源的功能不起作用,这在一周前工作正常,可能是微软改变了他们的 api? ? 我收到一个属性错误,AttributeError: 'ResourceGroupsOperations' 对象没有属性 'list_resources'

请找到下面的代码,

from azure.common.credentials import ServicePrincipalCredentials  
from azure.mgmt.resource.resources import ResourceManagementClient   
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
subscription_id = ''
credentials = ServicePrincipalCredentials(
client_id = '',
secret = '',
tenant = '',
)
resource_client = ResourceManagementClient(credentials,subscription_id)
resource_client.providers.register('Microsoft.Batch')
def get_resources():
for rg in resource_client.resource_groups.list():
for item in resource_client.resource_groups.list_resources(rg.name):
print "%s,%s,%s,%s,"%(item.name,item.type,item.location,rg.name)
get_resources()
Run Code Online (Sandbox Code Playgroud)

请帮忙解决这个问题!提前致谢 !

Jay*_*ong 9

只是一个总结,您可以list_resources2017-05-04 中SDK 源代码版本声明中找到该方法的描述。

resource_groups.list_resources has been moved to resources.list_by_resource_group
Run Code Online (Sandbox Code Playgroud)

Python SDK 升级应该是您出现问题的原因。

请按如下方式修改您的代码,它将起作用。

from azure.common.credentials import ServicePrincipalCredentials  
from azure.mgmt.resource.resources import ResourceManagementClient   
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
subscription_id = ''
credentials = ServicePrincipalCredentials(
client_id = '',
secret = '',
tenant = '',
)
resource_client = ResourceManagementClient(credentials,subscription_id)
resource_client.providers.register('Microsoft.Batch')
def get_resources():
  for rg in resource_client.resource_groups.list():
    for item in resource_client.resources.list_by_resource_group(rg.name):
      print "%s,%s,%s,%s,"%(item.name,item.type,item.location,rg.name)
get_resources()
Run Code Online (Sandbox Code Playgroud)