Jeff
Jeff

Reputation: 850

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.

Upvotes: 2

Views: 2367

Answers (2)

Shui shengbao
Shui shengbao

Reputation: 19195

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

Upvotes: 3

Sraw
Sraw

Reputation: 20214

You can list all resources inside given resource group by list_by_resource_group.

Then you will get a page container which contains GenericResource. Then that's easy to select what you need.

Or you can directly list all disks inside given resource group by list_by_resource_group for disk.

Upvotes: 2

Related Questions