Attributes define the individual fields in your provider's schemas. Pyvider uses factory functions to create attributes with proper typing and validation.
🤖 AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
port=a_num(default=8080,description="Port number",validators=[lambdax:1<=x<=65535or"Port must be between 1 and 65535"])timeout=a_num(default=30.5,description="Timeout in seconds")
# List of stringstags=a_list(a_str(),default=[],description="Resource tags")# List of numbersports=a_list(a_num(),description="Allowed ports")# List of objectsrules=a_list(a_obj({"port":a_num(required=True),"protocol":a_str(required=True),}),description="Firewall rules")
# Map of stringslabels=a_map(a_str(),default={},description="Label key-value pairs")# Map of numbersquotas=a_map(a_num(),description="Resource quotas by type")
# Tuple with specific types for each elementcoordinates=a_tuple([a_num(),a_num()],# [latitude, longitude]description="Geographic coordinates")# Mixed typesmetadata=a_tuple([a_str(),a_num(),a_bool()],# [name, count, active]description="Resource metadata tuple")
api_key=a_str(required=True,sensitive=True,# Value will be maskeddescription="API key for authentication")credentials=a_obj({"username":a_str(required=True),"password":a_str(required=True,sensitive=True),},description="Login credentials")
# Simple defaultsport=a_num(default=8080)enabled=a_bool(default=True)region=a_str(default="us-east-1")# Collection defaultstags=a_list(a_str(),default=[])labels=a_map(a_str(),default={})# Object defaultsconfig=a_obj({"timeout":a_num(default=30),"retries":a_num(default=3),},description="Configuration with defaults")
A default is the value the framework substitutes when the practitioner omits the
argument. It reaches the configuration the resource reads and the state it plans, so
the plan shows "us-east-1" rather than nothing at all.
Five things follow:
An attribute with a default is Computed. Pyvider marks it so automatically.
Terraform only lets a provider plan a value the configuration does not contain for a
computed attribute; required=True, write_only=True and computed-only attributes are
rejected with a default, as is a default whose type does not match the attribute.
Removing the argument reverts it to the default rather than keeping the value it
last had. This matches terraform-plugin-framework: Terraform Core builds the proposed
new state by falling back to prior state for an Optional + Computed attribute, and the
framework's Default then overwrites that, keying on configuration nullness rather
than plan nullness. (Inside a b_set block the value is kept instead: a set has no
element order, so an element cannot be matched back to the configuration that produced
it.)
default=None means "no default". There is no way to declare an explicit null
default, and none is needed: an optional attribute is already null when omitted.
Nothing absent is invented. A default nested inside an object attribute or a block
is filled only when the practitioner wrote that object or block; a value that is not
yet known ("known after apply") is never replaced.
Every direct a_obj() is a nested type. This applies whether or not the object or
its members declare defaults, and exposes each member's required/optional/computed flags
to Terraform. Compared with the former opaque cty.Object encoding, members marked
optional can be omitted individually instead of every member having to appear in
configuration. The configuration syntax and resulting cty object shape are unchanged.
frompyvider.schemaimporta_str,a_num# Single validatorport=a_num(validators=[lambdax:1<=x<=65535or"Port must be between 1 and 65535"])# Multiple validatorsusername=a_str(required=True,validators=[lambdax:len(x)>=3or"Username must be at least 3 characters",lambdax:x.isalnum()or"Username must be alphanumeric",lambdax:notx.startswith("_")or"Username cannot start with underscore",])# Validators for collectionstags=a_list(a_str(),validators=[lambdax:len(x)<=10or"Maximum 10 tags allowed",lambdax:all(len(tag)<=50fortaginx)or"Tag length must be <= 50 chars",])