【问题标题】:tag resources azure python sdk - operation merge?标签资源 azure python sdk - 操作合并?
【发布时间】:2021-08-19 09:59:01
【问题描述】:

我想弄清楚如何像在 PS 中那样使用 Merge 操作标记资源。

powershell 中的示例 -

Update-AzTag -ResourceId $s.ResourceId -Tag $mergedTags -Operation Replace

我在 python 中的代码 -

    # Tag the resource groups.
    resource_group_client.resource_groups.create_or_update(resource_group_name=rg["Resource-group-name"],parameters=
    {'location': rg['location'], 
        'tags':tags_dict,
        'Operation': 'Merge'})

如您所见,我正在尝试我的运气来放置“操作”:“合并”,但它不起作用...... 请问这里有什么帮助吗?

【问题讨论】:

    标签: python azure tags resources azure-sdk-python


    【解决方案1】:

    我们在 python create_or_update 函数中没有合并选项,请查看合并标签上的documentation

    我们可以使用 resource_group_params.update(tags={'hello': 'world'}) 来一次更新所有标签。 下面是关于更新的代码

    resource_group_params = {'location':'westus'} #adding location tag to a variable to pass it in create_or_update function
    resource_group_params.update(tags={'hello': 'world'}) # adding tags (This will remove all the previous tags and update with the one which we are passing currently
    client.resource_groups.create_or_update('azure-sample-group', resource_group_params)
    

    但是文档中的上述代码将删除所有以前的标签并使用我们当前传递的标签进行更新。

    这里我们的要求是附加/合并标签,所以我创建了一个python脚本,我们可以在其中将标签附加到旧标签:

    # Import the needed credential and management objects from the libraries.
    from types import FunctionType
    from azure.identity import AzureCliCredential
    from azure.mgmt.resource import ResourceManagementClient
    import os
    
    from azure.mgmt.resource.resources.v2016_02_01 import operations
    
    credential = AzureCliCredential()
    subscription_id = "SUBSCRIPTION ID" #Add your subscription ID
    resource_group = "RESOURCE_GRP_ID" #Add your resource group
    resource_client = ResourceManagementClient(credential, subscription_id)
    resource_list = resource_client.resource_groups.list()
    for resource in resource_list:
        if resource.name == resource_group:
            appendtags = resource.tags #gathering old tags
            newTags = {"Name1": "first"} #new tags
            appendtags.update(newTags) # adding my new tags to old ones
            print(appendtags) 
            resource_client.resource_groups.create_or_update(resource_group_name=resource_group, parameters= {"location": "westus2", "tags": appendtags})
    

    【讨论】:

      【解决方案2】:

      我已经用这段代码修复了它..它工作得非常完美 - (您应该使用“update_at_scope”。

      resource_group_client = ResourceManagementClient(credential, subscription_id=sub.subscription_id)
      
      
               body = {
                   "operation" :  "Merge",
                   "properties" : {
                       "tags" : 
                           tags_dict,
                   }
               }
      
               resource_group_client.tags.update_at_scope(rg["Resource-id"] ,body)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-23
        • 1970-01-01
        • 2020-01-17
        • 1970-01-01
        • 1970-01-01
        • 2022-11-03
        • 2020-05-05
        • 1970-01-01
        相关资源
        最近更新 更多