Skip to main content

Variable Models

Overview

The Variable models provide a structured way to manage variable resources in Palo Alto Networks' Strata Cloud Manager. These models represent reusable values that can be referenced across configurations. The models handle validation of inputs and outputs when interacting with the SCM API.

Models

The module provides the following Pydantic models:

  • VariableBaseModel: Base model with fields common to all variable operations
  • VariableCreateModel: Model for creating new variables
  • VariableUpdateModel: Model for updating existing variables
  • VariableResponseModel: Response model for variable operations

All models use extra="forbid" configuration, which rejects any fields not explicitly defined in the model.

Model Attributes

VariableBaseModel

AttributeTypeRequiredDefaultDescription
namestrYesNoneName of the variable. Max length: 63 chars
typestrYesNoneVariable type (see Variable Types)
valuestrYesNoneValue of the variable
descriptionstrNoNoneDescription of the variable
folderstrNo*NoneFolder scope. Pattern: ^[a-zA-Z0-9\-_. ]+$. Max: 64
snippetstrNo*NoneSnippet scope. Pattern: ^[a-zA-Z0-9\-_. ]+$. Max: 64
devicestrNo*NoneDevice scope. Pattern: ^[a-zA-Z0-9\-_. ]+$. Max: 64

* Exactly one of folder, snippet, or device must be provided

VariableCreateModel

Inherits all fields from VariableBaseModel without additional fields. Includes container validation.

VariableUpdateModel

Extends VariableBaseModel by adding:

AttributeTypeRequiredDefaultDescription
idUUIDYesNoneThe unique identifier of the variable

VariableResponseModel

Extends VariableBaseModel by adding:

AttributeTypeRequiredDefaultDescription
idUUIDYesNoneThe unique identifier of the variable
overriddenboolNoNoneWhether the variable is overridden
labelsList[str]NoNoneLabels assigned to the variable
parentstrNoNoneParent folder or container
snippetsList[str]NoNoneSnippets associated with the variable
modelstrNoNoneDevice model information
serial_numberstrNoNoneDevice serial number
device_onlyboolNoNoneWhether variable is device-only

Variable Types

The type field must be one of the following values:

TypeDescription
percentPercentage value
countCount/number value
ip-netmaskIP address with subnet mask
zoneSecurity zone
ip-rangeIP address range
ip-wildcardIP wildcard mask
device-priorityDevice priority value
device-idDevice identifier
egress-maxMaximum egress value
as-numberAS number
fqdnFully qualified domain name
portPort number
link-tagLink tag
group-idGroup identifier
rateRate value
router-idRouter identifier
qos-profileQoS profile
timerTimer value

Exceptions

The Variable models can raise the following exceptions during validation:

  • ValueError: Raised when field validation fails (e.g., invalid type, missing container)
  • ValidationError: Raised by Pydantic when model validation fails

Model Validators

Type Validation

The type field is validated to ensure it matches one of the allowed variable types:

from scm.models.setup.variable import VariableCreateModel

# This will raise a validation error
try:
variable = VariableCreateModel(
name="test-var",
type="invalid-type",
value="test",
folder="Texas"
)
except ValueError as e:
print(f"Validation error: {e}")
# Output: type must be one of [...], got invalid-type

Container Validation

Variables must have exactly one container field set (folder, snippet, or device):

from scm.models.setup.variable import VariableCreateModel

# Error: No container specified
try:
VariableCreateModel(
name="test-var",
type="ip-netmask",
value="192.168.1.0/24"
)
except ValueError as e:
print(f"Error: {e}")

# Error: Multiple containers specified
try:
VariableCreateModel(
name="test-var",
type="ip-netmask",
value="192.168.1.0/24",
folder="Texas",
snippet="web-config"
)
except ValueError as e:
print(f"Error: {e}")

Usage Examples

Creating a Variable

from scm.client import Scm

# Initialize client
client = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)

# Using dictionary
variable_data = {
"name": "subnet-variable",
"type": "ip-netmask",
"value": "192.168.1.0/24",
"description": "Network subnet for department A",
"folder": "Texas"
}

response = client.variable.create(variable_data)
print(f"Created variable: {response.name} with ID: {response.id}")

Updating a Variable

from scm.client import Scm

# Initialize client
client = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)

# Fetch existing variable
existing = client.variable.fetch(name="subnet-variable", folder="Texas")

# Modify attributes using dot notation
existing.value = "10.0.0.0/16"
existing.description = "Updated network subnet"

# Pass modified object to update()
updated = client.variable.update(existing)
print(f"Updated variable: {updated.name}")

Listing Variables

from scm.client import Scm

# Initialize client
client = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)

# List all variables
all_variables = client.variable.list()

for variable in all_variables:
print(f"Variable: {variable.name}")
print(f"Type: {variable.type}")
print(f"Value: {variable.value}")

# Filter by type
ip_variables = client.variable.list(type="ip-netmask")

# Filter by labels
labeled_variables = client.variable.list(labels=["network"])

Fetching a Variable by Name

from scm.client import Scm

# Initialize client
client = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)

# Fetch variable by name and folder
variable = client.variable.fetch(name="subnet-variable", folder="Texas")
if variable:
print(f"Found variable: {variable.name}")
print(f"ID: {variable.id}")
print(f"Type: {variable.type}")
print(f"Value: {variable.value}")
else:
print("Variable not found")