azure - 在 AZURE 中使用 python sdk 更新资源标签时需要帮助
问题描述
我正在寻找在 Azure 中使用 python SDK 更新资源标签的帮助。我能够更新标签,resource groups
但我无法找到如何更新标签resources
下面是我用来更新resource groups
标签的代码
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
import os
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] = "Key"
#resource_group = os.getenv("RESOURCE_GROUP_NAME", "name")
resource_client = ResourceManagementClient(credential, subscription_id)
group_list = resource_client.resources.list()
for group in list(group_list):
a = group.location
b = group.name
if not group.tags:
resource_client.resources.create_or_update(
b,
{
"location" : a,
"tags" : tag_dict
}
)
解决方案
根据您的要求,我们编写了以下代码来为特定资源组下的各个资源创建标签我们已经在本地环境中测试了代码并且运行良好。
from logging import NullHandler
from azure.core import pipeline
from azure.identity import AzureCliCredential, _credentials
from azure.mgmt.resource import ResourceManagementClient, resources
import os
credential = AzureCliCredential()
subscription_id = "<subscription-id>"
resource_group = "<resource-group>"
resource_client = ResourceManagementClient(credential, subscription_id)
resource_list= resource_client.resources.list_by_resource_group(resource_group)
tag_dict = {"Month": "sept-24"}
for resource in resource_list:
body = {
"operation" : "create",
"properties" : {
"tags" : tag_dict
}
}
resource_client.tags.create_or_update_at_scope(resource.id,body)
print("Resource tags were updated successfully please check the output in the portal")
这是供参考的示例输出:
推荐阅读
- c++ - OpenCV 读取 3D 原始图像
- android - 如何为图像制作圆角?
- sql-server - SQL Server 无效版本:15 (Microsoft.SqlServer.Smo)
- javascript - 使用 Youtube API V3 时出现 403 错误
- node.js - NodeJs 请求响应正文为空
- python - 在 python 中是否有一个容易切换 GPU 的方法
- python - Numpy:向量化 np.argwhere
- git - 如何在 git 中创建权限访问组?
- variables - SwiftUI 新建文件并传递值
- java - 如何实现降序选择排序