【发布时间】:2016-03-29 10:26:11
【问题描述】:
我需要使用 .Net Web 应用程序更新(添加、删除、编辑值)分配给 Azure 资源的标签。有 Azure 资源 API,我可以使用这些 API 获取分配给资源的标签。如果有任何 Azure API 或任何其他方式将这些分配的标签更新到资源,请告诉我。
【问题讨论】:
我需要使用 .Net Web 应用程序更新(添加、删除、编辑值)分配给 Azure 资源的标签。有 Azure 资源 API,我可以使用这些 API 获取分配给资源的标签。如果有任何 Azure API 或任何其他方式将这些分配的标签更新到资源,请告诉我。
【问题讨论】:
使用下面的 Powershell Cmdlet 添加新标签或使用新值更新现有标签。
Set-AzureRmResource -Tag @( @{ Name="tag_name"; Value="tag_value" }) -ResourceId <resource_id>
使用REST API,请求URI为:
https://management.azure.com/subscriptions/{subscription-id}/tagNames/{tag-name}/tagValues/{tag-value}?api-version={api-version}
将 {tag-name} 替换为要添加值的标签的名称。将 {tag-value} 替换为要添加到资源标签的值。标签值最多可以有 256 个字符,并且区分大小写。
详情请参阅resource-group-using-tags 和https://msdn.microsoft.com/en-us/library/azure/dn848370.aspx。
更新:
在上面的 REST API 中创建的标签没有资源。似乎没有可用于将标签添加到指定资源的 API。 但是,您可以尝试下面的 C# 代码来更新分配的标签:
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
//MyResourceOperation implemented interface IResourcesOperations
MyResourceOperation resourceOpertion = new MyResourceOperation();
//Get a resource belonging to a resource group
Resource myResource = resourceOpertion.Get("resourceGroupName", "resourceProviderNamespace", "parentResourcePath", "resourceType", "resourceName", "apiVersion");
//update the assigned tag with a new value
myResource.Tags.Add("tagName", "updatedValue");
希望这会有所帮助。
【讨论】: