Identify Storage Accounts that are not having any resources like tables, containers.. using Python Sdk

huangapple go评论71阅读模式
英文:

Identify Storage Accounts that are not having any resources like tables, containers.. using Python Sdk

问题

以下是翻译好的代码部分:

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)

# 获取订阅中的所有存储帐户的列表
storage_accounts = storage_client.storage_accounts.list()

# 遍历每个存储帐户并检查是否有容器、表、队列或文件共享
for account in storage_accounts:
    account_name = account.name
    resource_group_name = account.id.split("/")[4]
    
    # 检查存储帐户是否有容器
    containers = list(storage_client.blob_containers.list(resource_group_name, account_name))
    #print(containers)
    if containers:
        continue

    # 检查存储帐户是否有表
    tables = list(storage_client.table.list(resource_group_name, account_name))
    if tables:
        continue

    # 检查存储帐户是否有队列
    queues = list(storage_client.queue.list(resource_group_name, account_name))
    if queues:
        continue

    # 检查存储帐户是否有文件共享
    file_shares = list(storage_client.file_shares.list(resource_group_name, account_name))
    if file_shares:
        continue

    # 如果上述资源都未找到,打印存储帐户名称为孤立的
    print(f"Orphaned Storage account name {account_name}.")
英文:

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}.")

</details>


# 答案1
**得分**: 1

在我这里复制后,我可以使用以下代码完成此操作。

```python
#!/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)

# 获取订阅中所有存储帐户的列表
storage_accounts = storage_client.storage_accounts.list()

# 循环遍历每个存储帐户并检查其是否有容器、表、队列或文件共享
for account in storage_accounts:
    account_name = account.name
    resource_group_name = account.id.split("/")[4]
    
    # 检查存储帐户是否有容器
    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"孤立的存储帐户名称 {account_name}。")
        
    else:
        print("不是孤立的存储")

以下是我已安装的Python模块。

Identify Storage Accounts that are not having any resources like tables, containers.. using Python Sdk

结果:

Identify Storage Accounts that are not having any resources like tables, containers.. using Python Sdk

英文:

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 = &#39;&lt;CLIENT_ID&gt;&#39;
tenant_id = &#39;&lt;TENANT_ID&gt;&#39;
client_secret = &#39;&lt;CLIENT_SECRET&gt;&#39;
subscription_id = (&#39;&lt;SUBSCRIPTION_ID&gt;&#39;)

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(&quot;/&quot;)[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&quot;Orphaned Storage account name {account_name}.&quot;)
        
    else:
        print(&quot;Not a Orphaned Storage&quot;)

Below are the python modules that I have installed.

Identify Storage Accounts that are not having any resources like tables, containers.. using Python Sdk

Results:

Identify Storage Accounts that are not having any resources like tables, containers.. using Python Sdk

huangapple
  • 本文由 发表于 2023年3月7日 17:38:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定