Python SDK Azure 资源图响应不可迭代

标签 python azure

我正在尝试使用 Azure 函数中的 Azure 资源图 python SDK 查询 Azure 订阅中的资源。事实上它使用 Functions 运行时并不重要,代码也无法在本地运行。 查询本身效果很好。但是,我想在后续步骤中使用结果,这就是我遇到的问题。

    credentials, *_ = get_azure_cli_credentials()

    subscription_client = SubscriptionClient(credentials)
    subs = [sub.as_dict() for sub in subscription_client.subscriptions.list()]
    subs_list = []
    for sub in subs:
        subs_list.append(sub.get('subscription_id'))

    client = rg.ResourceGraphClient(credentials)

    request = QueryRequest(subscriptions=subs_list, query="resources | where type == 'microsoft.storage/storageaccounts'| where properties.supportsHttpsTrafficOnly == 'false'")

    response = client.resources(request)
    logging.info(response)

我尝试了以下方法:

for resource in response:
    logging.info(resource)

此操作失败,并出现异常:类型错误:“QueryResponse”对象不可迭代。 供引用:https://learn.microsoft.com/en-us/python/api/azure-mgmt-resourcegraph/azure.mgmt.resourcegraph.models.queryresponse?view=azure-python

logging.info(response['data'])

此操作失败,并出现异常:类型错误:“QueryResponse”对象不可下标。 现在有人知道如何使用此响应吗? 谢谢!

最佳答案

可以通过传递QueryRequestOptions来简化响应格式以匹配资源JSON:

client = ResourceGraphClient(credentials)

options = QueryRequestOptions(
    result_format=ResultFormat.object_array
)

request = QueryRequest(subscriptions=subs_list, query="<query>", options=options)

response = client.resources(request)

for resource in response.data:
    logging.info(resource)

关于Python SDK Azure 资源图响应不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61694497/

相关文章:

python - 减少 Dart 中包装对象的数量

python - 根据谓词从列表中删除元素

python - Gensim word2vec 增强或合并预训练向量

azure - 在 Azure 数据工厂中的表之间转换数据类型

python - 为什么设置一个元素值会更改 Python 二维矩阵中其他元素的值

python - 如何调试在 Python 中使用的 Windows DLL?

azure - DocumentDB 模拟器作为 Windows 服务

azure - 如何使用 Terraform 的文件配置程序从本地计算机复制到虚拟机?

json - 将 oAuth token 与 Azure MobileServiceClient.login() 结合使用

azure - Windows Azure 上的 JavaEE 技术堆栈?