0

I am using azure python sdk for resource management. below snippet was working with Azure -> azure-mgmt-compute==12.0.0 after upgrade to azure-mgmt-compute==20.0.0 below snippet is not working

creds = ServicePrincipalCredentials(client_id=client_id, secret=secret, tenant=tenant_id, **kwargs)

compute_client =  ComputeManagementClient(creds,
                                   subscription_id,
                                   base_url='https://management.azure.com')

paged_iter = compute_client.virtual_machines.list_all(raw=True)

output = []
paged_iter.get(paged_iter.next_link)
while True:
    chunk = json.loads(paged_iter.raw.response.content)
    if 'nextLink' in chunk:
        paged_iter.get(chunk['nextLink'])
    else:
        break
resp = {'value': output}
print(resp)

after upgrade getting error AttributeError: 'ItemPaged' object has no attribute 'get'

Please help to understand how to consume output of compute_client.virtual_machines.list_all(raw=True)

  • Why did you wanted to page manually while you get a page iterator that does all for that you? Why doing to raw=True in the first place? If there is a good reason why the iterator was not doing what you wanted it to do, please ask a question in https://github.com/Azure/azure-sdk-for-python/issues – Laurent Mazuel Jun 17 '21 at 18:25
  • How's going? Has your issue got resolved? – Stanley Gong Jun 18 '21 at 14:17
  • Thanks, @StanleyGong. Yes, issue got resolved able to iterate over VMs. For raw json should I use ```__dict__``` ? – Sanvar Inamdar Jun 19 '21 at 04:48
  • @LaurentMazuel I have to filter the result based on location or some other params, do you have any suggestion for filtering ? Also, ``` raw=True``` was used for getting raw json which was generically getting used at my end, you can put your thoughts for any other best practices or suggestions . -Thanks – Sanvar Inamdar Jun 19 '21 at 04:58
  • @SanvarInamdar May I know which way you want to filter VMS? By name,resource group or something others? – Stanley Gong Jun 19 '21 at 06:51
  • @StanleyGong I want to filter based on location, by name & resource group also will help. – Sanvar Inamdar Jun 19 '21 at 17:16
  • @SanvarInamdar try `computer_client.virtual_machines.get('','')` to get a vm by resource name and vm name and try `computer_client.virtual_machines.list('')` get vms in a resource group – Stanley Gong Jun 20 '21 at 01:11
  • @StanleyGong how can we get output of ```computer_client.virtual_machines.list_all()``` in json, I need complete model mapping currently after converting to dict using ```computer_client.virtual_machines.list_all().__dict__``` some of the params are still in object format like ``` hardware_profile': ``` instead of this I want value of hardware profile. How can I do complete model mapping ? – Sanvar Inamdar Jun 22 '21 at 08:28

1 Answers1

0

Try this:

from azure.mgmt.compute import ComputeManagementClient
from azure.identity import ClientSecretCredential

credentials = ClientSecretCredential(
   client_id='',
   client_secret='',
   tenant_id=''
)

subID= ''

computer_client = ComputeManagementClient(credentials,subID)
vms = computer_client.virtual_machines.list_all()

for vm in vms:
   print( vm.name )

Result:

enter image description here enter image description here

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16