0

I am new to use Azure python sdk. I have been using the following command to add tags to a subscription:

az tag update --operation merge --resource-id /subscriptions/{subscription-id} --tags <tag-name>=<tag-value>

The above command works good and creates the tag with the specified value in the subscription. However, I want to use the similar functionality in Python SDK. I found this to create / update a tag. It is returning a successful message. But I could not find any tag attached to the subscription with the given name. Please find attached code sample:

import os
import json
import csv
from azure.identity import DefaultAzureCredential
from azure.identity import InteractiveBrowserCredential
from azure.mgmt.resource.subscriptions import SubscriptionClient
from azure.mgmt.resource.resources import ResourceManagementClient

# from azure.

def main() :
    header = ("subscription name", "subscription id", "pce-identity-functional-group")
    subscription_client = SubscriptionClient(credential=DefaultAzureCredential())

    result = subscription_client.subscriptions.list()
    for e in result:
        rc = ResourceManagementClient(credential=DefaultAzureCredential(), subscription_id=e.subscription_id)
        print(rc.tags.create_or_update(tag_name="testing_one"))

Could anyone help me find where I am going wrong ? Thanks in advance.

rammanoj
  • 479
  • 8
  • 26
  • Maybe or maybe not. But you don't define anywhere which version you are using. You are pointing at documentation for 2019. Maybe define the 2019 version for your resource management client? Currently it's using latest, which is 2020. My experience is that not all functionality is always made available in the new version, even if the older one has it. Has happened to me multiple times for several of the clients. Other than that, i can't realy see anything wrong with the code. I see you are using the create_or_update, might be worth to try the at_scope version instead? – Marco Oct 28 '22 at 06:26
  • Hi @rammanoj I have tried with piece code to update tag but it shows an error because it takes only predefined tags and I have tried piece code that can successfully update tag in resource group level. – Venkatesan Oct 29 '22 at 14:44

1 Answers1

1

I tried in my environment and got below results:

Initially I have tried python code to Add the tag in the subscription level.

enter image description here

The error show in SDK it updates only Predefined tags .

According to MS-DOCS it clearly state used to create and update method is used to predefined tags. you can add tags by Powershell as you mentioned in your post.

enter image description here

But in python sdk you can update in resource group level.

Code:

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.subscriptions import SubscriptionClient



subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "<id>") # your Azure Subscription Id
credentials = DefaultAzureCredential()
client = ResourceManagementClient(credentials, subscription_id)
SubscriptionClient = client
resource_group_params = {'location':'East US'}
resource_group_params.update(tags={'environment': 'cloud'})
client.resource_groups.update('resourcegroup', resource_group_params)

Console: enter image description here

Portal: enter image description here

Reference:

Venkatesan
  • 3,748
  • 1
  • 3
  • 15