从Azure检索可用位置列表-Python SDK

时间:2019-05-07 14:29:12

标签: python python-2.7 azure sdk cloud

我想保存一个可用位置列表以供我订阅Azure。到目前为止,我已经知道了:

from azure.mgmt.subscription import SubscriptionClient


class ProviderAzure(CloudProviderAbstractClass):
    def __init__(self, client_id, access_secret_key, tenant, subscription_id, location):
        super(ProviderAzure, self).__init__()

        self.credentials = [subscription_id,
                            ServicePrincipalCredentials(
                                client_id=client_id,
                                secret=access_secret_key,
                                tenant=tenant)]

        self.location = location
        self.subscription_id = subscription_id
        self.subscription_client = SubscriptionClient(self.credentials)

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

但是当我运行list_available_locations()时,我得到以下输出:

     63         session = request.context.session
     64         try:
---> 65             self._creds.signed_session(session)
     66         except TypeError: # Credentials does not support session injection
     67             _LOGGER.warning("Your credentials class does not support session injection. Performance will not be at the maximu
m.")


AttributeError: 'list' object has no attribute 'signed_session'

有人知道我如何检索列表吗?我正在使用python2.7。

谢谢。

1 个答案:

答案 0 :(得分:0)

您传递给SubscriptionClient的凭据对象无效,您传递的列表包含两个元素:subscription_id +凭据类。 SubscriptionClient仅按设计接受凭据类(因为它是订阅客户端,因此不接受订阅作为输入)

from azure.mgmt.subscription import SubscriptionClient


class ProviderAzure(CloudProviderAbstractClass):
    def __init__(self, client_id, access_secret_key, tenant, subscription_id, location):
        super(ProviderAzure, self).__init__()

        self.credentials =  ServicePrincipalCredentials(
                                client_id=client_id,
                                secret=access_secret_key,
                                tenant=tenant)

        self.location = location
        self.subscription_client = SubscriptionClient(self.credentials)

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