0

I am trying to create a azure resource id in python script using information of provider and resourcegroup name. which will be used for rest api calls. But the problem is I have only name of subscription. How can I fetch subscriptionId in my prog?

Umesh417
  • 75
  • 1
  • 8
  • You would need to get the list of all subscriptions and then find a matching subscription based on the subscription name. – Gaurav Mantri May 20 '22 at 15:19
  • To get the list of all subscriptions, you can use the SubscriptionClient in azure-mgmt-resource – Laurent Mazuel May 20 '22 at 21:26
  • +1 to Laurent, refer to https://learn.microsoft.com/en-us/python/api/azure-mgmt-resource/azure.mgmt.resource.subscriptions.subscriptionclient?view=azure-python-preview for more detial – Elendil Zheng-MSFT May 23 '22 at 06:57

1 Answers1

-1

You can achieve this by calling azcli in python to get the subscription details which follows

Here I am following github code to achieve your requirement

def list_subscriptions():
    log = logging.getLogger(__name__)

try:
    subClient = get_client_from_cli_profile(SubscriptionClient)

except CLIError:
    log.info("Not logged in, running az login")

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

Reference:
List subscriptions for a given Azure account