如何使用创建标签Python Azure SDK?

时间:2016-04-13 09:22:04

标签: python azure azure-sdk-python

我正在尝试使用Python在Azure 2.0.0rc2中创建标记。 以下是我使用的代码:

def __update_tags(self):
    username = 'user@xyz.com'
    password = 'user@1234'
    subscription_id = '478-ytehn-47ds5-784aa-4758a'
    credentials = UserPassCredentials(username=username, password=password)
    resource_client = ResourceManagementClient(credentials=credentials)
    tag_operations = TagOperations(client=resource_client)
    tag_operations.create_or_update_value(tag_name='key_1', tag_value='val_1')

在运行此代码时,我收到如下错误:

    if self.client.credentials.subscription_id is not None:
AttributeError: 'UserPassCredentials' object has no attribute 'subscription_id'

任何人都有想法解决这个问题。

2 个答案:

答案 0 :(得分:0)

在您的代码中,指定了subscription_id但未使用。创建resource_client时需要subscription_id。请使用以下代码替换" resource_client = ResourceManagementClient(credentials = credentials)"

resource_client = ResourceManagementClient(
ResourceManagementClientConfiguration(
    credentials,
    subscription_id
)

查看here了解详情。

更新: 确认导入ResourceManagementClientConfiguration enter image description here

答案 1 :(得分:0)

根据文件(Resource ManagementResource Management Authentication),正如@ forester123所说,总结如下代码。

from azure.common.credentials import UserPassCredentials
from azure.mgmt.resource.resources import ResourceManagementClient, ResourceManagementClientConfiguration

username = 'user@xyz.com'
password = 'user@1234'
subscription_id = '478-ytehn-47ds5-784aa-4758a'

credentials = UserPassCredentials(username, password)
resource_client = ResourceManagementClient(
    ResourceManagementClientConfiguration(
        credentials,
        subscription_id
    )
)