Hassan Turi
Hassan Turi

Reputation: 354

How to convert these objects to valuable information

I have this code to list down the details of Azure Virtual Machines, but I am getting output in this form. How can I change (azure.mgmt.compute.v2019_03_01.models.storage_profile_py3) this kind of output to valuable information.

'name': 'Linux', 'type': 'Microsoft.Compute/virtualMachines', 'location': 'eastus', 'tags': None, 'plan': None, 'hardware_profile': <azure.mgmt.compute.v2019_03_01.models.hardware_profile_py3.HardwareProfile object at 0x0000023C8DCFB8B0>, 'storage_profile': <azure.mgmt.compute.v2019_03_01.models.storage_profile_py3.StorageProfile object at 0x0000023C8DCFB0A0>, 'additional_capabilities': None, 'os_profile': <azure.mgmt.compute.v2019_03_01.models.os_profile_py3.OSProfile object at 0x0000023C8DCFB4C0>, 'network_profile': <azure.mgmt.compute.v2019_03_01.models.network_profile_py3.NetworkProfile object at 0x0000023C8DCFB910>, 'diagnostics_profile': <azure.mgmt.compute.v2019_03_01.models.diagnostics_profile_py3.DiagnosticsProfile object at 0x0000023C8DCFB9D0>, 'availability_set': None, 'provisioning_state': 'Succeeded', 'instance_view': None, 'license_type': None, 'vm_id': 'ef2a71f9-34ac-42d4-ab64-74a18616e635', 'resources': None, 'identity': None, 'zones': None

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient

credential = ServicePrincipalCredentials(client_id='6ba20ca0-9eac-47df-a48a-69a935cf3841',
                                         secret='Kqh7Q~gxKhu52LTHbJyGiWGEBRff2Zcfw3O3K', tenant='75df096c-8b72-48e4-9b91-cbf79d87ee3a')
compute_client = ComputeManagementClient(
    credential, '8abf0e0e-3787-4d52-92a1-98258b7561cb')

def list_virtual_machines():
    for vm in compute_client.virtual_machines.list_all():
        print(vm)
list_virtual_machines()

Upvotes: 0

Views: 175

Answers (1)

Delliganesh Sevanesan
Delliganesh Sevanesan

Reputation: 4776

You can use the below format to get valuable Information about VM

def list_virtual_machines():
    for vm in compute_client.virtual_machines.list_all():
        print(vm.name)
        print(vm.type)
        print(vm.location)
        print(vm.tags)
        ...
list_virtual_machines()

Or

Use compute_client.virtual_machines.get to get information about the virtual machine.

Example Code follows

vm = compute_client.virtual_machines.get(GROUP_NAME, VM_NAME, expand='instanceView') 
print("hardwareProfile") 
print(" vmSize: ", vm.hardware_profile.vm_size) 
print("\nstorageProfile") 
print(" imageReference") 
print(" publisher: ", vm.storage_profile.image_reference.publisher)
......

Upvotes: 0

Related Questions