azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

How to get tenant list which contain more information?

Open dyhe83 opened this issue 6 years ago • 5 comments

Hi All,

I'm searching for how to get tenant list by using Python SDK. After some research I find out SubscriptionClient can get list of tenants which only contain id & tenant id. But SubscriptionClient does not provide sufficient information as Azure Restful API (name, country code, etc...).

I would like to know is there any other method in Python SDK can list tenants? Or this maybe a feature in the future. Many thanks!


Restful API: https://docs.microsoft.com/en-us/rest/api/resources/tenants/list

SubscriptionClient: https://docs.microsoft.com/en-us/python/api/azure-mgmt-resource/azure.mgmt.resource.subscriptionclient?view=azure-python#tenants

dyhe83 avatar Aug 04 '20 03:08 dyhe83

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @anuragdalmia, @shilpigautam, @ramaganesan-rg.

ghost avatar Aug 04 '20 15:08 ghost

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @adamedx.

ghost avatar Aug 04 '20 16:08 ghost

Routing to a AAD for this question.

ramaganesan-rg avatar Aug 04 '20 16:08 ramaganesan-rg

hi, in python sdk, SubscriptionClient has a member named tenants whose type is TenantsOperations. TenantsOperations has a method:

        def extract_data(pipeline_response):
            deserialized = self._deserialize('TenantListResult', pipeline_response)
            list_of_elem = deserialized.value
            if cls:
                list_of_elem = cls(list_of_elem)
            return deserialized.next_link or None, iter(list_of_elem)

TenantListResult has country, country code etc, pls have a try https://docs.microsoft.com/en-us/rest/api/resources/tenants/list#tenantiddescription

chenjianfei2017 avatar Aug 06 '20 02:08 chenjianfei2017

@chenjianfei2017 many thanks. I just found out I had wrong import statement. Two different package will have different output.

Python

from azure.mgmt import resource, subscription


def get_tenants(sub_client):
    tenants = sub_client.tenants
    print('api_version: ' + tenants.api_version)
    for tenant in tenants.list():
        print(tenant.__dict__.keys())


def main():
    credentials = get_credential()
    token = credentials.token['access_token']

    print('[azure.mgmt.resource.SubscriptionClient]')
    sub_client = resource.SubscriptionClient(credentials)
    get_tenants(sub_client)
    print()
    print('[azure.mgmt.subscription.SubscriptionClient]')
    sub_client = subscription.SubscriptionClient(credentials)
    get_tenants(sub_client)

Output

[azure.mgmt.resource.SubscriptionClient]
api_version: 2019-11-01
dict_keys(['additional_properties', 'id', 'tenant_id', 'tenant_category', 'country', 'country_code', 'display_name', 'domains'])
dict_keys(['additional_properties', 'id', 'tenant_id', 'tenant_category', 'country', 'country_code', 'display_name', 'domains'])
dict_keys(['additional_properties', 'id', 'tenant_id', 'tenant_category', 'country', 'country_code', 'display_name', 'domains'])
dict_keys(['additional_properties', 'id', 'tenant_id', 'tenant_category', 'country', 'country_code', 'display_name', 'domains'])

[azure.mgmt.subscription.SubscriptionClient]
api_version: 2016-06-01
dict_keys(['additional_properties', 'id', 'tenant_id'])
dict_keys(['additional_properties', 'id', 'tenant_id'])
dict_keys(['additional_properties', 'id', 'tenant_id'])
dict_keys(['additional_properties', 'id', 'tenant_id'])

Conclusion

should use resource package. https://pypi.org/project/azure-mgmt-resource/

dyhe83 avatar Aug 07 '20 02:08 dyhe83