Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[terraform] 0.12 support #7

Merged
merged 9 commits into from
Jun 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Terraform 0.12 support
osterman committed Jun 4, 2019
commit 86fec7b3d0fe142fd96d2e6e4f9ed7b4ea71e0a4
3 changes: 3 additions & 0 deletions bin/terraform-docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash


5 changes: 4 additions & 1 deletion test/terraform/input-descriptions.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
load 'lib'

@test "check if terraform inputs have descriptions" {
TMPFILE="$(mktemp /tmp/terraform-docs-XXXXXXXXXXX.json)"
skip_unless_terraform
run bash -c "terraform-docs json . | jq -rS '.Inputs[] | select (.Description == \"\") | .Name'"
terraform_docs json . > $TMPFILE
run bash -c "jq -rS '.Inputs[] | select (.Description == \"\") | .Name' < $TMPFILE"
rm -f $TMPFILE
log "$output"
[ -z "$output" ]
}
16 changes: 16 additions & 0 deletions test/terraform/lib.bash
Original file line number Diff line number Diff line change
@@ -40,3 +40,19 @@ function clean() {
function skip_unless_terraform() {
[[ -n $(echo *.tf) ]] || skip "no *.tf files"
}


function terraform_docs() {
which awk 2>&1 >/dev/null || ( echo "awk not available"; exit 1)
which terraform 2>&1 >/dev/null || ( echo "terraform not available"; exit 1)
which terraform-docs 2>&1 >/dev/null || ( echo "terraform-docs not available"; exit 1)

if [[ "`terraform version`" =~ 0\.12 ]]; then
TMP_FILE="$(mktemp /tmp/terraform-docs-XXXXXXXXXX.tf)"
awk -f ${BUILD_HARNESS_PATH}/bin/terraform-docs.awk $2/*.tf > ${TMP_FILE}
terraform-docs $1 ${TMP_FILE}
rm -f ${TMP_FILE}
else
terraform-docs $1 $2
fi
}
2 changes: 1 addition & 1 deletion test/terraform/module-pinning.bats
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ function teardown() {

@test "check if terraform modules are properly pinned" {
skip_unless_terraform
run bash -c "cat *.tf | json2hcl -reverse | jq -r '..|select(.source?)|.source' | sort -u | sed 's/^.*?ref=//' | grep -Ev '^(tags/[0-9]+\\.[0-9]+.*|)$$'"
run bash -c "grep -Eo '^\s*source\s*=\s*"(.*?)"' *.tf | cut -d'"' -f2 | sort -u | sed 's/^.*?ref=//' | grep -Ev '^(tags/[0-9]+\\.[0-9]+.*|)$$'"
log "$output"
[ $status -ne 0 ]
}
6 changes: 5 additions & 1 deletion test/terraform/output-descriptions.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
load 'lib'

@test "check if terraform outputs have descriptions" {
TMPFILE="$(mktemp /tmp/terraform-docs-XXXXXXXXXXX.json)"
skip_unless_terraform
run bash -c "terraform-docs json . | jq -rS '.Outputs[] | select (.Description == \"\") | .Name'"
terraform_docs json . > $TMPFILE
run bash -c "jq -rS '.Outputs[] | select (.Description == \"\") | .Name' < $TMPFILE"
rm -f $TMPFILE
log "$output"
[ -z "$output" ]
}

45 changes: 45 additions & 0 deletions test/terraform/terraform-docs.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs`
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
# https://github.com/segmentio/terraform-docs/
# https://github.com/segmentio/terraform-docs/issues/62

{
if ( /\{/ ) {
braceCnt++
}

if ( /\}/ ) {
braceCnt--
}

if ($0 ~ /(variable|output) "(.*?)"/) {
blockCnt++
print $0
}

if ($1 == "description") {
print $0
}

if ($1 == "default") {
if (braceCnt > 1) {
print " default = {}"
} else {
print $0
}
}

if ($1 == "type" ) {
type=$3
if (type ~ "object") {
print " type = \"object\""
} else {
print " type = \"" $3 "\""
}
}

if (braceCnt == 0 && blockCnt > 0) {
blockCnt--
print $0
}
}