pythonazuresdkazure-virtual-machineazure-managed-disk

azure-sdk-for-python: get list of managed disks from a specified resource group


Its been a tough day. I am struggling to find how to get the list of managed disks by a specified resource group in azure-sdk-for-python. Searched all possible solutions, but nothing gets near what i was looking for. Hand salute for ms who did well its documentation, yes sir! Successfully made me so frustrated.

I can get the list of managed disks if i loop through the VMs, but it may not be the best solution, as managed disks can be attached/detached, and i won't be able to get those detached ones.

Suggestions are much appreciated.


Solution

  • You could use the following script to list disks in a resource group.

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.compute import ComputeManagementClient
    from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient
    
    # Tenant ID for your Azure Subscription
    TENANT_ID = ''
    
    # Your Service Principal App ID
    CLIENT = ''
    
    # Your Service Principal Password
    KEY = ''
    
    credentials = ServicePrincipalCredentials(
        client_id = CLIENT,
        secret = KEY,
        tenant = TENANT_ID
    )
    
    subscription_id = ''
    
    compute_client = ComputeManagementClient(credentials, subscription_id)
    
    rg = 'shuilinux'
    
    disks = compute_client.disks.list_by_resource_group(rg)
    for disk in disks:
        print disk