1

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.

Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
Jeff
  • 760
  • 1
  • 12
  • 26

2 Answers2

2

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
Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
  • many thanks. this is the one i have been looking for. by the way, what advise would you give me on using the doc page's search, i find it hard to use it because the search filter is confusing. – Jeff Feb 28 '18 at 16:45
1

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.

Sraw
  • 18,892
  • 11
  • 54
  • 87
  • thanks Sraw, i'm just curious how you find your way into ms documentation, when honestly i get lost using it because its hard to search for specific keywords. – Jeff Feb 28 '18 at 16:47
  • 1
    @allenj If you understand the design philosophy of azure resources structure, it will become much easier to search. For each types of resource, there will be a management client, you can first find that client and then check what you want to manage. For example, https://learn.microsoft.com/zh-cn/python/api/azure.mgmt.compute.computemanagementclient?view=azure-python is the client of `compute` resources. And `disk` is one of `compute` resources. That is the same if you want to manage other resources such as managing storage account by `StorageManagementClient`. – Sraw Mar 01 '18 at 02:04
  • thanks man, will do that, ill practice searching using their documentation, and hopefully understand my way thru it – Jeff Mar 01 '18 at 06:16