Skip to content

Commit

Permalink
Initial poseidon/cue Terraform provider
Browse files Browse the repository at this point in the history
* Add cue_config data source to evaludate CUE contents
and render as JSON
* Evaluate inline CUE content
* Evaluate CUE files specified via paths list
* Support pretty_print option (default false)
  • Loading branch information
dghubble committed Aug 21, 2022
0 parents commit 1129119
Show file tree
Hide file tree
Showing 26 changed files with 1,287 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
pull-request-branch-name:
separator: "-"
open-pull-requests-limit: 3
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
30 changes: 30 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: test
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: go
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
go: ['1.18', '1.19']
steps:
- name: setup Go
uses: actions/setup-go@v3
with:
go-version: ${{matrix.go}}

- name: setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false

- name: checkout
uses: actions/checkout@v3

- name: test
run: make
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
_output/
13 changes: 13 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# terraform-provider-cue

Notable changes between releases.

## Latest

## v0.1.0

* Add `cue_config` data source to evaluate CUE contents
* Evaluate inline CUE `content`
* Evaluate CUE files specified via `paths` list
* Support `pretty_print` option (default false)
* Render CUE values as JSON
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Contributing

## Developer Certificate of Origin

By contributing, you agree to the Linux Foundation's Developer Certificate of Origin ([DCO](DCO)). The DCO is a statement that you, the contributor, have the legal right to make your contribution and understand the contribution will be distributed as part of this project.
36 changes: 36 additions & 0 deletions DCO
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
660 York Street, Suite 102,
San Francisco, CA 94110 USA

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or

(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or

(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.

(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 Dalton Hubble

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
62 changes: 62 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export CGO_ENABLED:=0

VERSION=$(shell git describe --tags --match=v* --always --dirty)
SEMVER=$(shell git describe --tags --match=v* --always --dirty | cut -c 2-)

.PHONY: all
all: build test vet fmt

.PHONY: build
build: clean bin/terraform-provider-cue

bin/terraform-provider-cue:
@go build -o $@ github.com/poseidon/terraform-provider-cue

.PHONY: test
test:
@go test ./... -cover

.PHONY: vet
vet:
@go vet -all ./...

.PHONY: fmt
fmt:
@test -z $$(go fmt ./...)

.PHONY: clean
clean:
@rm -rf bin
@rm -rf _output

.PHONY: release
release: \
clean \
_output/plugin-linux-amd64.zip \
_output/plugin-linux-arm64.zip \
_output/plugin-darwin-amd64.zip \
_output/plugin-darwin-arm64.zip \
_output/plugin-windows-amd64.zip

_output/plugin-%.zip: NAME=terraform-provider-cue_$(SEMVER)_$(subst -,_,$*)
_output/plugin-%.zip: DEST=_output/$(NAME)
_output/plugin-%.zip: _output/%/terraform-provider-cue
@mkdir -p $(DEST)
@cp _output/$*/terraform-provider-cue $(DEST)/terraform-provider-cue_$(VERSION)
@zip -j $(DEST).zip $(DEST)/terraform-provider-cue_$(VERSION)

_output/linux-amd64/terraform-provider-cue: GOARGS = GOOS=linux GOARCH=amd64
_output/linux-arm64/terraform-provider-cue: GOARGS = GOOS=linux GOARCH=arm64
_output/darwin-amd64/terraform-provider-cue: GOARGS = GOOS=darwin GOARCH=amd64
_output/darwin-arm64/terraform-provider-cue: GOARGS = GOOS=darwin GOARCH=arm64
_output/windows-amd64/terraform-provider-cue: GOARGS = GOOS=windows GOARCH=amd64
_output/%/terraform-provider-cue:
$(GOARGS) go build -o $@ github.com/poseidon/terraform-provider-cue

release-sign:
cd _output; sha256sum *.zip > terraform-provider-cue_$(SEMVER)_SHA256SUMS
gpg2 --detach-sign _output/terraform-provider-cue_$(SEMVER)_SHA256SUMS

release-verify: NAME=_output/terraform-provider-cue
release-verify:
gpg2 --verify $(NAME)_$(SEMVER)_SHA256SUMS.sig $(NAME)_$(SEMVER)_SHA256SUMS
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# terraform-provider-cue
[![GoDoc](https://pkg.go.dev/badge/github.com/poseidon/terraform-provider-cue.svg)](https://pkg.go.dev/github.com/poseidon/terraform-provider-cue) [![Workflow](https://github.com/poseidon/terraform-provider-cue/actions/workflows/test.yaml/badge.svg)](https://github.com/poseidon/terraform-provider-cue/actions/workflows/test.yaml?query=branch%3Amain) ![Downloads](https://img.shields.io/github/downloads/poseidon/terraform-provider-cue/total) [![Sponsors](https://img.shields.io/github/sponsors/poseidon?logo=github)](https://github.com/sponsors/poseidon) [![Twitter](https://img.shields.io/badge/follow-news-1da1f2?logo=twitter)](https://twitter.com/poseidonlabs)

`terraform-provider-cue` allows Terraform to evaluate [CUE](https://cuelang.org/docs/) configs and render as JSON for use in Terraform.

<details>
<summary>Author's Note</summary>
CUE has potential to be a better Jsonnet (if it gets a proper module manager). But like Jsonnet, its usage should be limited to preparing JSON-only configs where there are no viable alternatives (e.g. Grafana dashboards). Prefer native Terraform where possible, its ecosystem and design is simpler, more powerful, more mature, and ubiquitous.
</details>

## Usage

Configure the `cue` provider (e.g. `providers.tf`).

```tf
provider "cue" {}
terraform {
required_providers {
ct = {
source = "poseidon/cue"
version = "0.1.0"
}
}
}
```

Run `terraform init` to ensure version requirements are met.

```
$ terraform init -upgrade
```

Define a `cue_config` data source to validate CUE `content`.

```tf
data "cue_config" "example" {
content = <<EOF
a: 1
b: 2
sum: a + b
_hidden: 3
l: [a, b]
map: [string]:int
map: {a: 1 * 5}
map: {"b": b * 5}
EOF
pretty_print = true
}
```

Alternately, provide `paths` to CUE files (supports imports).

```tf
data "cue_config" "example" {
paths = [
"core.cue",
"box.cue",
]
pretty_print = false
}
```

Render the CUE config as JSON for use in Terraform expressions.

```tf
output "out" {
description = "Show Cue rendered as JSON"
value = data.cue_config.example.rendered
}
```

The rendered `content` example looks like:

```json
{
"a": 1,
"b": 2,
"sum": 3,
"l": [
1,
2
],
"map": {
"a": 5,
"b": 10
}
}
```

## Requirements

* Terraform v1.0+ [installed](https://www.terraform.io/downloads.html)

## Development

### Binary

To develop the provider plugin locally, build an executable with Go v1.18+.

```
make
```
1 change: 1 addition & 0 deletions cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module: "github.com/poseidon/terraform-provider-cue"
56 changes: 56 additions & 0 deletions docs/data-sources/cue_config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# cue_config Data Source

`cue_config` allows Terraform to evaluate [CUE](https://cuelang.org/docs/) configs and render JSON for usage.

## Usage

Define a `cue_config` data source to validate CUE `content`.

```tf
data "cue_config" "example" {
content = <<EOF
a: 1
b: 2
sum: a + b
_hidden: 3
l: [a, b]
map: [string]:int
map: {a: 1 * 5}
map: {"b": b * 5}
EOF
pretty_print = true
}
```

Alternately, provide `paths` to CUE files (supports imports).

```tf
data "cue_config" "example" {
paths = [
"core.cue",
"box.cue",
]
pretty_print = false
}
```

Render the CUE config as JSON for use in Terraform expressions.

```tf
output "out" {
description = "Show Cue rendered as JSON"
value = data.cue_config.example.rendered
}
```

## Argument Reference

* `content` - inline CUE contents to evaluate (exclusive with `paths`)
* `paths` - list of paths to CUE files (relative to Terraform workspace) to evaluate (exclusive with `content`)
* `pretty_print` - indent rendered JSON for visual prettiness (default: false)

## Argument Attributes

* `rendered` - JSON output from evaluating CUE content(s)

Loading

0 comments on commit 1129119

Please sign in to comment.