Skip to main content
 首页 » 编程设计

python之Azure Python SDK 错误

2024年08月19日36jiqing9006

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

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

请您参考如下方法:

只是一个总结,您可以在 2017-05-04 中从 SDK source code version statement 中找到对 list_resources 方法的描述.

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