Search code examples
pythonazureazure-virtual-machineazure-sdkazure-sdk-python

How to use nextLink property in Azure Python SDK while listing all VMs


I am trying to list all virtual machines in a subscription in Azure using the code given here:

 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 )

This works fine. It is listing all the vms for the first page. However, how do I get the next_link property and consume the next page of VMs? What would be the syntax? In the docs: https://learn.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2019_07_01.models.virtualmachinelistresult?view=azure-python

it says:

next_link str

Required

The URI to fetch the next page of VMs. Call ListNext() with this URI to fetch the next page of Virtual Machines.

I am just wondering what the syntax would look like to consume the next_link property and make the ListNext() call?


Solution

  • You don't need to care about nextLink, the object returned by listAll() is an iterator that will use nextLink for you automatically as you can continue to iterate. If you want all VMs at once in memory, you can do something like list(vms) which while converting the iterator to an actual list, will read all pages at once and put everything in memory.

    If you have additional questions, feel free to open an issues on our Github repo: https://github.com/Azure/azure-sdk-for-python/issues

    (I'm the engineering manager of the Azure SDK for Python at Microsoft)