8

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?

Varun
  • 119
  • 1
  • 1
  • 6

4 Answers4

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

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
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)

Laurent Mazuel
  • 3,422
  • 13
  • 27
  • 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. – Varun May 29 '19 at 16:35
  • Note that CLI is using the very same SDK to do the call. Are you logging the CLI with the same credentials? – Laurent Mazuel May 29 '19 at 23:27
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.

Varun
  • 119
  • 1
  • 1
  • 6
  • 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? – Ben Sussman Nov 17 '20 at 00:16
1

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.

Pin90
  • 91
  • 1
  • 10