The Pyvider schema system provides a type-safe, declarative way to define the structure and constraints of your provider's resources, data sources, and functions. It bridges Python and Terraform's type systems, enabling proper validation and documentation.
🤖 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.
frompyvider.schemaimportb_main,b_list,b_single# Main block (unnested, attributes at top level)schema=b_main({"name":a_str(required=True),"port":a_num(default=8080),})# List of blocks (0 or more)schema_with_rules=b_main({"name":a_str(required=True),"rule":b_list("rule"),# Multiple rule blocks allowed})# Single block (0 or 1)schema_with_config=b_main({"name":a_str(required=True),"config":b_single("config"),# At most one config block})
frompyvider.schemaimport(s_resource,a_str,a_num,a_bool,a_list,a_map,a_obj,b_list,b_single,)@register_resource("mycloud_server")classServerResource(BaseResource):@classmethoddefget_schema(cls)->PvsSchema:returns_resource({# Required string"name":a_str(required=True,description="Server name"),# Optional number with default"port":a_num(default=8080,description="Port number",validators=[lambdax:1<=x<=65535or"Port must be 1-65535"]),# Optional boolean"enabled":a_bool(default=True,description="Whether server is enabled"),# Computed attribute (provider sets this)"ip_address":a_str(computed=True,description="Server IP address"),# Sensitive attribute (masked in logs)"admin_password":a_str(required=True,sensitive=True,description="Administrator password"),# List of strings"tags":a_list(a_str(),default=[],description="Tags for the server"),# Map of strings"labels":a_map(a_str(),default={},description="Label key-value pairs"),# Complex object"config":a_obj({"timeout":a_num(default=30),"retries":a_num(default=3),"debug":a_bool(default=False),},description="Server configuration"),# Nested block (list)"rule":b_list("rule"),# Multiple firewall rules# Nested block (single)"backup":b_single("backup"),# At most one backup config})
Blocks are named, structured containers:
- Can contain attributes and other blocks
- Support nesting modes (list, set, map, single, group)
- Represented as HCL blocks in Terraform
"port":a_num(required=True,validators=[lambdax:1<=x<=65535or"Port must be between 1 and 65535",lambdax:x!=22or"Port 22 is reserved for SSH",])"email":a_str(required=True,validators=[lambdax:"@"inxor"Must be a valid email address",lambdax:(x.endswith(".com")orx.endswith(".org"))or"Must end with .com or .org",])
Validators return:
- True if valid
- Error message string if invalid
# Common attributes moduledefcommon_name_attr():returna_str(required=True,description="Resource name",validators=[lambdax:len(x)>=3or"Name must be at least 3 characters",lambdax:x.isalnum()or"Name must be alphanumeric",])defcommon_tags_attr():returna_list(a_str(),default=[],description="Resource tags")# Use in resources@classmethoddefget_schema(cls)->PvsSchema:returns_resource({"name":common_name_attr(),"tags":common_tags_attr(),"port":a_num(default=8080),})