簡體   English   中英

如何使用 Azure SDK 解析 VirtualMachinePaged object?

[英]How to parse VirtualMachinePaged object using Azure SDK for Python?

我正在嘗試使用 Azure SDK 為 Python 獲取資源組中的虛擬機列表。 我使用所有必需的 Azure 工具配置了我的 Visual Studio 代碼。 我創建了一個 function 並使用下面的代碼來獲取 VM 列表。

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>

我真的很想得到實際的 object,我不確定如何解析它。 有任何想法嗎?

由於它返回一個集合,您需要使用 Use for 循環,您可以執行以下操作

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

VirtualMachinePaged包含一個VirtualMachine類型的 object 的集合。 您可以在此處查看 class 的源代碼: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/compute/azure-mgmt-compute/azure/mgmt2_11/v2019 /models/_models.py

從這個鏈接,這里是屬性列表:

{
    'id': {'key': 'id', 'type': 'str'},
    'name': {'key': 'name', 'type': 'str'},
    'type': {'key': 'type', 'type': 'str'},
    'location': {'key': 'location', 'type': 'str'},
    'tags': {'key': 'tags', 'type': '{str}'},
    'plan': {'key': 'plan', 'type': 'Plan'},
    'hardware_profile': {'key': 'properties.hardwareProfile', 'type': 'HardwareProfile'},
    'storage_profile': {'key': 'properties.storageProfile', 'type': 'StorageProfile'},
    'additional_capabilities': {'key': 'properties.additionalCapabilities', 'type': 'AdditionalCapabilities'},
    'os_profile': {'key': 'properties.osProfile', 'type': 'OSProfile'},
    'network_profile': {'key': 'properties.networkProfile', 'type': 'NetworkProfile'},
    'diagnostics_profile': {'key': 'properties.diagnosticsProfile', 'type': 'DiagnosticsProfile'},
    'availability_set': {'key': 'properties.availabilitySet', 'type': 'SubResource'},
    'virtual_machine_scale_set': {'key': 'properties.virtualMachineScaleSet', 'type': 'SubResource'},
    'proximity_placement_group': {'key': 'properties.proximityPlacementGroup', 'type': 'SubResource'},
    'priority': {'key': 'properties.priority', 'type': 'str'},
    'eviction_policy': {'key': 'properties.evictionPolicy', 'type': 'str'},
    'billing_profile': {'key': 'properties.billingProfile', 'type': 'BillingProfile'},
    'host': {'key': 'properties.host', 'type': 'SubResource'},
    'provisioning_state': {'key': 'properties.provisioningState', 'type': 'str'},
    'instance_view': {'key': 'properties.instanceView', 'type': 'VirtualMachineInstanceView'},
    'license_type': {'key': 'properties.licenseType', 'type': 'str'},
    'vm_id': {'key': 'properties.vmId', 'type': 'str'},
    'resources': {'key': 'resources', 'type': '[VirtualMachineExtension]'},
    'identity': {'key': 'identity', 'type': 'VirtualMachineIdentity'},
    'zones': {'key': 'zones', 'type': '[str]'},
}

對於 Python 3,代碼可以在這里找到: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/compute/azure-mgmt-compute/azure/mgmt/compute/ /models/_models_py3.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM