python - 如何使用 Azure SDK for Python 解析 VirtualMachinePaged 对象?

标签 python azure visual-studio-code azure-functions azure-sdk-python

我正在尝试使用 Azure SDK for Python 获取资源组中的虚拟机列表。我使用所有必需的 Azure 工具配置了 Visual Studio 代码。我创建了一个函数并使用下面的代码来获取虚拟机列表。

import os
import random
import string

from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.resource import ResourceManagementClient

def main():

    SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
    GROUP_NAME = "testgroupx"
    VIRTUAL_MACHINE_NAME = "virtualmachinex"
    SUBNET_NAME = "subnetx"
    INTERFACE_NAME = "interfacex"
    NETWORK_NAME = "networknamex"
    VIRTUAL_MACHINE_EXTENSION_NAME = "virtualmachineextensionx"

resource_client = ResourceManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    network_client = NetworkManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    compute_client = ComputeManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )

vm = compute_client .virtual_machines.list(
        'RGName'
    )

print("Get virtual machine:\n{}", vm)

当我查看日志时,我看到下面的打印响应。

<azure.mgmt.compute.v2019_12_01.models._paged_models.VirtualMachinePaged object at 0x0000024584F92EC8>

我真的想获取实际的对象,我不知道如何解析它。有什么想法吗?

最佳答案

由于它返回一个集合,你需要使用 for 循环,你可以这样做

for vm in compute_client .virtual_machines.list('RGName'):
  print("\tVM: {}".format(vm.name))

关于python - 如何使用 Azure SDK for Python 解析 VirtualMachinePaged 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62217585/

相关文章:

visual-studio-code - VSCode : keep focus in Source Control Viewlet when opening a file

python - TypeError : an integer is required (got type datetime. datetime) 尝试将字典中的日期时间对象转换为字符串时

python - 返回名称包含字符串的所有文件的函数

python - 尝试使用具有多行及其属性的 Bokeh Widget Checkbox 组时出现值错误 "visible"

azure - 如何将个人帐户下创建的Azure应用程序移动到目录?

visual-studio-code - VS Code 可以使用键盘快捷键输入文本吗?

flutter - 开发 flutter 时是否可能在 Visual Studio Code 中禁用设备开关?

python - 防止 Django 模板中的换行符分割名称列表

azure - 修复 Azure DevOps 上的应用程序部署错误

azure - 如何使用powershell ARM查找azure存储帐户中有多少文件共享