简体   繁体   中英

How to parse VirtualMachinePaged object using Azure SDK for Python?

I am trying to get list of VMs in a Resource Group using Azure SDK for Python. I configured my Visual Studio code with all the required Azure Tools. I created a function and used below code to get List of VMs.

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)

When I see the logs, I see below as the print response.

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

I am really trying to get the actual object, I am not sure how can I parse it. Any ideas?

Since it returns a collection you need to use Use for loop, You can do something like this

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

VirtualMachinePaged contains a collection of an object of type VirtualMachine . You can see the source code of that class here: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/compute/azure-mgmt-compute/azure/mgmt/compute/v2019_12_01/models/_models.py .

From this link, here're the list of attributes:

{
    '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]'},
}

For Python 3, the code can be found here: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/compute/azure-mgmt-compute/azure/mgmt/compute/v2019_12_01/models/_models_py3.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM