List subscriptions for a given Azure account
Asked Answered
L

4

9

I'm trying to list the subscriptions in an Azure account using azure-python-sdk.

I have followed this link in documentation.

https://learn.microsoft.com/en-us/python/api/azure-mgmt-subscription/azure.mgmt.subscription.operations.subscriptionsoperations?view=azure-python#list-custom-headers-none--raw-false----operation-config-


from azure.mgmt.subscription import SubscriptionClient
from msrestazure.azure_active_directory import UserPassCredentials

credentials = UserPassCredentials(username='xxxx', password='xxxx')
sub_client = SubscriptionClient(credentials)
subs = [sub.as_dict() for sub in sub_client.subscriptions.list()]
print(subs)

It is supposed to return a list of subscriptions. However, I see only empty list returned every time I try the above code. Can anybody help?

Longford answered 28/5, 2019 at 3:54 Comment(0)
V
3

Try this code,

def list_subscriptions():
    try:
        sub_client = get_client_from_cli_profile(SubscriptionClient)
    except CLIError:
        logger.info("Not logged in, running az login")
        _run_az_cli_login()
        sub_client = get_client_from_cli_profile(SubscriptionClient)

    return [["Subscription_name", "Subscription ID"]] + [
        [sub.display_name, sub.subscription_id]
        for sub in sub_client.subscriptions.list()
    ]

You can find the handy tool from here

Venator answered 28/5, 2019 at 4:5 Comment(1)
File "..............\Lib\site-packages\azure\common\cloud.py", line 24, in get_cli_active_cloud raise ImportError( ImportError: The public API of azure-cli-core has been deprecated starting 2.21.0, and this method no longer can return a cloud instance. If you want to use this method, you need to install 'azure-cli-core<2.21.0'. You may corrupt data if you use current CLI and old azure-cli-core.Socialism
F
3

I have a similar problem, so I have used AzureCliCredential and it simply worked.

The code is this:

def subscription_list():
    credential = AzureCliCredential()
    subscription_client = SubscriptionClient(credential)
    sub_list = subscription_client.subscriptions.list()
    column_width = 40

    print("Subscription ID".ljust(column_width) + "Display name")
    print("-" * (column_width * 2))
    for group in list(sub_list):
        print(f'{group.subscription_id:<{column_width}}{group.display_name}')

Before trying this code, you have to log to Azure through the command line in your dev environment.

Frohman answered 26/11, 2021 at 0:11 Comment(0)
M
1

If the list is empty and you get not exception, it's likely your credentials are correct (no exception), but your user doesn't have access to subscriptions (no permissions)

In the Azure portal, in the subscription panel you have a button "Access control (IAM)" to define what users are allowed to a given subscription. https://learn.microsoft.com/azure/role-based-access-control/role-assignments-portal

https://learn.microsoft.com/azure/role-based-access-control/rbac-and-directory-admin-roles

(I work at MS in the SDK team)

Montespan answered 28/5, 2019 at 16:30 Comment(2)
Thanks @Laurent I see my user on IAM and also I was able to list using azure cli using below code. python import subprocess import json subscriptions = json.loads(subprocess.check_output('az account list', shell=True).decode('utf-8')) print(subscriptions) However, using the python SDK it returns an empty list.Longford
Note that CLI is using the very same SDK to do the call. Are you logging the CLI with the same credentials?Montespan
L
1

I think I solved the issue using Azure CLI. Yet, I still wonder why it didn't work as supposed using azure-python-sdk.

Here is the code:

import subprocess
import json
subscriptions = json.loads(subprocess.check_output('az account list', shell=True).decode('utf-8'))
print(subscriptions)

Thank you for your responses.

Longford answered 29/5, 2019 at 16:44 Comment(1)
I am having a similar(ish) problem: az account list correctly lists BOTH subscriptions I have, but in python subscriptions_client.subscriptions.list() only returns ONE of the TWO subscriptions. What gives?Ajaajaccio

© 2022 - 2024 — McMap. All rights reserved.