brokkr-models::models::stack_templates
Rust
Stack Template Module
This module defines structures and methods for managing stack templates in the system.
Data Model
Stack templates are reusable definitions for deployment objects with parameter placeholders. They use Tera templating syntax and JSON Schema for parameter validation.
id: UUID, primary keycreated_at: TIMESTAMP, when the template was createdupdated_at: TIMESTAMP, when the template was last updateddeleted_at: TIMESTAMP, for soft deletion supportgenerator_id: UUID, nullable - NULL means system template (admin-only)name: VARCHAR(255), name of the templatedescription: TEXT, optional descriptionversion: INTEGER, version number (auto-incremented per name+generator_id)template_content: TEXT, Tera template contentparameters_schema: TEXT, JSON Schema for parameter validationchecksum: VARCHAR(64), SHA-256 checksum of template_content
Constraints
- The
namemust be a non-empty string. - The
template_contentmust be valid Tera syntax. - The
parameters_schemamust be valid JSON Schema. - Unique constraint on (generator_id, name, version).
Structs
Section titled “Structs”struct StackTemplate
Section titled “struct StackTemplate”pub
Derives: Queryable, Selectable, Identifiable, AsChangeset, Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash, ToSchema
Represents a stack template in the database.
Fields
Section titled “Fields”| Name | Type | Description |
|---|---|---|
id |
Uuid |
Unique identifier for the template. |
created_at |
DateTime<Utc> |
Timestamp when the template was created. |
updated_at |
DateTime<Utc> |
Timestamp when the template was last updated. |
deleted_at |
Option<DateTime<Utc>> |
Timestamp for soft deletion, if applicable. |
generator_id |
Option<Uuid> |
Generator ID - NULL for system templates (admin-only). |
name |
String |
Name of the template. |
description |
Option<String> |
Optional description of the template. |
version |
i32 |
Version number (auto-incremented per name+generator_id). |
template_content |
String |
Tera template content. |
parameters_schema |
String |
JSON Schema for parameter validation. |
checksum |
String |
SHA-256 checksum of template_content. |
struct NewStackTemplate
Section titled “struct NewStackTemplate”pub
Derives: Insertable, Debug, Clone, Serialize, Deserialize, ToSchema
Represents a new stack template to be inserted into the database.
Fields
Section titled “Fields”| Name | Type | Description |
|---|---|---|
generator_id |
Option<Uuid> |
Generator ID - NULL for system templates (admin-only). |
name |
String |
Name of the template. |
description |
Option<String> |
Optional description of the template. |
version |
i32 |
Version number. |
template_content |
String |
Tera template content. |
parameters_schema |
String |
JSON Schema for parameter validation. |
checksum |
String |
SHA-256 checksum of template_content. |
Methods
Section titled “Methods”pub
fn new( generator_id: Option<Uuid>, name: String, description: Option<String>, version: i32, template_content: String, parameters_schema: String,) -> Result<Self, String>Creates a new NewStackTemplate instance.
Note
This constructor performs basic validation. Tera syntax and JSON Schema validation should be performed separately before calling this.
Parameters:
| Name | Type | Description |
|---|---|---|
generator_id |
Option<Uuid> |
Optional generator ID. NULL means system template. |
name |
String |
Name of the template. Must be non-empty. |
description |
Option<String> |
Optional description. If provided, must not be empty. |
version |
i32 |
Version number for this template. |
template_content |
String |
Tera template content. |
parameters_schema |
String |
JSON Schema as a string. |
Returns:
Returns Ok(NewStackTemplate) if all parameters are valid,
otherwise returns an Err with a description of the validation failure.
Source
pub fn new( generator_id: Option<Uuid>, name: String, description: Option<String>, version: i32, template_content: String, parameters_schema: String, ) -> Result<Self, String> { // Validate name if name.trim().is_empty() { return Err("Template name cannot be empty".to_string()); }
// Validate description (if provided) if let Some(desc) = &description && desc.trim().is_empty() { return Err("Template description cannot be empty if provided".to_string()); }
// Validate template_content is not empty if template_content.trim().is_empty() { return Err("Template content cannot be empty".to_string()); }
// Validate parameters_schema is not empty if parameters_schema.trim().is_empty() { return Err("Parameters schema cannot be empty".to_string()); }
// Validate version is positive if version < 1 { return Err("Version must be at least 1".to_string()); }
// Generate checksum let checksum = generate_checksum(&template_content);
Ok(NewStackTemplate { generator_id, name, description, version, template_content, parameters_schema, checksum, }) }Functions
Section titled “Functions”fn generate_checksum
Section titled “fn generate_checksum”pub
fn generate_checksum(content: &str) -> StringGenerates a SHA-256 checksum for the given content.
Source
pub fn generate_checksum(content: &str) -> String { let mut hasher = Sha256::new(); hasher.update(content.as_bytes()); format!("{:x}", hasher.finalize())}