diff --git a/.gitignore b/.gitignore index 82d1b5c..f4795f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .kitchen/ .kitchen.local.yml +Gemfile.lock +credentials.json diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2eae1fd --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,27 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on +[Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to +[Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [1.0.0] - 2019-YY-ZZ + +### Changed + +- Supported version of Terraform is 0.12. [#7] + +## [0.1.0] + +### Added + +- Initial release + +[Unreleased]: https://github.com/terraform-google-modules/terraform-google-pubsub/compare/v0.1.0...HEAD +[0.1.0]: https://github.com/terraform-google-modules/terraform-google-pubsub/releases/tag/v0.1.0 + +[#7]: https://github.com/terraform-google-modules/terraform-google-pubsub/pull/7 diff --git a/Gemfile b/Gemfile index 81f83c6..7186bbc 100644 --- a/Gemfile +++ b/Gemfile @@ -15,5 +15,6 @@ source 'https://rubygems.org/' gem 'test-kitchen' -gem 'kitchen-terraform', '~> 4.0.3' +gem 'kitchen-terraform', '~> 4.9.0' gem 'kitchen-inspec' + diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 6cd1f0d..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,260 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - addressable (2.5.2) - public_suffix (>= 2.0.2, < 4.0) - aws-sdk (2.11.156) - aws-sdk-resources (= 2.11.156) - aws-sdk-core (2.11.156) - aws-sigv4 (~> 1.0) - jmespath (~> 1.0) - aws-sdk-resources (2.11.156) - aws-sdk-core (= 2.11.156) - aws-sigv4 (1.0.3) - azure_graph_rbac (0.17.0) - ms_rest_azure (~> 0.11.0) - azure_mgmt_key_vault (0.17.2) - ms_rest_azure (~> 0.11.0) - azure_mgmt_resources (0.17.2) - ms_rest_azure (~> 0.11.0) - builder (3.2.3) - coderay (1.1.2) - concurrent-ruby (1.0.5) - declarative (0.0.10) - declarative-option (0.1.0) - diff-lcs (1.3) - docker-api (1.34.2) - excon (>= 0.47.0) - multi_json - domain_name (0.5.20180417) - unf (>= 0.0.5, < 1.0.0) - dry-configurable (0.7.0) - concurrent-ruby (~> 1.0) - dry-container (0.6.0) - concurrent-ruby (~> 1.0) - dry-configurable (~> 0.1, >= 0.1.3) - dry-core (0.4.7) - concurrent-ruby (~> 1.0) - dry-equalizer (0.2.1) - dry-inflector (0.1.2) - dry-logic (0.4.2) - dry-container (~> 0.2, >= 0.2.6) - dry-core (~> 0.2) - dry-equalizer (~> 0.2) - dry-types (0.13.2) - concurrent-ruby (~> 1.0) - dry-container (~> 0.3) - dry-core (~> 0.4, >= 0.4.4) - dry-equalizer (~> 0.2) - dry-inflector (~> 0.1, >= 0.1.2) - dry-logic (~> 0.4, >= 0.4.2) - dry-validation (0.12.2) - concurrent-ruby (~> 1.0) - dry-configurable (~> 0.1, >= 0.1.3) - dry-core (~> 0.2, >= 0.2.1) - dry-equalizer (~> 0.2) - dry-logic (~> 0.4, >= 0.4.0) - dry-types (~> 0.13.1) - erubis (2.7.0) - excon (0.62.0) - faraday (0.15.3) - multipart-post (>= 1.2, < 3) - faraday-cookie_jar (0.0.6) - faraday (>= 0.7.4) - http-cookie (~> 1.0.0) - faraday_middleware (0.12.2) - faraday (>= 0.7.4, < 1.0) - ffi (1.9.25) - google-api-client (0.23.9) - addressable (~> 2.5, >= 2.5.1) - googleauth (>= 0.5, < 0.7.0) - httpclient (>= 2.8.1, < 3.0) - mime-types (~> 3.0) - representable (~> 3.0) - retriable (>= 2.0, < 4.0) - signet (~> 0.9) - googleauth (0.6.7) - faraday (~> 0.12) - jwt (>= 1.4, < 3.0) - memoist (~> 0.16) - multi_json (~> 1.11) - os (>= 0.9, < 2.0) - signet (~> 0.7) - gssapi (1.2.0) - ffi (>= 1.0.1) - gyoku (1.3.1) - builder (>= 2.1.2) - hashie (3.6.0) - htmlentities (4.3.4) - http-cookie (1.0.3) - domain_name (~> 0.5) - httpclient (2.8.3) - inifile (3.0.0) - inspec (2.3.24) - addressable (~> 2.4) - faraday (>= 0.9.0) - faraday_middleware (~> 0.12.2) - hashie (~> 3.4) - htmlentities - json (>= 1.8, < 3.0) - method_source (~> 0.8) - mixlib-log - multipart-post - parallel (~> 1.9) - parslet (~> 1.5) - pry (~> 0) - rspec (~> 3) - rspec-its (~> 1.2) - rubyzip (~> 1.2, >= 1.2.2) - semverse - sslshake (~> 1.2) - term-ansicolor - thor (~> 0.20) - tomlrb (~> 1.2) - train (~> 1.5) - jmespath (1.4.0) - json (2.1.0) - jwt (2.1.0) - kitchen-inspec (0.25.0) - hashie (~> 3.4) - inspec (>= 0.34.0, < 4.0.0) - test-kitchen (~> 1.6) - kitchen-terraform (4.0.3) - dry-types (~> 0.9) - dry-validation (~> 0.10) - inspec (~> 2.2, != 2.3.5, != 2.3.4, != 2.2.112, != 2.2.102, != 2.2.101) - mixlib-shellout (~> 2.2) - test-kitchen (~> 1.23) - little-plugger (1.1.4) - logging (2.2.2) - little-plugger (~> 1.1) - multi_json (~> 1.10) - memoist (0.16.0) - method_source (0.9.0) - mime-types (3.2.2) - mime-types-data (~> 3.2015) - mime-types-data (3.2018.0812) - mixlib-install (3.11.5) - mixlib-shellout - mixlib-versioning - thor - mixlib-log (2.0.4) - mixlib-shellout (2.4.0) - mixlib-versioning (1.2.2) - ms_rest (0.7.3) - concurrent-ruby (~> 1.0) - faraday (~> 0.9) - timeliness (~> 0.3) - ms_rest_azure (0.11.0) - concurrent-ruby (~> 1.0) - faraday (~> 0.9) - faraday-cookie_jar (~> 0.0.6) - ms_rest (~> 0.7.2) - multi_json (1.13.1) - multipart-post (2.0.0) - net-scp (1.2.1) - net-ssh (>= 2.6.5) - net-ssh (4.2.0) - net-ssh-gateway (1.3.0) - net-ssh (>= 2.6.5) - nori (2.6.0) - os (1.0.0) - parallel (1.12.1) - parslet (1.8.2) - pry (0.11.3) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - public_suffix (3.0.3) - representable (3.0.4) - declarative (< 0.1.0) - declarative-option (< 0.2.0) - uber (< 0.2.0) - retriable (3.1.2) - rspec (3.8.0) - rspec-core (~> 3.8.0) - rspec-expectations (~> 3.8.0) - rspec-mocks (~> 3.8.0) - rspec-core (3.8.0) - rspec-support (~> 3.8.0) - rspec-expectations (3.8.2) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-its (1.2.0) - rspec-core (>= 3.0.0) - rspec-expectations (>= 3.0.0) - rspec-mocks (3.8.0) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-support (3.8.0) - rubyntlm (0.6.2) - rubyzip (1.2.2) - semverse (2.0.0) - signet (0.11.0) - addressable (~> 2.3) - faraday (~> 0.9) - jwt (>= 1.5, < 3.0) - multi_json (~> 1.10) - sslshake (1.2.0) - term-ansicolor (1.6.0) - tins (~> 1.0) - test-kitchen (1.23.2) - mixlib-install (~> 3.6) - mixlib-shellout (>= 1.2, < 3.0) - net-scp (~> 1.1) - net-ssh (>= 2.9, < 5.0) - net-ssh-gateway (~> 1.2) - thor (~> 0.19) - winrm (~> 2.0) - winrm-elevated (~> 1.0) - winrm-fs (~> 1.1) - thor (0.20.0) - timeliness (0.3.8) - tins (1.17.0) - tomlrb (1.2.7) - train (1.5.4) - aws-sdk (~> 2) - azure_graph_rbac (~> 0.16) - azure_mgmt_key_vault (~> 0.17) - azure_mgmt_resources (~> 0.15) - docker-api (~> 1.26) - google-api-client (~> 0.23.9) - googleauth (~> 0.6.6) - inifile - json (>= 1.8, < 3.0) - mixlib-shellout (~> 2.0) - net-scp (~> 1.2) - net-ssh (>= 2.9, < 6.0) - winrm (~> 2.0) - winrm-fs (~> 1.0) - uber (0.1.0) - unf (0.1.4) - unf_ext - unf_ext (0.0.7.5) - winrm (2.3.0) - builder (>= 2.1.2) - erubis (~> 2.7) - gssapi (~> 1.2) - gyoku (~> 1.0) - httpclient (~> 2.2, >= 2.2.0.2) - logging (>= 1.6.1, < 3.0) - nori (~> 2.0) - rubyntlm (~> 0.6.0, >= 0.6.1) - winrm-elevated (1.1.0) - winrm (~> 2.0) - winrm-fs (~> 1.0) - winrm-fs (1.3.1) - erubis (~> 2.7) - logging (>= 1.6.1, < 3.0) - rubyzip (~> 1.1) - winrm (~> 2.0) - -PLATFORMS - ruby - -DEPENDENCIES - kitchen-inspec - kitchen-terraform (~> 4.0.3) - test-kitchen - -BUNDLED WITH - 1.16.6 diff --git a/README.md b/README.md index 7c2ae46..b96d140 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ This module makes it easy to create Google Cloud Pub/Sub topic and subscriptions associated with the topic. +## Compatibility + +This module is meant for use with Terraform 0.12. If you haven't [upgraded][terraform-0.12-upgrade] and need a Terraform 0.11.x-compatible version of this module, the last released version intended for Terraform 0.11.x +is [0.2.0][v0.2.0]. + ## Usage This is a simple usage of the module. Please see also a simple setup provided in the example directory. @@ -30,35 +35,34 @@ module "pubsub" { } ``` -[^]: (autogen_docs_start) - + ## Inputs | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| -| project_id | The project ID to manage the Pub/Sub resources | string | - | yes | -| pull_subscriptions | The list of the pull subscriptions | list | `` | no | -| push_subscriptions | The list of the push subscriptions | list | `` | no | -| topic | The Pub/Sub topic name | string | - | yes | +| project\_id | The project ID to manage the Pub/Sub resources | string | n/a | yes | +| pull\_subscriptions | The list of the pull subscriptions | list(map(string)) | `` | no | +| push\_subscriptions | The list of the push subscriptions | list(map(string)) | `` | no | +| topic | The Pub/Sub topic name | string | n/a | yes | ## Outputs | Name | Description | |------|-------------| | id | The ID of the Pub/Sub topic | -| subscription_names | The name list of Pub/Sub subscriptions | -| subscription_paths | The path list of Pub/Sub subscriptions | +| subscription\_names | The name list of Pub/Sub subscriptions | +| subscription\_paths | The path list of Pub/Sub subscriptions | | topic | The name of the Pub/Sub topic | | uri | The URI of the Pub/Sub topic | -[^]: (autogen_docs_end) + ## Requirements ### Installation Dependencies -- [terraform](https://www.terraform.io/downloads.html) 0.11.x -- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v1.12.x +- [terraform](https://www.terraform.io/downloads.html) 0.12.x +- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v2.7.x ### Configure a Service Account @@ -159,4 +163,7 @@ are as follows: - Golang - gofmt. gofmt comes with the standard golang installation. golang -s a compiled language so there is no standard linter. - Terraform - terraform has a built-in linter in the `terraform validate` command. -- Dockerfiles - hadolint. Can be found in homebrew \ No newline at end of file +- Dockerfiles - hadolint. Can be found in homebrew + +[v0.2.0]: https://registry.terraform.io/modules/terraform-google-modules/pubsub/google/0.2.0 +[terraform-0.12-upgrade]: https://www.terraform.io/upgrade-guides/0-12.html diff --git a/examples/simple/README.md b/examples/simple/README.md new file mode 100644 index 0000000..1dff37a --- /dev/null +++ b/examples/simple/README.md @@ -0,0 +1,35 @@ +# Simple Example + +This example illustrates how to use the `pubsub` module. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| project\_id | The project ID to manage the Pub/Sub resources | string | n/a | yes | +| topic\_name | The name for the Pub/Sub topic | string | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| project\_id | The project ID | +| topic\_name | The name of the Pub/Sub topic created | + + + +## Requirements + +The following sections describe the requirements which must be met in +order to invoke this example. The requirements of the +[root module][root-module-requirements] must be met. + +## Usage + +To provision this example, populate `terraform.tfvars` with the [required variables](#inputs) and run the following commands within +this directory: +- `terraform init` to get the plugins +- `terraform plan` to see the infrastructure plan +- `terraform apply` to apply the infrastructure build +- `terraform destroy` to destroy the built infrastructure diff --git a/examples/simple/example.tf b/examples/simple/main.tf similarity index 97% rename from examples/simple/example.tf rename to examples/simple/main.tf index c3475d1..6115382 100644 --- a/examples/simple/example.tf +++ b/examples/simple/main.tf @@ -15,14 +15,14 @@ */ provider "google" { - version = "~> 1.19" + version = "~> 2.7" region = "us-central1" } module "pubsub" { source = "../../" - topic = "${var.topic_name}" project_id = "${var.project_id}" + topic = "${var.topic_name}" pull_subscriptions = [ { @@ -34,9 +34,10 @@ module "pubsub" { push_subscriptions = [ { name = "push" - ack_deadline_seconds = 20 - push_endpoint = "https://${var.project_id}.appspot.com" + push_endpoint = "https://${var.project_id}.appspot.com/" x-goog-version = "v1beta1" + ack_deadline_seconds = 20 }, ] + } diff --git a/examples/simple/outputs.tf b/examples/simple/outputs.tf new file mode 100644 index 0000000..25b632c --- /dev/null +++ b/examples/simple/outputs.tf @@ -0,0 +1,26 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +output "project_id" { + value = var.project_id + description = "The project ID" +} + +output "topic_name" { + value = var.topic_name + description = "The name of the Pub/Sub topic created" +} + diff --git a/examples/simple/variables.tf b/examples/simple/variables.tf index 2796070..3267d8b 100644 --- a/examples/simple/variables.tf +++ b/examples/simple/variables.tf @@ -15,9 +15,11 @@ */ variable "project_id" { - description = "The project id to run tests against" + type = string + description = "The project ID to manage the Pub/Sub resources" } variable "topic_name" { - description = "The name for the topic" + type = string + description = "The name for the Pub/Sub topic" } diff --git a/helpers/combine_docfiles.py b/helpers/combine_docfiles.py deleted file mode 100644 index b01af7c..0000000 --- a/helpers/combine_docfiles.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env python3 - -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -''' Combine file from: - * script argument 1 - with content of file from: - * script argument 2 - using the beginning of line separators - hardcoded using regexes in this file: - - We exclude any text using the separate - regex specified here -''' - -import re -import sys - -insert_separator_regex = '(.*?\[\^\]\:\ \(autogen_docs_start\))(.*?)(\n\[\^\]\:\ \(autogen_docs_end\).*?$)' -exclude_separator_regex = '(.*?)Copyright 20\d\d Google LLC.*?limitations under the License.(.*?)$' - -if len(sys.argv) != 3: - sys.exit(1) - -input = open(sys.argv[1], "r").read() -replace_content = open(sys.argv[2], "r").read() - -# Exclude the specified content from the replacement content -groups = re.match(exclude_separator_regex, replace_content, re.DOTALL).groups(0) -replace_content = groups[0] + groups[1] - -# Find where to put the replacement content, overwrite the input file -groups = re.match(insert_separator_regex, input, re.DOTALL).groups(0) -output = groups[0] + replace_content + groups[2] -open(sys.argv[1], "w").write(output) diff --git a/helpers/terraform_docs b/helpers/terraform_docs new file mode 100755 index 0000000..0935b69 --- /dev/null +++ b/helpers/terraform_docs @@ -0,0 +1,694 @@ +#!/usr/bin/env bash + +set -e + +main() { + declare argv + argv=$(getopt -o a: --long args: -- "$@") || return + eval "set -- $argv" + + declare args + declare files + + for argv; do + case $argv in + (-a|--args) + shift + args="$1" + shift + ;; + (--) + shift + files="$@" + break + ;; + esac + done + + local hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12) + + if [[ "$hack_terraform_docs" == "1" ]]; then + which awk 2>&1 >/dev/null || ( echo "awk is required for terraform-docs hack to work with Terraform 0.12"; exit 1) + + tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") + terraform_docs_awk "$tmp_file_awk" + terraform_docs "$tmp_file_awk" "$args" "$files" + rm -f "$tmp_file_awk" + else + terraform_docs "0" "$args" "$files" + fi + +} + +terraform_docs() { + readonly terraform_docs_awk_file="$1" + readonly args="$2" + readonly files="$3" + + declare -a paths + declare -a tfvars_files + + index=0 + + for file_with_path in $files; do + file_with_path="${file_with_path// /__REPLACED__SPACE__}" + + paths[index]=$(dirname "$file_with_path") + + if [[ "$file_with_path" == *".tfvars" ]]; then + tfvars_files+=("$file_with_path") + fi + + ((index+=1)) + done + + readonly tmp_file=$(mktemp) + readonly text_file="README.md" + + for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do + path_uniq="${path_uniq//__REPLACED__SPACE__/ }" + + pushd "$path_uniq" > /dev/null + + if [[ ! -f "$text_file" ]]; then + popd > /dev/null + continue + fi + + if [[ "$terraform_docs_awk_file" == "0" ]]; then + terraform-docs $args md ./ > "$tmp_file" + else + # Can't append extension for mktemp, so renaming instead + tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") + mv "$tmp_file_docs" "$tmp_file_docs.tf" + tmp_file_docs_tf="$tmp_file_docs.tf" + + awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf" + terraform-docs $args md "$tmp_file_docs_tf" > "$tmp_file" + rm -f "$tmp_file_docs_tf" + fi + + # Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834 + perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file" + + # Replace placeholder with the content of the file + perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", ; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$text_file" + + rm -f "$tmp_file" + + popd > /dev/null + done +} + +terraform_docs_awk() { + readonly output_file=$1 + + cat <<"EOF" > $output_file +# 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 + +# Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk + +{ + if ( $0 ~ /\{/ ) { + braceCnt++ + } + + if ( $0 ~ /\}/ ) { + braceCnt-- + } + + # [START] variable or output block started + if ($0 ~ /^[[:space:]]*(variable|output)[[:space:]][[:space:]]*"(.*?)"/) { + # Normalize the braceCnt (should be 1 now) + braceCnt = 1 + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + blockCnt++ + print $0 + } + + # [START] multiline default statement started + if (blockCnt > 0) { + if ($0 ~ /^[[:space:]][[:space:]]*(default)[[:space:]][[:space:]]*=/) { + if ($3 ~ "null") { + print " default = \"null\"" + } else { + print $0 + blockDefCnt++ + blockDefStart=1 + } + } + } + + # [PRINT] single line "description" + if (blockCnt > 0) { + if (blockDefCnt == 0) { + if ($0 ~ /^[[:space:]][[:space:]]*description[[:space:]][[:space:]]*=/) { + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + print $0 + } + } + } + + # [PRINT] single line "type" + if (blockCnt > 0) { + if ($0 ~ /^[[:space:]][[:space:]]*type[[:space:]][[:space:]]*=/ ) { + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + type=$3 + if (type ~ "object") { + print " type = \"object\"" + } else { + # legacy quoted types: "string", "list", and "map" + if ($3 ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) { + print " type = " $3 + } else { + print " type = \"" $3 "\"" + } + } + } + } + + # [CLOSE] variable/output block + if (blockCnt > 0) { + if (braceCnt == 0 && blockCnt > 0) { + blockCnt-- + print $0 + } + } + + # [PRINT] Multiline "default" statement + if (blockCnt > 0 && blockDefCnt > 0) { + if (blockDefStart == 1) { + blockDefStart = 0 + } else { + print $0 + } + } +} +EOF + +} + +getopt() { + # pure-getopt, a drop-in replacement for GNU getopt in pure Bash. + # version 1.4.3 + # + # Copyright 2012-2018 Aron Griffis + # + # 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. + + _getopt_main() { + # Returns one of the following statuses: + # 0 success + # 1 error parsing parameters + # 2 error in getopt invocation + # 3 internal error + # 4 reserved for -T + # + # For statuses 0 and 1, generates normalized and shell-quoted + # "options -- parameters" on stdout. + + declare parsed status + declare short long name flags + declare have_short=false + + # Synopsis from getopt man-page: + # + # getopt optstring parameters + # getopt [options] [--] optstring parameters + # getopt [options] -o|--options optstring [options] [--] parameters + # + # The first form can be normalized to the third form which + # _getopt_parse() understands. The second form can be recognized after + # first parse when $short hasn't been set. + + if [[ -n ${GETOPT_COMPATIBLE+isset} || $1 == [^-]* ]]; then + # Enable compatibility mode + flags=c$flags + # Normalize first to third synopsis form + set -- -o "$1" -- "${@:2}" + fi + + # First parse always uses flags=p since getopt always parses its own + # arguments effectively in this mode. + parsed=$(_getopt_parse getopt ahl:n:o:qQs:TuV \ + alternative,help,longoptions:,name:,options:,quiet,quiet-output,shell:,test,version \ + p "$@") + status=$? + if [[ $status != 0 ]]; then + if [[ $status == 1 ]]; then + echo "Try \`getopt --help' for more information." >&2 + # Since this is the first parse, convert status 1 to 2 + status=2 + fi + return $status + fi + eval "set -- $parsed" + + while [[ $# -gt 0 ]]; do + case $1 in + (-a|--alternative) + flags=a$flags ;; + + (-h|--help) + _getopt_help + return 2 # as does GNU getopt + ;; + + (-l|--longoptions) + long="$long${long:+,}$2" + shift ;; + + (-n|--name) + name=$2 + shift ;; + + (-o|--options) + short=$2 + have_short=true + shift ;; + + (-q|--quiet) + flags=q$flags ;; + + (-Q|--quiet-output) + flags=Q$flags ;; + + (-s|--shell) + case $2 in + (sh|bash) + flags=${flags//t/} ;; + (csh|tcsh) + flags=t$flags ;; + (*) + echo 'getopt: unknown shell after -s or --shell argument' >&2 + echo "Try \`getopt --help' for more information." >&2 + return 2 ;; + esac + shift ;; + + (-u|--unquoted) + flags=u$flags ;; + + (-T|--test) + return 4 ;; + + (-V|--version) + echo "pure-getopt 1.4.3" + return 0 ;; + + (--) + shift + break ;; + esac + + shift + done + + if ! $have_short; then + # $short was declared but never set, not even to an empty string. + # This implies the second form in the synopsis. + if [[ $# == 0 ]]; then + echo 'getopt: missing optstring argument' >&2 + echo "Try \`getopt --help' for more information." >&2 + return 2 + fi + short=$1 + have_short=true + shift + fi + + if [[ $short == -* ]]; then + # Leading dash means generate output in place rather than reordering, + # unless we're already in compatibility mode. + [[ $flags == *c* ]] || flags=i$flags + short=${short#?} + elif [[ $short == +* ]]; then + # Leading plus means POSIXLY_CORRECT, unless we're already in + # compatibility mode. + [[ $flags == *c* ]] || flags=p$flags + short=${short#?} + fi + + # This should fire if POSIXLY_CORRECT is in the environment, even if + # it's an empty string. That's the difference between :+ and + + flags=${POSIXLY_CORRECT+p}$flags + + _getopt_parse "${name:-getopt}" "$short" "$long" "$flags" "$@" + } + + _getopt_parse() { + # Inner getopt parser, used for both first parse and second parse. + # Returns 0 for success, 1 for error parsing, 3 for internal error. + # In the case of status 1, still generates stdout with whatever could + # be parsed. + # + # $flags is a string of characters with the following meanings: + # a - alternative parsing mode + # c - GETOPT_COMPATIBLE + # i - generate output in place rather than reordering + # p - POSIXLY_CORRECT + # q - disable error reporting + # Q - disable normal output + # t - quote for csh/tcsh + # u - unquoted output + + declare name="$1" short="$2" long="$3" flags="$4" + shift 4 + + # Split $long on commas, prepend double-dashes, strip colons; + # for use with _getopt_resolve_abbrev + declare -a longarr + _getopt_split longarr "$long" + longarr=( "${longarr[@]/#/--}" ) + longarr=( "${longarr[@]%:}" ) + longarr=( "${longarr[@]%:}" ) + + # Parse and collect options and parameters + declare -a opts params + declare o alt_recycled=false error=0 + + while [[ $# -gt 0 ]]; do + case $1 in + (--) + params=( "${params[@]}" "${@:2}" ) + break ;; + + (--*=*) + o=${1%%=*} + if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then + error=1 + elif [[ ,"$long", == *,"${o#--}"::,* ]]; then + opts=( "${opts[@]}" "$o" "${1#*=}" ) + elif [[ ,"$long", == *,"${o#--}":,* ]]; then + opts=( "${opts[@]}" "$o" "${1#*=}" ) + elif [[ ,"$long", == *,"${o#--}",* ]]; then + if $alt_recycled; then o=${o#-}; fi + _getopt_err "$name: option '$o' doesn't allow an argument" + error=1 + else + echo "getopt: assertion failed (1)" >&2 + return 3 + fi + alt_recycled=false + ;; + + (--?*) + o=$1 + if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then + error=1 + elif [[ ,"$long", == *,"${o#--}",* ]]; then + opts=( "${opts[@]}" "$o" ) + elif [[ ,"$long", == *,"${o#--}::",* ]]; then + opts=( "${opts[@]}" "$o" '' ) + elif [[ ,"$long", == *,"${o#--}:",* ]]; then + if [[ $# -ge 2 ]]; then + shift + opts=( "${opts[@]}" "$o" "$1" ) + else + if $alt_recycled; then o=${o#-}; fi + _getopt_err "$name: option '$o' requires an argument" + error=1 + fi + else + echo "getopt: assertion failed (2)" >&2 + return 3 + fi + alt_recycled=false + ;; + + (-*) + if [[ $flags == *a* ]]; then + # Alternative parsing mode! + # Try to handle as a long option if any of the following apply: + # 1. There's an equals sign in the mix -x=3 or -xy=3 + # 2. There's 2+ letters and an abbreviated long match -xy + # 3. There's a single letter and an exact long match + # 4. There's a single letter and no short match + o=${1::2} # temp for testing #4 + if [[ $1 == *=* || $1 == -?? || \ + ,$long, == *,"${1#-}"[:,]* || \ + ,$short, != *,"${o#-}"[:,]* ]]; then + o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2>/dev/null) + case $? in + (0) + # Unambiguous match. Let the long options parser handle + # it, with a flag to get the right error message. + set -- "-$1" "${@:2}" + alt_recycled=true + continue ;; + (1) + # Ambiguous match, generate error and continue. + _getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" >/dev/null + error=1 + shift + continue ;; + (2) + # No match, fall through to single-character check. + true ;; + (*) + echo "getopt: assertion failed (3)" >&2 + return 3 ;; + esac + fi + fi + + o=${1::2} + if [[ "$short" == *"${o#-}"::* ]]; then + if [[ ${#1} -gt 2 ]]; then + opts=( "${opts[@]}" "$o" "${1:2}" ) + else + opts=( "${opts[@]}" "$o" '' ) + fi + elif [[ "$short" == *"${o#-}":* ]]; then + if [[ ${#1} -gt 2 ]]; then + opts=( "${opts[@]}" "$o" "${1:2}" ) + elif [[ $# -ge 2 ]]; then + shift + opts=( "${opts[@]}" "$o" "$1" ) + else + _getopt_err "$name: option requires an argument -- '${o#-}'" + error=1 + fi + elif [[ "$short" == *"${o#-}"* ]]; then + opts=( "${opts[@]}" "$o" ) + if [[ ${#1} -gt 2 ]]; then + set -- "$o" "-${1:2}" "${@:2}" + fi + else + if [[ $flags == *a* ]]; then + # Alternative parsing mode! Report on the entire failed + # option. GNU includes =value but we omit it for sanity with + # very long values. + _getopt_err "$name: unrecognized option '${1%%=*}'" + else + _getopt_err "$name: invalid option -- '${o#-}'" + if [[ ${#1} -gt 2 ]]; then + set -- "$o" "-${1:2}" "${@:2}" + fi + fi + error=1 + fi ;; + + (*) + # GNU getopt in-place mode (leading dash on short options) + # overrides POSIXLY_CORRECT + if [[ $flags == *i* ]]; then + opts=( "${opts[@]}" "$1" ) + elif [[ $flags == *p* ]]; then + params=( "${params[@]}" "$@" ) + break + else + params=( "${params[@]}" "$1" ) + fi + esac + + shift + done + + if [[ $flags == *Q* ]]; then + true # generate no output + else + echo -n ' ' + if [[ $flags == *[cu]* ]]; then + printf '%s -- %s' "${opts[*]}" "${params[*]}" + else + if [[ $flags == *t* ]]; then + _getopt_quote_csh "${opts[@]}" -- "${params[@]}" + else + _getopt_quote "${opts[@]}" -- "${params[@]}" + fi + fi + echo + fi + + return $error + } + + _getopt_err() { + if [[ $flags != *q* ]]; then + printf '%s\n' "$1" >&2 + fi + } + + _getopt_resolve_abbrev() { + # Resolves an abbrevation from a list of possibilities. + # If the abbreviation is unambiguous, echoes the expansion on stdout + # and returns 0. If the abbreviation is ambiguous, prints a message on + # stderr and returns 1. (For first parse this should convert to exit + # status 2.) If there is no match at all, prints a message on stderr + # and returns 2. + declare a q="$1" + declare -a matches + shift + for a; do + if [[ $q == "$a" ]]; then + # Exact match. Squash any other partial matches. + matches=( "$a" ) + break + elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q" ]]; then + # Exact alternative match. Squash any other partial matches. + matches=( "$a" ) + break + elif [[ $a == "$q"* ]]; then + # Abbreviated match. + matches=( "${matches[@]}" "$a" ) + elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q"* ]]; then + # Abbreviated alternative match. + matches=( "${matches[@]}" "${a#-}" ) + fi + done + case ${#matches[@]} in + (0) + [[ $flags == *q* ]] || \ + printf "$name: unrecognized option %s\\n" >&2 \ + "$(_getopt_quote "$q")" + return 2 ;; + (1) + printf '%s' "${matches[0]}"; return 0 ;; + (*) + [[ $flags == *q* ]] || \ + printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \ + "$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" + return 1 ;; + esac + } + + _getopt_split() { + # Splits $2 at commas to build array specified by $1 + declare IFS=, + eval "$1=( \$2 )" + } + + _getopt_quote() { + # Quotes arguments with single quotes, escaping inner single quotes + declare s space q=\' + for s; do + printf "$space'%s'" "${s//$q/$q\\$q$q}" + space=' ' + done + } + + _getopt_quote_csh() { + # Quotes arguments with single quotes, escaping inner single quotes, + # bangs, backslashes and newlines + declare s i c space + for s; do + echo -n "$space'" + for ((i=0; i<${#s}; i++)); do + c=${s:i:1} + case $c in + (\\|\'|!) + echo -n "'\\$c'" ;; + ($'\n') + echo -n "\\$c" ;; + (*) + echo -n "$c" ;; + esac + done + echo -n \' + space=' ' + done + } + + _getopt_help() { + cat <<-EOT >&2 + + Usage: + getopt + getopt [options] [--] + getopt [options] -o|--options [options] [--] + + Parse command options. + + Options: + -a, --alternative allow long options starting with single - + -l, --longoptions the long options to be recognized + -n, --name the name under which errors are reported + -o, --options the short options to be recognized + -q, --quiet disable error reporting by getopt(3) + -Q, --quiet-output no normal output + -s, --shell set quoting conventions to those of + -T, --test test for getopt(1) version + -u, --unquoted do not quote the output + + -h, --help display this help and exit + -V, --version output version information and exit + + For more details see getopt(1). + EOT + } + + _getopt_version_check() { + if [[ -z $BASH_VERSION ]]; then + echo "getopt: unknown version of bash might not be compatible" >&2 + return 1 + fi + + # This is a lexical comparison that should be sufficient forever. + if [[ $BASH_VERSION < 2.05b ]]; then + echo "getopt: bash $BASH_VERSION might not be compatible" >&2 + return 1 + fi + + return 0 + } + + _getopt_version_check + _getopt_main "$@" + declare status=$? + unset -f _getopt_main _getopt_err _getopt_parse _getopt_quote \ + _getopt_quote_csh _getopt_resolve_abbrev _getopt_split _getopt_help \ + _getopt_version_check + return $status +} + +[[ $BASH_SOURCE != "$0" ]] || main "$@" diff --git a/helpers/terraform_validate b/helpers/terraform_validate new file mode 100755 index 0000000..7f60982 --- /dev/null +++ b/helpers/terraform_validate @@ -0,0 +1,23 @@ +#! /bin/bash +# +# Copyright 2019 Google LLC. This software is provided as-is, without warranty +# or representation for any use or purpose. Your use of it is subject to your +# agreement with Google. +# +# This script initializes modules so that terraform validate as of 0.12 behaves +# as expected and does not issue errors such as: +# +# Error: Module not installed +# +# on test/fixtures/shared_vpc_no_subnets/main.tf line 37: +# 37: module "project-factory" { +# +# This module is not yet installed. Run "terraform init" to install all modules +# required by this configuration. + +# The first and only argument to this script is the directory containing *.tf +# files to validate. This directory is assumed to be a root module. + +cd "$1" +terraform init -backend=false +terraform validate diff --git a/main.tf b/main.tf index 7b2f878..5eb82e7 100644 --- a/main.tf +++ b/main.tf @@ -19,36 +19,45 @@ locals { } resource "google_pubsub_topic" "topic" { - name = "${var.topic}" - project = "${var.project_id}" + name = var.topic + project = var.project_id } resource "google_pubsub_subscription" "push_subscriptions" { - count = "${length(var.push_subscriptions)}" - name = "${lookup(var.push_subscriptions[count.index], "name")}" - topic = "${google_pubsub_topic.topic.name}" - project = "${var.project_id}" - ack_deadline_seconds = "${lookup(var.push_subscriptions[count.index], "ack_deadline_seconds", local.default_ack_deadline_seconds)}" + count = length(var.push_subscriptions) + name = var.push_subscriptions[count.index].name + topic = google_pubsub_topic.topic.name + project = var.project_id + ack_deadline_seconds = lookup( + var.push_subscriptions[count.index], + "ack_deadline_seconds", + local.default_ack_deadline_seconds, + ) push_config { - push_endpoint = "${lookup(var.push_subscriptions[count.index], "push_endpoint")}" + push_endpoint = var.push_subscriptions[count.index]["push_endpoint"] // FIXME: This should be programmable, but nested map isn't supported at this time. // https://github.com/hashicorp/terraform/issues/2114 attributes = { - x-goog-version = "${lookup(var.push_subscriptions[count.index], "x-goog-version", "v1")}" + x-goog-version = lookup(var.push_subscriptions[count.index], "x-goog-version", "v1") } } - depends_on = ["google_pubsub_topic.topic"] + depends_on = [google_pubsub_topic.topic] } resource "google_pubsub_subscription" "pull_subscriptions" { - count = "${length(var.pull_subscriptions)}" - name = "${lookup(var.pull_subscriptions[count.index], "name")}" - topic = "${google_pubsub_topic.topic.name}" - project = "${var.project_id}" - ack_deadline_seconds = "${lookup(var.pull_subscriptions[count.index], "ack_deadline_seconds", local.default_ack_deadline_seconds)}" - - depends_on = ["google_pubsub_topic.topic"] + count = length(var.pull_subscriptions) + name = var.pull_subscriptions[count.index].name + topic = google_pubsub_topic.topic.name + project = var.project_id + ack_deadline_seconds = lookup( + var.pull_subscriptions[count.index], + "ack_deadline_seconds", + local.default_ack_deadline_seconds, + ) + + depends_on = [google_pubsub_topic.topic] } + diff --git a/outputs.tf b/outputs.tf index 649aea4..95da795 100644 --- a/outputs.tf +++ b/outputs.tf @@ -15,12 +15,12 @@ */ output "topic" { - value = "${google_pubsub_topic.topic.name}" + value = google_pubsub_topic.topic.name description = "The name of the Pub/Sub topic" } output "id" { - value = "${google_pubsub_topic.topic.id}" + value = google_pubsub_topic.topic.id description = "The ID of the Pub/Sub topic" } @@ -30,19 +30,20 @@ output "uri" { } output "subscription_names" { - value = "${concat( + value = concat( google_pubsub_subscription.push_subscriptions.*.name, - google_pubsub_subscription.pull_subscriptions.*.name - )}" + google_pubsub_subscription.pull_subscriptions.*.name, + ) description = "The name list of Pub/Sub subscriptions" } output "subscription_paths" { - value = "${concat( + value = concat( google_pubsub_subscription.push_subscriptions.*.path, - google_pubsub_subscription.pull_subscriptions.*.path - )}" + google_pubsub_subscription.pull_subscriptions.*.path, + ) description = "The path list of Pub/Sub subscriptions" } + diff --git a/test/fixtures/main.tf b/test/fixtures/main.tf index 1b3b3ca..ffa09b4 100644 --- a/test/fixtures/main.tf +++ b/test/fixtures/main.tf @@ -14,19 +14,15 @@ * limitations under the License. */ -provider "google" { - credentials = "${file(var.credentials_file_path)}" -} - module "pubsub" { source = "../../" - project_id = "${var.project}" - topic = "${var.topic_name}" + project_id = var.project_id + topic = var.topic_name push_subscriptions = [ { name = "push" - push_endpoint = "https://${var.project}.appspot.com/" + push_endpoint = "https://${var.project_id}.appspot.com/" x-goog-version = "v1beta1" ack_deadline_seconds = 20 }, diff --git a/test/fixtures/outputs.tf b/test/fixtures/outputs.tf index b11382b..a7d8c43 100644 --- a/test/fixtures/outputs.tf +++ b/test/fixtures/outputs.tf @@ -15,9 +15,9 @@ */ output "project_id" { - value = "${var.project}" + value = var.project_id } output "topic_name" { - value = "${var.topic_name}" + value = var.topic_name } diff --git a/test/fixtures/terraform.tfvars.sample b/test/fixtures/terraform.tfvars.sample index 540c233..75801bb 100644 --- a/test/fixtures/terraform.tfvars.sample +++ b/test/fixtures/terraform.tfvars.sample @@ -18,6 +18,5 @@ ################################################################# ## These values you *MUST* modify to match your environment -credentials_file_path="../integration/default/sa-key.json" project="tf-proj" topic_name="tf-proj-topic" diff --git a/test/fixtures/variables.tf b/test/fixtures/variables.tf index 52172b0..7e9b646 100644 --- a/test/fixtures/variables.tf +++ b/test/fixtures/variables.tf @@ -14,11 +14,7 @@ * limitations under the License. */ -variable "credentials_file_path" { - description = "Service account json auth path" -} - -variable "project" { +variable "project_id" { description = "The project to run tests against" } diff --git a/test/integration/default/inspec.yml b/test/integration/default/inspec.yml index 2df7ed1..6e861c7 100644 --- a/test/integration/default/inspec.yml +++ b/test/integration/default/inspec.yml @@ -14,8 +14,6 @@ name: cloud-pubsub title: Google Cloud Pub/Sub -version: 0.1.0 -inspec_version: '~> 2.3.24' attributes: - name: project_id type: string @@ -23,6 +21,3 @@ attributes: - name: topic_name type: string required: true - - name: credentials_file_path - type: string - required: true diff --git a/test/make.sh b/test/make.sh index 8bdbfe4..833905d 100755 --- a/test/make.sh +++ b/test/make.sh @@ -14,9 +14,54 @@ # See the License for the specific language governing permissions and # limitations under the License. +# Create a temporary directory that's auto-cleaned, even if the process aborts. +DELETE_AT_EXIT="$(mktemp -d)" +finish() { + [[ -d "${DELETE_AT_EXIT}" ]] && rm -rf "${DELETE_AT_EXIT}" +} +trap finish EXIT +# Create a temporary file in the auto-cleaned up directory while avoiding +# overwriting TMPDIR for other processes. +# shellcheck disable=SC2120 # (Arguments may be passed, e.g. maketemp -d) +maketemp() { + TMPDIR="${DELETE_AT_EXIT}" mktemp "$@" +} + +# find_files is a helper to exclude .git directories and match only regular +# files to avoid double-processing symlinks. +find_files() { + local pth="$1" + shift + find "${pth}" '(' \ + -path '*/.git' \ + -o -path '*/.terraform' \ + -o -path '*/.kitchen' \ + ')' \ + -prune -o -type f "$@" +} + +# Compatibility with both GNU and BSD style xargs. +compat_xargs() { + local compat=() rval + # Test if xargs is GNU or BSD style. GNU xargs will succeed with status 0 + # when given --no-run-if-empty and no input on STDIN. BSD xargs will fail and + # exit status non-zero If xargs fails, assume it is BSD style and proceed. + # stderr is silently redirected to avoid console log spam. + if xargs --no-run-if-empty /dev/null; then + compat=("--no-run-if-empty") + fi + xargs "${compat[@]}" "$@" + rval="$?" + if [[ -z "${NOWARN:-}" ]] && [[ "${rval}" -gt 0 ]]; then + echo "Warning: compat_xargs $* failed with exit code ${rval}" >&2 + fi + return "${rval}" +} + # This function checks to make sure that every # shebang has a '- e' flag, which causes it # to exit on error + function check_bash() { find . -name "*.sh" | while IFS= read -d '' -r file; do @@ -43,14 +88,28 @@ function docker() { find . -name "Dockerfile" -exec hadolint {} \; } -# This function runs 'terraform validate' against all -# files ending in '.tf' +# This function runs 'terraform validate' and 'terraform fmt' +# against all directory paths which contain *.tf files. function check_terraform() { - echo "Running terraform validate" - #shellcheck disable=SC2156 - find . -name "*.tf" -exec bash -c 'terraform validate --check-variables=false $(dirname "{}")' \; + local rval=125 + # fmt is before validate for faster feedback, validate requires terraform + # init which takes time. echo "Running terraform fmt" - terraform fmt -check=true -write=false + find_files . -name "*.tf" -print0 \ + | compat_xargs -0 -n1 dirname \ + | sort -u \ + | compat_xargs -t -n1 terraform fmt -diff -check=true -write=false + rval="$?" + if [[ "${rval}" -gt 0 ]]; then + echo "Error: terraform fmt failed with exit code ${rval}" >&2 + echo "Check the output for diffs and correct using terraform fmt " >&2 + return "${rval}" + fi + echo "Running terraform validate" + find_files . -not -path "./test/fixtures/shared/*" -name "*.tf" -print0 \ + | compat_xargs -0 -n1 dirname \ + | sort -u \ + | compat_xargs -t -n1 helpers/terraform_validate } # This function runs 'go fmt' and 'go vet' on every file @@ -89,10 +148,20 @@ function check_trailing_whitespace() { function generate_docs() { echo "Generating markdown docs with terraform-docs" - TMPFILE=$(mktemp) - for j in $(for i in $(find . -type f | grep \.tf$ | grep -v "^./\(test/fixtures\|examples\)") ; do dirname "$i" ; done | sort -u) ; do - terraform-docs markdown "$j" > "$TMPFILE" - python helpers/combine_docfiles.py "$j"/README.md "$TMPFILE" - done - rm -f "$TMPFILE" + local pth helper_dir rval + helper_dir="$(pwd)/helpers" + while read -r pth; do + if [[ -e "${pth}/README.md" ]]; then + (cd "${pth}" || return 3; "${helper_dir}"/terraform_docs .;) + rval="$?" + if [[ "${rval}" -gt 0 ]]; then + echo "Error: terraform_docs in ${pth} exit code: ${rval}" >&2 + return "${rval}" + fi + else + echo "Skipping ${pth} because README.md does not exist." + fi + done < <(find_files . -name '*.tf' -print0 \ + | compat_xargs -0 -n1 dirname \ + | sort -u) } diff --git a/variables.tf b/variables.tf index 86caf7c..304f866 100644 --- a/variables.tf +++ b/variables.tf @@ -15,21 +15,24 @@ */ variable "project_id" { + type = string description = "The project ID to manage the Pub/Sub resources" } variable "topic" { + type = string description = "The Pub/Sub topic name" } variable "push_subscriptions" { - type = "list" + type = list(map(string)) description = "The list of the push subscriptions" default = [] } variable "pull_subscriptions" { - type = "list" + type = list(map(string)) description = "The list of the pull subscriptions" default = [] } + diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..832ec1d --- /dev/null +++ b/versions.tf @@ -0,0 +1,19 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +terraform { + required_version = ">= 0.12" +}