'How to traverse through azure subscriptions?

In the code below, I want to show blob properties by traversing through azure subscriptions. But I'm facing an error 'SUBSCRIPTION' object is not subscriptable.

from azure.identity import AzureCliCredential
import pandas as pd
from azure.mgmt import subscription
from azure.mgmt.storage import StorageManagementClient
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
credential = AzureCliCredential()
subscription_client = subscription.SubscriptionClient(credential = credential)

dataset: list = []
for subscription in subscription_client.subscriptions.list():
    storage_client = StorageManagementClient(credential, subscription['subscription_id'])
    data: dict = data.as_dict()
    data_vals: dict = {
        'resource_id': data.get('id'),
        'subscription_id': subscription['subscription_id'],
        'subscription_name': subscription['subscription_name'],
        'resource_group': data.get('id').split('/')[4],
        'resource_name': data.get('name'),
        'location': data.get('location')        
    }
    print(f"\n Storage Account Name = {data_vals.get('resource_name')}")


Solution 1:[1]

If the error is at subscription['subscription_id'],Please check the correct way to get the subscription id.

The subscription is imported module itself and pplease check if is correct to get subscription id as an argument . Try to get subscription id from data.subscription_id or data.get('subscription_id') or try with subscription.subscription_id

or try to improt subscriptionClient

 from azure.identity import AzureCliCredential
    from azure.mgmt.resource import SubscriptionClient
    
    credential = AzureCliCredential()
    subscription_client = SubscriptionClient(credential)
    
dataset: list = []
    for data in subscription_client.subscriptions.list():

and then try to get subscription id from dict data id data.get('subscription_id')

References:

  1. How to authenticate Python applications with Azure services (alternate methods) | Microsoft Docs
  2. How to get all resources with details from Azure subscription via Python - Stack Overflow
  3. python - List subscriptions for a given Azure account - Stack Overflow

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kavyasaraboju-MT