# Templating and Property Toolkit

> Use Templating and Property Toolkit features in Terraform

---

Site [llms.txt](/llms.txt)

---

To learn more about this OneFuse feature, refer to [Templating guide and Property Toolkit features](/docs/onefuse/templating/best-practices/).

## Introduction

**The OneFuse Property Toolkit (PTK) is the Swiss army knife of the OneFuse Platform.**

PTK can be used to simplify configurations, define business logic as configuration, build simple logic or tackle the most complex problems. At face value, the PTK is meta-data that can be called upon offering simple name value pairs that can be used as a reference for decision making. In its most powerful form, it can contain logic, decision trees, platform abstractions, and much more.

## Feature highlights

- Minimize automation inputs by presenting simple meaningful inputs, and leveraging policy-driven decisioning
- Reduce configuration sprawl with dynamic property configurations
- Reduce custom code/technical debt in the areas of configurations and business logic
- Cross-platform/tool standardization
- Use OneFuse Property Sets to drive property assignment and rendering
- Supports OneFuse Template Engine.

## Resources

- [Property Sets for Property Toolkit features](/docs/onefuse/templating/property-sets/#property-sets-for-property-toolkit-features)
- [Property Sets for Upstream Providers](/docs/onefuse/templating/property-sets/#property-sets-for-upstream-providers)

## Best practices

### `template_properties` parameter for OneFuse modules

Every OneFuse Module takes an input parameter called `template_properties`:

- Allows users to define their own values for variables within each Module.
- Is an unstructured inputs map that takes multiple values and passes them to OneFuse Modules.

**It is best practice to combine all of your `template_properties` together and pass them collectively into each OneFuse Module**, rather than passing only the needed variables for a particular OneFuse Module. This greatly simplifies Terraform configurations, and allows for:

- Defining a base set of `template_properties` in a `.tfvars` file.
- Merging in additional ones as needed, always ensuring the needed values are passed into each OneFuse Module.

### OneFuse Property Sets for Property Toolkit consumption

The OneFuse Property Toolkit is a very powerful tool within Terraform with the ability to look up data against a standard set of properties.

An example would be a Property Sets for: `Environment` that defines known values for different integrations or services for provisioning:

- You may have a Property Set for the **production** environment that effects the name, network selection, DNS registration, template to be used, etc.
- You may also have a Property Set for the **development** environment that differently effects the name, network selection, DNS registration, template to be used, etc.

**It is a best practice to have all of your Property Set management within a dedicated `.tf` file named `properties.tf`.** This allows a single location where you can review and manage the OneFuse Property Sets and the resultant property associations versus having them scattered throughout the Terraform Module.

---

## Render templates in Terraform

Render OneFuse templates in Terraform using the OneFuse Templating Engine.

### Rendering template specific parameters

| Parameter Name | Notes |
| --- | --- |
| `template` | OneFuse Template string. |
| `template_properties` | (Optional) Additional properties that are referenced and rendered within the Template. |

### Rendering examples

#### Data source example for rendering a template

The following can be added to the `main.tf` file to render Templates.

```hcl
// Data Source
data "onefuse_rendered_template" "environment" {
    template = "{{ environment }}"
    template_properties = {"environment": "production"}
}

// Output
output "rendered_environment" {
  value = data.onefuse_rendered_template.environment.value  // rendered_environment = "production"
}
```

#### Data source example for rendering a templated Property Set

```hcl
// Example OneFuse Property Set called "production" with the following properties:
// {
//   "someKey": "{{templatedValue}}",
//   "someParentKey": {
//     "someNestedKey": "someNestedValue"
//   }
// }

// Data Source
data "onefuse_rendered_template" "rendered_propertyset_prod" {
    template = data.onefuse_property_set.production.raw
    template_properties = {"templatedValue": "Hello World"}
}

// Output of rendered Property Set
output "rendered_propertyset_prod" {
  value = data.onefuse_rendered_template.rendered_propertyset_prod.value
}
// rendered_propertyset_prod:
// {
//   "someKey": "Hello World",
//   "someParentKey": {
//     "someNestedKey": "someNestedValue"
//   }
// }
```

---

## Use OneFuse Property Sets in Terraform

Get OneFuse Property Sets in Terraform with the Property Sets Data Source.

<div class="alert alert-info" role="alert">


Starting OneFuse v1.4, *Static* Property Sets are now **Property Sets**.

Backward compatibility exists, however, it is recommended to use Property Sets.
</div>


### Property Set specific parameters

| Parameter Name | Notes |
| --- | --- |
| `name` | OneFuse Property Set name to retrieve. |
| `template_properties` | (Optional) Additional properties that are rendered within the Property Set. |

#### Outputs

| Attribute Name | Notes |
| --- | --- |
| `properties` | Used to index key:value pairs for the Property Set's properties. |
| `raw` | The Property Set properties raw and rendered output in JSON. |

### Reference nested properties

Reference nested properties within OneFuse Property Sets via a Terraform data source. This allows access to complex property hierarchies from Terraform in your Property Sets.

#### Example

Add the following to the `main.tf` file. Update the values as needed.

```hcl
// Example OneFuse Property Set called "production" with the following properties:
// {
//   "someKey": "someValue",
//   "someParentKey": {
//     "someNestedKey": "someNestedValue"
//   }
// }

// Data Source
data "onefuse_property_set" "production" {
    name = "production"
}

// Output
// Simple key:value pairs can be indexed in the 'properties' attribute
output "production_someKey" {
  value = data.onefuse_property_set.production.properties.someKey
}
```
