【问题标题】:Azure Python SDK ErrorAzure Python SDK 错误
【发布时间】:2018-04-23 08:59:03
【问题描述】:

我已经编写了 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()

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

【问题讨论】:

    标签: python api azure


    【解决方案1】:

    只是一个总结,你可以找到list_resources方法的描述已经在2017-05-04SDK source code version statement中删除。

    resource_groups.list_resources has been moved to resources.list_by_resource_group
    

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

    【讨论】:

    • @user2795938 没关系。你可以在论坛上标记答案供其他人参考。祝你有美好的一天!
    • 完成!祝你有美好的一天。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多