Skip to content

URL Categories Configuration Object

Table of Contents

  1. Overview
  2. Core Methods
  3. URL Category Model Attributes
  4. Exceptions
  5. Basic Configuration
  6. Usage Examples
  7. Managing Configuration Changes
  8. Error Handling
  9. Best Practices
  10. Full Script Examples
  11. Related Models

Overview

The URLCategories class provides functionality to manage URL category objects in Palo Alto Networks' Strata Cloud Manager. This class inherits from BaseObject and provides methods for creating, retrieving, updating, and deleting custom URL categories that can be used in security policies and profiles.

Core Methods

Method Description Parameters Return Type
create() Creates a new URL category data: Dict[str, Any] URLCategoriesResponseModel
get() Retrieves a category by ID object_id: str URLCategoriesResponseModel
update() Updates an existing category profile: URLCategoriesUpdateModel URLCategoriesResponseModel
delete() Deletes a category object_id: str None
list() Lists categories with filtering folder: str, **filters List[URLCategoriesResponseModel]
fetch() Gets category by name name: str, folder: str URLCategoriesResponseModel

URL Category Model Attributes

Attribute Type Required Description
name str Yes Name of URL category
id UUID Yes* Unique identifier (*response only)
list List[str] Yes List of URLs or patterns
type Enum No Category type (URL List/Category Match)
description str No Category description
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 category data or format
MissingQueryParameterError 400 Missing required parameters
NameNotUniqueError 409 Category name already exists
ObjectNotPresentError 404 Category not found
ReferenceNotZeroError 409 Category still referenced
AuthenticationError 401 Authentication failed
ServerError 500 Internal server error

Basic Configuration

from scm.client import Scm
from scm.config.security import URLCategories
# Initialize clientclient = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)
# Initialize URLCategories objecturl_categories = URLCategories(client)

Usage Examples

Creating URL Categories

# URL List category configurationurl_list_config = {
"name": "blocked_sites",
"type": "URL List",
"list": ["example.com", "test.com"],
"description": "Blocked websites list",
"folder": "Texas"
}
# Create URL List categoryurl_list = url_categories.create(url_list_config)
# Category Match configurationcategory_match_config = {
"name": "social_media",
"type": "Category Match",
"list": ["social-networking", "personal-sites-and-blogs"],
"description": "Social media categories",
"folder": "Texas"
}
# Create Category Match categorycategory_match = url_categories.create(category_match_config)

Retrieving URL Categories

# Fetch by name and foldercategory = url_categories.fetch(name="blocked_sites", folder="Texas")
print(f"Found category: {category.name}")
# Get by IDcategory_by_id = url_categories.get(category.id)
print(f"Retrieved category: {category_by_id.name}")
print(f"URL list: {category_by_id.list}")

Updating URL Categories

# Fetch existing categoryexisting_category = url_categories.fetch(name="blocked_sites", folder="Texas")
# Update URL listexisting_category.list.extend(["newsite.com", "anothersite.com"])
existing_category.description = "Updated blocked websites list"
# Perform updateupdated_category = url_categories.update(existing_category)

Listing URL Categories

# List with direct filter parametersfiltered_categories = url_categories.list(
folder='Texas',
members=['example.com']
)
# Process resultsfor category in filtered_categories:
print(f"Name: {category.name}")
print(f"Type: {category.type}")
print(f"URLs: {category.list}")
# Define filter parameters as dictionarylist_params = {
"folder": "Texas",
"members": ["social-networking"]
}
# List with filters as kwargsfiltered_categories = url_categories.list(**list_params)

Filtering Responses

The list() method supports additional parameters to refine your query results even further. Alongside basic filters (like types, values, and tags), 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): When True, only objects defined exactly in the specified container (folder, snippet, or device) are returned.
  • exclude_folders (List[str]): Provide a list of folder names to exclude.
  • exclude_snippets (List[str]): Provide a list of snippet values to exclude.
  • exclude_devices (List[str]): Provide a list of device values to exclude.

Examples:

# Only return url_categories defined exactly in 'Texas'exact_url_categories = url_categories.list(
folder='Texas',
exact_match=True
)

for app in exact_url_categories:
print(f"Exact match: {app.name} in {app.folder}")
# Exclude all url_categories from the 'All' folderno_all_url_categories = url_categories.list(
folder='Texas',
exclude_folders=['All']
)

for app in no_all_url_categories:
assert app.folder != 'All'
print(f"Filtered out 'All': {app.name}")
# Exclude url_categories that come from 'default' snippetno_default_snippet = url_categories.list(
folder='Texas',
exclude_snippets=['default']
)

for app in no_default_snippet:
assert app.snippet != 'default'
print(f"Filtered out 'default' snippet: {app.name}")
# Exclude url_categories associated with 'DeviceA'no_deviceA = url_categories.list(
folder='Texas',
exclude_devices=['DeviceA']
)

for app in no_deviceA:
assert app.device != 'DeviceA'
print(f"Filtered out 'DeviceA': {app.name}")
# Combine exact_match with multiple exclusionscombined_filters = url_categories.list(
folder='Texas',
exact_match=True,
exclude_folders=['All'],
exclude_snippets=['default'],
exclude_devices=['DeviceA']
)

for app in combined_filters:
print(f"Combined filters result: {app.name} in {app.folder}")

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.

# Initialize the URLCategories object with a custom max_limit# This will retrieve up to 4321 objects per API call, up to the API limit of 5000.category_client = URLCategories(api_client=client, max_limit=4321)
# Now when we call list(), it will use the specified max_limit for each request# while auto-paginating through all available objects.all_categories = category_client.list(folder='Texas')
# 'all_categories' contains all objects from 'Texas', fetched in chunks of up to 4321 at a time.

Deleting URL Categories

# Delete by IDcategory_id = "123e4567-e89b-12d3-a456-426655440000"
url_categories.delete(category_id)

Managing Configuration Changes

Performing Commits

# Prepare commit parameterscommit_params = {
"folders": ["Texas"],
"description": "Updated URL categories",
"sync": True,
"timeout": 300 # 5 minute timeout
}
# Commit the changesresult = url_categories.commit(**commit_params)

print(f"Commit job ID: {result.job_id}")

Monitoring Jobs

# Get status of specific jobjob_status = url_categories.get_job_status(result.job_id)
print(f"Job status: {job_status.data[0].status_str}")
# List recent jobsrecent_jobs = url_categories.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
)

try:
# Create category configuration
category_config = {
"name": "test_category",
"type": "URL List",
"list": ["example.com", "test.com"],
"folder": "Texas",
"description": "Test URL category"
}

# Create the category
new_category = url_categories.create(category_config)

# Commit changes
result = url_categories.commit(
folders=["Texas"],
description="Added test category",
sync=True
)

# Check job status
status = url_categories.get_job_status(result.job_id)

except InvalidObjectError as e:
print(f"Invalid category data: {e.message}")
except NameNotUniqueError as e:
print(f"Category name already exists: {e.message}")
except ObjectNotPresentError as e:
print(f"Category not found: {e.message}")
except ReferenceNotZeroError as e:
print(f"Category still in use: {e.message}")
except MissingQueryParameterError as e:
print(f"Missing parameter: {e.message}")

Best Practices

  1. URL List Management

    • Use descriptive category names
    • Group related URLs together
    • Validate URL patterns before adding
    • Keep lists manageable in size
    • Document category purposes
  2. Container Management

    • Always specify exactly one container (folder, snippet, or device)
    • Use consistent container names
    • Validate container existence
    • Group related categories
  3. Category Types

    • Choose appropriate type (URL List vs Category Match)
    • Validate category match patterns
    • Consider URL resolution impact
    • Document type selection rationale
  4. Performance

    • Use appropriate pagination
    • Cache frequently accessed categories
    • Implement proper retry logic
    • Monitor URL resolution times
  5. Security

    • Follow least privilege principle
    • Validate input URLs
    • Use secure connection settings
    • Monitor category usage

Full Script Examples

Refer to the url_categories.py example.