Kiran
Kiran

Reputation: 63

Listing locations in Azure using Python Azure SDK error

I am trying out the listing of different locations in Azure using Python Azure SDKs from Windows machine Below is the error :

enter image description here

Please see my code :

import os
import sys
import logging
from azure import *
from azure.servicemanagement import *


subscription_id = 'XXXXXX-XXXXX-XXXXX-XXXXX-XXXXXXXXXXXX'
certificate_path = '\.pem'

   sms = ServiceManagementService(subscription_id, certificate_path)
   result = sms.list_subscriptions()
   for location in result:
    print(location.name)

enter image description here

Upvotes: 3

Views: 1993

Answers (5)

Shivam Anand
Shivam Anand

Reputation: 1601

An update to the answer by @darc. Azure SDK for python has changed the way how auth works from from azure.common.credentials import ServicePrincipalCredentials to from azure.identity import ClientSecretCredential. The newer version has been re-designed and replaced by azure-identity.
Use the code below to refer to things that have changed from @darc's answer

from azure.identity import ClientSecretCredential
from azure.mgmt.subscription import SubscriptionClient

subscription_id = "11111111-1111-1111-1111-111111111111"
client_id = "11111111-1111-1111-1111-111111111111"
secret = "11111111-1111-1111-1111-111111111111"
tenant_id = "11111111-1111-1111-1111-111111111111"

creds = ClientSecretCredential(client_id=client_id, client_secret=secret,tenant_id=tenant_id)
subscription_client = SubscriptionClient(creds)

locations = subscription_client.subscriptions.list_locations(subscription_id)
for location in locations:
    print(location.name)

Upvotes: 1

darc
darc

Reputation: 530

I hope this helps, you can use the new api to list locations, you don't need to use a certificate :).

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.subscription import SubscriptionClient


    subscription_id = "11111111-1111-1111-1111-111111111111"
    client_id = "11111111-1111-1111-1111-111111111111"
    secret = "11111111-1111-1111-1111-111111111111"
    tenant_id = "11111111-1111-1111-1111-111111111111"

    creds = ServicePrincipalCredentials(client_id=client_id, secret=secret,                 
    tenant=tenant_id)

    subscription_client = SubscriptionClient(creds)
    locations = subscription_client.subscriptions.list_locations(subscription_id)
    for location in locations:
        print(location.name)

generating client ID and secret can be done by running the cli command:

    az ad sp create-for-rbac --sdk-auth > my.azureauth

Upvotes: 4

Peter Pan
Peter Pan

Reputation: 24138

According to your code & the error information, I think the issue was caused by the value of the certificate_path variable which is not a valid file path. On Windows, the path \ means the root path of disk driver like C:\ or D:\, and the file name .pem just is a file suffix name, not a valid file name, and the symbol \ is not a valid character for naming file. So please use a valid file path of a existing valid certificate file in your code.

Meanwhile, the list locations functions in ASM mode are different from its in ARM mode. The ASM one lists all of the data center locations that are valid for the specified subscription, but the ARM one list all of the available geo-locations. Please see the related REST API references below to know them because of Azure SDK for Python only wrapped the REST APIs.

  1. List locations in ASM mode, please see https://msdn.microsoft.com/en-us/library/azure/gg441293.aspx.
  2. List locations in ARM mode, please see https://learn.microsoft.com/en-us/rest/api/resources/subscriptions#Subscriptions_ListLocations.

Upvotes: 2

Gopal Venu
Gopal Venu

Reputation: 27

Use sms.list_locations() to list out the regions.

Thanks, Gopal.

Upvotes: -1

Laurent Mazuel
Laurent Mazuel

Reputation: 3546

Your code is correct. Based on the stacktrace, it's seems your certificate is not valid. This might help you: http://azure-sdk-for-python.readthedocs.io/en/latest/servicemanagement.html#creating-and-uploading-new-certificate-with-openssl

It's not directly related to your question, but this method is part of the Service Management library of Azure. Current recommendation is to use the Azure Resource Management library (a.k.a. ARM):

Upvotes: 1

Related Questions