1

List the storage accounts that are not having containers,tables,queues,fileshares using python sdk on azure automation account python version 3.8. Below is the script which im trying to get the same but getting error **as containers = list(storage_client.blob_containers.list(resource_group_name, account_name))TypeError: 'ListContainerItems' object is not iterable **

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.storage import StorageManagementClient
client_id = 'XXX'
tenant_id = 'XXX'
client_secret = 'XXX'
subscription_id = ('subscription_id')
credentials = ServicePrincipalCredentials(client_id, client_secret, tenant=tenant_id)
from azure.mgmt.storage import StorageManagementClient
storage_client = StorageManagementClient(credentials, subscription_id)

# Get a list of all storage accounts in the subscription
storage_accounts = storage_client.storage_accounts.list()

# Loop through each storage account and check if it has containers, tables, queues, or file shares
for account in storage_accounts:
    account_name = account.name
    resource_group_name = account.id.split("/")[4]
    
    # Check if the storage account has containers
    containers = list(storage_client.blob_containers.list(resource_group_name, account_name))
    #print(containers)
    if containers:
        continue

    # Check if the storage account has tables
    tables = list(storage_client.table.list(resource_group_name, account_name))
    if tables:
        continue

    # Check if the storage account has queues
    queues = list(storage_client.queue.list(resource_group_name, account_name))
    if queues:
        continue

    # Check if the storage account has file shares
    file_shares = list(storage_client.file_shares.list(resource_group_name, account_name))
    if file_shares:
        continue

    # If none of the above resources are found, print the storage account name as orphan
    print(f"Orphaned Storage account name {account_name}.")
DSH
  • 69
  • 1
  • 8

1 Answers1

1

After reproducing from my end, I could get this done using the below code.

#!/usr/bin/env python3

from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.storage import StorageManagementClient
client_id = '<CLIENT_ID>'
tenant_id = '<TENANT_ID>'
client_secret = '<CLIENT_SECRET>'
subscription_id = ('<SUBSCRIPTION_ID>')

credentials = ClientSecretCredential(
    tenant_id=tenant_id,
    client_id=client_id,
    client_secret=client_secret
)

storage_client = StorageManagementClient(credentials, subscription_id)

# Get a list of all storage accounts in the subscription
storage_accounts = storage_client.storage_accounts.list()

# Loop through each storage account and check if it has containers, tables, queues, or file shares
for account in storage_accounts:
    account_name = account.name
    resource_group_name = account.id.split("/")[4]
    
    # Check if the storage account has containers
    containers = list(storage_client.blob_containers.list(resource_group_name, account_name))
    tables = list(storage_client.table.list(resource_group_name, account_name))
    queues = list(storage_client.queue.list(resource_group_name, account_name))
    file_shares = list(storage_client.file_shares.list(resource_group_name, account_name))
    
    if(len(containers) and len(list(tables)) and len(queues) and len(file_shares) == 0):
        print(f"Orphaned Storage account name {account_name}.")
        
    else:
        print("Not a Orphaned Storage")

Below are the python modules that I have installed.

enter image description here

Results:

enter image description here

SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
  • Thanks @SwethaKandikonda May i know how to install python modules in Azure Automation runbook for azure_identity – DSH Mar 08 '23 at 05:27
  • @DSH, I have added corresponding .whl module files under python packages https://i.imgur.com/YPCH1jr.png – SwethaKandikonda Mar 08 '23 at 05:33
  • if possible Could you please share the .whl module file azure_identity for reference. – DSH Mar 08 '23 at 05:38
  • sure @DSH, you can get it from https://pypi.org/project/azure-identity/#files:~:text=azure_identity%2D1.12.0%2Dpy3%2Dnone%2Dany.whl%20(135.5%20kB%20view%20hashes) – SwethaKandikonda Mar 08 '23 at 05:41
  • my .whl module file having this line pip install azure-identity when im importing its failing may i know the mistake here the file im uploading should have proper? – DSH Mar 08 '23 at 06:26
  • refer this image for the configurations I used while installing/adding the modules https://i.imgur.com/1Nq1Xpb.png – SwethaKandikonda Mar 08 '23 at 08:20