Tag Configuration Object
Table of Contents
- Overview
- Core Methods
- Tag Model Attributes
- Exceptions
- Basic Configuration
- Usage Examples
- Creating Tags
- Retrieving Tags
- Updating Tags
- Listing Tags
- Filtering Responses
- Controlling Pagination with max_limit
- Deleting Tags
- Managing Configuration Changes
- Performing Commits
- Monitoring Jobs
- Error Handling
- Best Practices
- Full Script Examples
- Related Models
Overview
The Tag
class provides functionality to manage tag objects in Palo Alto Networks' Strata Cloud Manager. This class
inherits from BaseObject
and provides methods for creating, retrieving, updating, and deleting tags with specific
colors and attributes. In addition, it offers flexible filtering capabilities when listing tags, enabling you to
apply color-based filters, limit results to exact matches of a configuration container, and exclude certain folders,
snippets, or devices as needed.
Core Methods
Method | Description | Parameters | Return Type |
---|---|---|---|
create() |
Creates a new tag | data: Dict[str, Any] |
TagResponseModel |
get() |
Retrieves a tag by ID | object_id: str |
TagResponseModel |
update() |
Updates an existing tag | tag: TagUpdateModel |
TagResponseModel |
delete() |
Deletes a tag | object_id: str |
None |
list() |
Lists tags with comprehensive filtering options | folder or snippet or device , exact_match , exclude_folders , exclude_snippets , exclude_devices , **filters |
List[TagResponseModel] |
fetch() |
Gets tag by name and container | name: str and one container (folder , snippet , or device ) |
TagResponseModel |
Tag Model Attributes
Attribute | Type | Required | Description |
---|---|---|---|
name |
str | Yes | Name of the tag object (max 63 chars) |
id |
UUID | Yes* | Unique identifier (*response only) |
color |
str | No | Color from a predefined list |
comments |
str | No | Comments (max 1023 chars) |
folder |
str | Yes** | Folder location (one container required) |
snippet |
str | Yes** | Snippet location (one container required) |
device |
str | Yes** | Device location (one container required) |
Exceptions
Exception | HTTP Code | Description |
---|---|---|
InvalidObjectError |
400 | Invalid tag data or format |
MissingQueryParameterError |
400 | Missing required parameters |
NameNotUniqueError |
409 | Tag name already exists |
ObjectNotPresentError |
404 | Tag not found |
ReferenceNotZeroError |
409 | Tag still referenced |
AuthenticationError |
401 | Authentication failed |
ServerError |
500 | Internal server error |
Basic Configuration
The Tag service can be accessed using either the unified client interface (recommended) or the traditional service instantiation.
Unified Client Interface (Recommended)
# Initialize clientclient = ScmClient(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)
# Access the Tag service directly through the client# No need to create a separate Tag instancetags = client.tag
Traditional Service Instantiation (Legacy)
from scm.config.objects import Tag
# Initialize clientclient = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)
# Initialize Tag object explicitlytags = Tag(client)
Note
While both approaches work, the unified client interface is recommended for new development as it provides a more streamlined developer experience and ensures proper token refresh handling across all services.
Usage Examples
Creating Tags
# Initialize clientclient = ScmClient(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)
# Basic tag configurationbasic_tag = {
"name": "Production",
"color": "Red",
"comments": "Production environment resources",
"folder": "Texas"
}
# Create basic tag using the unified client interfacebasic_tag_obj = client.tag.create(basic_tag)
# Tag with different colordev_tag = {
"name": "Development",
"color": "Blue",
"comments": "Development environment resources",
"folder": "Texas"
}
dev_tag_obj = client.tag.create(dev_tag)
# Tag for a specific applicationapp_tag = {
"name": "Web-Servers",
"color": "Green",
"comments": "Web server resources",
"folder": "Texas"
}
app_tag_obj = client.tag.create(app_tag)
Retrieving Tags
print(f"Found tag: {tag.name}")
print(f"Color: {tag.color}")
# Get by ID using the unified client interfacetag_by_id = client.tag.get(tag.id)
print(f"Retrieved tag: {tag_by_id.name}")
Updating Tags
# Update attributesexisting_tag.color = "Azure Blue"
existing_tag.comments = "Updated production environment tag"
# Perform update using the unified client interfaceupdated_tag = client.tag.update(existing_tag)
Listing Tags
folder='Texas'
)
# Apply color filters using the unified client interfacefiltered_tags = client.tag.list(
folder='Texas',
colors=['Red', 'Blue']
)
for tag in filtered_tags:
print(f"Name: {tag.name}, Color: {tag.color}")
Filtering Responses
The list()
method supports additional parameters to refine your query results even further. Alongside basic filters
(like colors
), you can leverage the exact_match
, exclude_folders
, exclude_snippets
, and exclude_devices
parameters to control which objects are included or excluded after the initial API response is fetched.
Parameters:
exact_match (bool)
: WhenTrue
, only objects defined exactly in the specified container (folder
,snippet
, ordevice
) are returned.exclude_folders (List[str])
: List of folders to exclude.exclude_snippets (List[str])
: List of snippets to exclude.exclude_devices (List[str])
: List of devices to exclude.colors (List[str])
: Filter tags by specified colors.
Examples:
# Initialize clientclient = ScmClient(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)
# Only return tags defined exactly in 'Texas' using the unified client interfaceexact_tags = client.tag.list(
folder='Texas',
exact_match=True
)
for tag in exact_tags:
print(f"Exact match: {tag.name} in {tag.folder}")
# Exclude all tags from the 'All' folder using the unified client interfaceno_all_tags = client.tag.list(
folder='Texas',
exclude_folders=['All']
)
for tag in no_all_tags:
assert tag.folder != 'All'
print(f"Filtered out 'All': {tag.name}")
# Exclude tags that come from 'default' snippet using the unified client interfaceno_default_snippet = client.tag.list(
folder='Texas',
exclude_snippets=['default']
)
for tag in no_default_snippet:
assert tag.snippet != 'default'
print(f"Filtered out 'default' snippet: {tag.name}")
# Exclude tags associated with 'DeviceA' using the unified client interfaceno_deviceA = client.tag.list(
folder='Texas',
exclude_devices=['DeviceA']
)
for tag in no_deviceA:
assert tag.device != 'DeviceA'
print(f"Filtered out 'DeviceA': {tag.name}")
# Combine exact_match with multiple exclusions and colors using the unified client interfacerefined_tags = client.tag.list(
folder="Texas",
exact_match=True,
exclude_folders=["All"],
exclude_snippets=["default"],
exclude_devices=["DeviceA"],
colors=["Red", "Blue"]
)
for tag in refined_tags:
print(f"Refined filter result: {tag.name} in {tag.folder}, Color: {tag.color}")
Controlling Pagination with max_limit
The SDK supports pagination through the max_limit
parameter, which defines how many objects are retrieved per API call. By default, max_limit
is set to 2500. The API itself imposes a maximum allowed value of 5000. If you set max_limit
higher than 5000, it will be capped to the API's maximum. The list()
method will continue to iterate through all objects until all results have been retrieved. Adjusting max_limit
can help manage retrieval performance and memory usage when working with large datasets.
from scm.config.objects import Tag
# Initialize clientclient = ScmClient(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)
# Two options for setting max_limit:# Option 1: Use the unified client interface but create a custom Tag instance with max_limittag_service = Tag(client, max_limit=4321)
all_tags1 = tag_service.list(folder='Texas')
# Option 2: Use the unified client interface directly# This will use the default max_limit (2500)all_tags2 = client.tag.list(folder='Texas')
# Both options will auto-paginate through all available objects.# The tags are fetched in chunks according to the max_limit.
Deleting Tags
client.tag.delete(tag_id)
Managing Configuration Changes
Performing Commits
"folders": ["Texas"],
"description": "Updated tag definitions",
"sync": True,
"timeout": 300 # 5 minute timeout
}
# Commit the changes directly using the client# Note: Commits should always be performed on the client object directly, not on service objectsresult = client.commit(**commit_params)
print(f"Commit job ID: {result.job_id}")
Monitoring Jobs
print(f"Job status: {job_status.data[0].status_str}")
# List recent jobs directly from the clientrecent_jobs = client.list_jobs(limit=10)
for job in recent_jobs.data:
print(f"Job {job.id}: {job.type_str} - {job.status_str}")
Error Handling
from scm.exceptions import (
InvalidObjectError,
MissingQueryParameterError,
NameNotUniqueError,
ObjectNotPresentError,
ReferenceNotZeroError
)
# Initialize clientclient = ScmClient(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)
try:
# Create tag configuration
tag_config = {
"name": "test_tag",
"color": "Red",
"folder": "Texas",
"comments": "Test tag"
}
# Create the tag using the unified client interface
new_tag = client.tag.create(tag_config)
# Commit changes directly from the client
result = client.commit(
folders=["Texas"],
description="Added test tag",
sync=True
)
# Check job status directly from the client
status = client.get_job_status(result.job_id)
except InvalidObjectError as e:
print(f"Invalid tag data: {e.message}")
except NameNotUniqueError as e:
print(f"Tag name already exists: {e.message}")
except ObjectNotPresentError as e:
print(f"Tag not found: {e.message}")
except ReferenceNotZeroError as e:
print(f"Tag still in use: {e.message}")
except MissingQueryParameterError as e:
print(f"Missing parameter: {e.message}")
Best Practices
-
Client Usage
- Use the unified client interface (
client.tag
) for streamlined code - Create a single client instance and reuse it across your application
- Perform commit operations directly on the client object (
client.commit()
) - For custom max_limit settings, create a dedicated service instance if needed
- Use the unified client interface (
-
Color Management
- Use standard color names from predefined list
- Maintain consistent color schemes
- Document color meanings
- Validate colors before creation
-
Consider color visibility
-
Container Management
- Always specify exactly one container (folder, snippet, or device)
- Use consistent container names
- Validate container existence
-
Group related tags
-
Naming Conventions
- Use descriptive names
- Follow consistent patterns
- Avoid special characters
- Document naming standards
-
Consider hierarchical naming
-
Performance
- Create a single client instance and reuse it
- Use appropriate pagination
- Implement proper retry logic
- Monitor API limits
-
Batch operations when possible
-
Error Handling
- Validate input data
- Handle specific exceptions
- Log error details
- Monitor commit status
- Track job completion
Full Script Examples
Refer to the tag.py example.