Azure Python SDK not able to consume output of list_of_vms with azure-mgmt-compute==20.0.0

Sanvar Inamdar 21 Reputation points
2021-06-17T10:36:57.477+00:00

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)

Azure Cloud Services
Azure Cloud Services
An Azure platform as a service offer that is used to deploy web and cloud applications.
689 questions
0 comments No comments
{count} votes

Accepted answer
  1. prmanhas-MSFT 17,891 Reputation points Microsoft Employee
    2021-06-18T13:46:15.093+00:00

    @Sanvar Inamdar Apologies for the delay in response and all the inconvenience caused because of the issue.

    As mentioned here :

    The Azure libraries for Python are currently being updated to share common cloud patterns such as authentication protocols, logging, tracing, transport protocols, buffered responses, and retries.

    This would change the Authentication mechanism a bit as well. In the older version, ServicePrincipalCredentials in azure.common was used for authenticating to Azure and creating a service client.

    In the newer version, the authentication mechanism has been re-designed and replaced by azure-identity library in order to provide unified authentication based on Azure Identity for all Azure SDKs. Run pip install azure-identity to get the package.

    In terms of code, what then was:

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.compute import ComputeManagementClient
    credentials = ServicePrincipalCredentials(
    client_id='xxxxx',
    secret='xxxxx',
    tenant='xxxxx'
    )
    compute_client = ComputeManagementClient(
    credentials=credentials,
    subscription_id=SUBSCRIPTION_ID
    )

    is now:

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

    credential = ClientSecretCredential(
    tenant_id='xxxxx',
    client_id='xxxxx',
    client_secret='xxxxx'
    )

    compute_client = ComputeManagementClient(
    credential=credential,
    subscription_id=SUBSCRIPTION_ID
    )
    You can then use the list_all method with compute_client to list all VMs as usual:

    List all Virtual Machines in the specified subscription

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

    list_virtual_machines()

    From your mentioned code snippet it seems like you are using Service Principal Credentials.

    Hope it helps!!!

    Please "Accept as Answer" if it helped so it can help others in community looking for help on similar topics.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.