Reputation: 689
How can I add Tags to an azure resource using C# Code in my ASP.NET Application. I am trying to create an azure tag management portal.
I found this question but it is about adding a tag to a resource group . Also that Library seems deprecated. If any one knows how to attach a tag to a Resource, Please help.
Note: (i)I have tried the Azure ServiceManagement API but I see there is no API support for attaching a tag to a resource. (ii) Will powershell Cmdlets a viable option if nothing else works?
Upvotes: 1
Views: 2705
Reputation: 100
Follow this link to create client application in AD and service principle. Make note of tenantId, ClientId and ClientKey
Find the resource, add / update tags and patch it.
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, clientKey);
var resourceClient = new ResourceManagementClient(serviceCreds);
resourceClient.SubscriptionId = subscriptionId;
GenericResource genericResource = resourceClient.Resources.Get("some-resource-group", "Microsoft.DocumentDB", "", "databaseAccounts", "some-resource", "2016-03-31");
genericResource.Tags.Add("Version", "1.0");
resourceClient.Resources.CreateOrUpdate("some-resource-group", "Microsoft.DocumentDB", "", "databaseAccounts", "some-resource", "2016-03-31", genericResource);
Upvotes: 2
Reputation: 689
If anyone interested in a way to try out through REST API, I found this after spying on powershell bound traffic using Fiddler. Note the tag as json-payload.
https://management.azure.com/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Compute/virtualMachines/{VM-Name}
PATCH **https://management.azure.com/subscriptions/6dcd{subscrID}e8f/resourceGroups/css-dev/providers/Microsoft.Sql/servers/css-development/databases/css-dev?api-version=2014-04-01 HTTP/1.1**
Authorization: Bearer sDSEsiJKV1QiLCJhbG[<PARTIALLY REMOVED BY KIRAN FOR SECURITY REASON>]XDvZBJG5Jhh0rivehvDS
User-Agent: AzurePowershell/v1.0.0.0
ParameterSetName: Resource that resides at the subscription level.
CommandName: Set-AzureRmResource
Content-Type: application/json; charset=utf-8
Host: management.azure.com
Content-Length: 52
Expect: 100-continue
Connection: Keep-Alive
**{
"tags": {
"displayName": "AzureV1"
}
}**
Upvotes: 1
Reputation: 1579
PowerShell could easily do this, you can use the command below:
PS C:\> Set-AzureRmResource -Tag @( @{ Name="tag_name"; Value="tag_value" }) -ResourceId <resource_id>
Check this article for details.
Upvotes: 1