0

I have this code that prints information related to VM. The output is in the class type. How can I convert it to a form so that I can understand it?

 from azure.common.credentials import ServicePrincipalCredentials
 from azure.mgmt.compute import ComputeManagementClient
    
 credential = ServicePrincipalCredentials(client_id='XXXX',
                                          secret='XXXX', tenant='XXXX')
 compute_client = ComputeManagementClient(
     credential, 'XXXX')
 for vm in compute_client.virtual_machines.list_all():
     print(vm.storage_profile)

I am getting output in the form. It is showing the class type of this output

<'azure.mgmt.compute.v2019_03_01.models.storage_profile_py3.StorageProfile'>

Hassan Turi
  • 334
  • 3
  • 14
  • 2
    Try `print(vm.storage_profile.as_dict())`. The method seems to be defined for its parent class, and that might get you something more readable. – baileythegreen Apr 05 '22 at 22:00
  • 1
    Does this answer your question? [How could I list Azure Virtual Machines using Python?](https://stackoverflow.com/questions/58925397/how-could-i-list-azure-virtual-machines-using-python) – Ecstasy Apr 06 '22 at 04:31
  • Hey @Hassan Turi, had it solved your problem else you can share more details so I can troubleshoot? – AjayKumarGhose May 07 '22 at 05:12

1 Answers1

1

It was printing class type because you are directly calling the variable without converting it. To do that You can create a new variable and assign the conversion to it and print that new variable. Or you can use directly in print statement as below,

Which is suggested by @baileythegreen as well,Thank you for your valuable insights posting the same in answer to help other community members.

CODE:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
    
 credential = ServicePrincipalCredentials(client_id='XXXX',
                                          secret='XXXX', tenant='XXXX')
 compute_client = ComputeManagementClient(
     credential, 'XXXX')
 for vm in compute_client.virtual_machines.list_all():
     print(vm.storage_profile.as_dict()) 

For more information please refer the below links:-

SO THREAD: Azure Python SDK - list VMs and generate custom JSON response

MS Q&A:- Printing list of Virtual Machines

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15