https://github.com/taskcluster/terraform-provider-jsone
A terraform provider for json-e templating
https://github.com/taskcluster/terraform-provider-jsone
Last synced: 5 months ago
JSON representation
A terraform provider for json-e templating
- Host: GitHub
- URL: https://github.com/taskcluster/terraform-provider-jsone
- Owner: taskcluster
- License: mpl-2.0
- Archived: true
- Created: 2018-06-05T20:03:18.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-10-14T23:30:05.000Z (over 6 years ago)
- Last Synced: 2024-06-21T20:07:11.897Z (almost 2 years ago)
- Language: Go
- Size: 21.5 KB
- Stars: 1
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# JSON-e Terraform Provider
A provider with a single resource `jsone_template`. This works similarly to the `template_file` resource
but allows us to use json-e constructs like $map to transforms lists and such things.
You can use [the json-e documentation](https://taskcluster.github.io/json-e/) to find out more about
how to use the templates.
## Usage
First `go get -u github.com/taskcluster/terraform-provider-jsone`. At that point, if you have built terraform from
source, simply running `go install` for this package will work. Otherwise, follow along with
[the official docs](https://www.terraform.io/docs/configuration/providers.html#third-party-plugins) on how to use
third party plugins.
Now it is as simple as:
```terraform
locals {
context = {
foo = "bar"
baz = ["bing", "qux"]
}
}
data "jsone_template" "service" {
template = "${file("${path.module}/service.yaml")}"
yaml_context = "${jsonencode(local.context)}"
}
output "rendered" {
value = "${data.jsone_template.service.rendered}"
}
```
The resource takes a string called `template` and either a field called `context` or `yaml_context`.
These are mutually exclusive. `context` should be good enough for most cases and is just a terraform map,
but if you run into issues with how terraform handles mixed-type maps or need to pass in typed data, opt
for `yaml_context` which just takes a string containing yaml. Oftentimes using the terraform interpolation
of `jsonencode` will be your best bet.
You can access the rendered template with `.rendered` much the same as with `template_file`. This will normally
be in `json` format unless you explicitely pass in `format = "yaml"` to the resource.
### Multiple Templates
In some cases, it is useful to have multiple templates in the input file.
In this case, the `rendered` value is a list of JSON- (or YAML-) formatted rendered templates.
Use the `jsone_templates` resource (note the plural) for this purpose.
```yaml
# service.yaml
---
one: '{$eval: "17 - 16"}'
---
two: '{$eval: "12 / 6"}'
```
```terraform
data "jsone_templates" "service" {
template = "${file("${path.module}/service.yaml")}"
yaml_context = "${jsonencode(local.context)}"
}
output "rendered" {
value1 = "${data.jsone_template.service.rendered[0]}"
# --> '{"one": 1}'
value2 = "${data.jsone_template.service.rendered[1]}"
# --> '{"two": 2}'
}
```
### Extra Functions
This provider adds a `base64encode()` function to the context that can be used as follows:
```terraform
foo: {$eval: "base64encode(something)"}
```
# Development
Go requirements are the same as Terraform itself (currently 1.9).
In a clean GOPATH, `go get github.com/taskcluster/terraform-provider-jsone`, then switch to that directory and run `go get -t ./...` to get dependencies.
Run `go test ./..` to test.