Ram
Ram

Reputation: 11

Azure APIs to update the tags assigned to Resources

I have a requirement to update(add, delete, edit value) the tags assigned to Azure resources using .Net web application. There are Azure resource APIs using which I can fetch the tags assigned to the resources. Please let me know if there are any Azure APIs or any other way to update these assigned tags to the resources.

Upvotes: 0

Views: 1264

Answers (1)

Derek
Derek

Reputation: 837

  1. Use the Powershell Cmdlet below to add a new tag or update a existed tag with new value.

    Set-AzureRmResource -Tag @( @{ Name="tag_name"; Value="tag_value" }) -ResourceId <resource_id>

  2. Use REST API, the request URI is:

    https://management.azure.com/subscriptions/{subscription-id}/tagNames/{tag-name}/tagValues/{tag-value}?api-version={api-version}

    Replace {tag-name} with the name of the tag to which you want to add a value. Replace {tag-value} with the value that you want to add to the resource tag. A tag value can have a maximum of 256 characters and is case sensitive.

Refer to resource-group-using-tags and https://msdn.microsoft.com/en-us/library/azure/dn848370.aspx for details.

Update:

The tag created in the REST API above has no resources.And it seems there is no API available to add the tag to a specified resource. However, you can try the C# code below to update assigned tag:

    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"); 

enter image description here

Hope this helps.

Upvotes: 1

Related Questions