Azure python sdk add tag to subscription

When working with the Azure Python SDK, you may come across the need to add tags to a subscription. Tags are key-value pairs that can be used to organize and categorize resources in Azure. In this article, we will explore three different ways to add tags to a subscription using the Azure Python SDK.

Option 1: Using the Azure Management Libraries for Python

The Azure Management Libraries for Python provide a high-level, object-oriented API for managing Azure resources. To add tags to a subscription using this library, you can follow these steps:


from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import SubscriptionClient

# Create a subscription client
credential = DefaultAzureCredential()
subscription_client = SubscriptionClient(credential)

# Get the subscription ID
subscription_id = ""

# Get the subscription
subscription = subscription_client.subscriptions.get(subscription_id)

# Add tags to the subscription
subscription.tags = {"key1": "value1", "key2": "value2"}

# Update the subscription
subscription_client.subscriptions.create_or_update(subscription_id, subscription)

This code snippet uses the Azure Identity library to authenticate the user and the SubscriptionClient class from the Azure Resource Management library to interact with the subscription. It retrieves the subscription using the provided subscription ID, adds the desired tags to the subscription object, and updates the subscription in Azure.

Option 2: Using the Azure SDK for Python (Azure CLI)

The Azure SDK for Python, also known as the Azure CLI, provides a command-line interface for managing Azure resources. To add tags to a subscription using the Azure CLI, you can run the following command:


!az login

!az account set --subscription ""

!az account update --set tags.key1=value1 tags.key2=value2

This code snippet uses the Azure CLI commands to authenticate the user, set the desired subscription, and update the subscription with the specified tags. The “az login” command prompts the user to authenticate, while the “az account set” command sets the active subscription. Finally, the “az account update” command adds the tags to the subscription.

Option 3: Using the Azure SDK for Python (Azure Management Libraries for REST)

The Azure Management Libraries for REST provide a low-level, HTTP-based API for managing Azure resources. To add tags to a subscription using these libraries, you can use the following code:


import requests
import json

# Set the subscription ID and tags
subscription_id = ""
tags = {"key1": "value1", "key2": "value2"}

# Set the request URL
url = f"https://management.azure.com/subscriptions/{subscription_id}?api-version=2021-04-01"

# Set the request headers
headers = {
    "Authorization": "Bearer ",
    "Content-Type": "application/json"
}

# Send the PATCH request
response = requests.patch(url, headers=headers, json={"tags": tags})

# Check the response status code
if response.status_code == 200:
    print("Tags added successfully.")
else:
    print("Failed to add tags.")

This code snippet uses the requests library to send an HTTP PATCH request to the Azure Management API. It sets the subscription ID and tags, constructs the request URL with the appropriate API version, sets the necessary headers including the access token, and sends the PATCH request with the specified tags. The response status code is then checked to determine the success of the operation.

After exploring these three options, it is clear that Option 1, using the Azure Management Libraries for Python, provides a more streamlined and Pythonic way to add tags to a subscription. It abstracts away the low-level details and provides a higher-level API for managing Azure resources. This option is recommended for its simplicity and ease of use.

Rate this post

8 Responses

  1. Option 2 for Azure SDK in Python is the way to go, no doubt! Its easy peasy lemon squeezy! 🍋🐍 #PythonPower

    1. I couldnt disagree more. Option 1 for Azure SDK in Python is the real deal. It offers more flexibility and power, making it the obvious choice for serious developers. Dont settle for easy peasy when you can have top-notch performance. #PythonProsOnly

  2. Wow, Im blown away by all these options! But seriously, which one is the most reliable and efficient for adding tags to Azure subscriptions using Python?

    1. In my experience, the most reliable and efficient Python library for adding tags to Azure subscriptions is azure-mgmt-resource. It provides a straightforward API and excellent documentation. Give it a try!

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents