From f8ddd017608e0ad20f1ea34eee75d0cd790cb3e1 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Thu, 31 Oct 2019 13:37:31 -0600 Subject: [PATCH 01/19] initial module addition --- .dockerignore | 10 +++ .gitignore | 45 +++++++++++ CHANGELOG.md | 19 +++++ CONTRIBUTING.md | 78 ++++++++++++++++++ Makefile | 75 +++++++++++++++++ README.md | 81 ++++++++++++++++++- build/int.cloudbuild.yaml | 41 ++++++++++ build/lint.cloudbuild.yaml | 24 ++++++ examples/simple_example/README.md | 25 ++++++ examples/simple_example/main.tf | 25 ++++++ examples/simple_example/outputs.tf | 25 ++++++ examples/simple_example/variables.tf | 27 +++++++ examples/simple_example/versions.tf | 19 +++++ kitchen.yml | 35 ++++++++ main.tf | 53 ++++++++++++ outputs.tf | 25 ++++++ test/.gitignore | 1 + test/fixtures/simple_example/main.tf | 22 +++++ test/fixtures/simple_example/outputs.tf | 25 ++++++ test/fixtures/simple_example/variables.tf | 27 +++++++ test/fixtures/simple_example/versions.tf | 19 +++++ .../simple_example/controls/gcloud.rb | 23 ++++++ test/integration/simple_example/inspec.yml | 14 ++++ test/setup/make_source.sh | 20 +++++ variables.tf | 27 +++++++ versions.tf | 19 +++++ 26 files changed, 803 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 Makefile create mode 100644 build/int.cloudbuild.yaml create mode 100644 build/lint.cloudbuild.yaml create mode 100644 examples/simple_example/README.md create mode 100644 examples/simple_example/main.tf create mode 100644 examples/simple_example/outputs.tf create mode 100644 examples/simple_example/variables.tf create mode 100644 examples/simple_example/versions.tf create mode 100644 kitchen.yml create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 test/.gitignore create mode 100644 test/fixtures/simple_example/main.tf create mode 100644 test/fixtures/simple_example/outputs.tf create mode 100644 test/fixtures/simple_example/variables.tf create mode 100644 test/fixtures/simple_example/versions.tf create mode 100644 test/integration/simple_example/controls/gcloud.rb create mode 100644 test/integration/simple_example/inspec.yml create mode 100755 test/setup/make_source.sh create mode 100644 variables.tf create mode 100644 versions.tf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..20265c2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.git +.terraform +.terraform.d +.kitchen +terraform.tfstate.d +test/fixtures/*/.terraform +test/fixtures/*/terraform.tfstate.d +examples/.kitchen +examples/*/.terraform +examples/*/terraform.tfstate.d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ce300b --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +# OSX leaves these everywhere on SMB shares +._* + +# OSX trash +.DS_Store + +# Python +*.pyc + +# Emacs save files +*~ +\#*\# +.\#* + +# Vim-related files +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +*.un~ +Session.vim +.netrwhist + +### https://raw.github.com/github/gitignore/90f149de451a5433aebd94d02d11b0e28843a1af/Terraform.gitignore + +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log + +# Kitchen files +**/inspec.lock +**/.kitchen +**/kitchen.local.yml +**/Gemfile.lock + +# Ignore any .tfvars files that are generated automatically for each Terraform run. Most +# .tfvars files are managed as part of configuration and so should be included in +# version control. +**/*.tfvars + +credentials.json diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..cbb125f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# 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] + +## [0.1.0] - 20XX-YY-ZZ + +### Added + +- Initial release + +[Unreleased]: https://github.com/terraform-google-modules/terraform-google-utils/compare/v0.1.0...HEAD +[0.1.0]: https://github.com/terraform-google-modules/terraform-google-utils/releases/tag/v0.1.0 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..2c16310 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,78 @@ +# Contributing + +This document provides guidelines for contributing to the module. + +## Dependencies + +The following dependencies must be installed on the development system: + +- [Docker Engine][docker-engine] +- [Google Cloud SDK][google-cloud-sdk] +- [make] + +## Generating Documentation for Inputs and Outputs + +The Inputs and Outputs tables in the READMEs of the root module, +submodules, and example modules are automatically generated based on +the `variables` and `outputs` of the respective modules. These tables +must be refreshed if the module interfaces are changed. + +### Execution + +Run `make generate_docs` to generate new Inputs and Outputs tables. + +## Integration Testing + +Integration tests are used to verify the behaviour of the root module, +submodules, and example modules. Additions, changes, and fixes should +be accompanied with tests. + +The integration tests are run using [Kitchen][kitchen], +[Kitchen-Terraform][kitchen-terraform], and [InSpec][inspec]. These +tools are packaged within a Docker image for convenience. + +The general strategy for these tests is to verify the behaviour of the +[example modules](./examples/), thus ensuring that the root module, +submodules, and example modules are all functionally correct. + +### Noninteractive Execution + +Run `make docker_test_integration` to test all of the example modules +noninteractively, using the prepared test project. + +### Interactive Execution + +1. Run `make docker_run` to start the testing Docker container in + interactive mode. + +1. Run `kitchen_do create ` to initialize the working + directory for an example module. + +1. Run `kitchen_do converge ` to apply the example module. + +1. Run `kitchen_do verify ` to test the example module. + +1. Run `kitchen_do destroy ` to destroy the example module + state. + +## Linting and Formatting + +Many of the files in the repository can be linted or formatted to +maintain a standard of quality. + +### Execution + +Run `make docker_test_lint`. + +[docker-engine]: https://www.docker.com/products/docker-engine +[flake8]: http://flake8.pycqa.org/en/latest/ +[gofmt]: https://golang.org/cmd/gofmt/ +[google-cloud-sdk]: https://cloud.google.com/sdk/install +[hadolint]: https://github.com/hadolint/hadolint +[inspec]: https://inspec.io/ +[kitchen-terraform]: https://github.com/newcontext-oss/kitchen-terraform +[kitchen]: https://kitchen.ci/ +[make]: https://en.wikipedia.org/wiki/Make_(software) +[shellcheck]: https://www.shellcheck.net/ +[terraform-docs]: https://github.com/segmentio/terraform-docs +[terraform]: https://terraform.io/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..15472b6 --- /dev/null +++ b/Makefile @@ -0,0 +1,75 @@ +# Copyright 2019 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. + +# Please note that this file was generated from [terraform-google-module-template](https://github.com/terraform-google-modules/terraform-google-module-template). +# Please make sure to contribute relevant changes upstream! + +# Make will use bash instead of sh +SHELL := /usr/bin/env bash + +DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.1.0 +DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools +REGISTRY_URL := gcr.io/cloud-foundation-cicd + +# Enter docker container for local development +.PHONY: docker_run +docker_run: + docker run --rm -it \ + -v "$(CURDIR)":/workspace \ + $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ + /bin/bash + +# Execute prepare tests within the docker container +.PHONY: docker_test_prepare +docker_test_prepare: + docker run --rm -it \ + -v "$(CURDIR)":/workspace \ + $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ + /usr/local/bin/execute_with_credentials.sh prepare_environment + +# Clean up test environment within the docker container +.PHONY: docker_test_cleanup +docker_test_cleanup: + docker run --rm -it \ + -v "$(CURDIR)":/workspace \ + $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ + /usr/local/bin/execute_with_credentials.sh cleanup_environment + +# Execute integration tests within the docker container +.PHONY: docker_test_integration +docker_test_integration: + docker run --rm -it \ + -v "$(CURDIR)":/workspace \ + $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ + /usr/local/bin/test_integration.sh + +# Execute lint tests within the docker container +.PHONY: docker_test_lint +docker_test_lint: + docker run --rm -it \ + -v "$(CURDIR)":/workspace \ + $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ + /usr/local/bin/test_lint.sh + +# Generate documentation +.PHONY: docker_generate_docs +docker_generate_docs: + docker run --rm -it \ + -v "$(CURDIR)":/workspace \ + $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ + /bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate_docs' + +# Alias for backwards compatibility +.PHONY: generate_docs +generate_docs: docker_generate_docs diff --git a/README.md b/README.md index be49bb6..a8aa81a 100644 --- a/README.md +++ b/README.md @@ -1 +1,80 @@ -# terraform-google-utils \ No newline at end of file +# terraform-google-utils + +This module provides a way to get the shortnames for given GCP region(s). For example, "us-central1" becomes "usc1". + +This module does not communicate with GCP in any way. + +## Usage + +Basic usage of this module is as follows: + +```hcl +module "utils" { + source = "terraform-google-modules/utils/google" + version = "~> 0.1" + regions = ["us-central1", "us-west1"] +} + +output "region_shortname_map" { + description = "Map of full to short name of all given regions" + value = "${module.utils.region_short_name_map}" +} + +module "utils" { + source = "terraform-google-modules/utils/google" + version = "~> 0.1" + regions = "us-central1" +} + +output "region_shortname" { + description = "Short name of the given region" + value = "${module.utils.region_short_name}" +} +``` + +Functional examples are included in the +[examples](./examples/) directory. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| bucket\_name | The name of the bucket to create | string | n/a | yes | +| project\_id | The project ID to deploy to | string | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| bucket\_name | | + + + +## Requirements + +These sections describe requirements for using this module. + +### Software + +The following dependencies must be available: + +- [Terraform][terraform] v0.12 + +### Service Account + +A service account is not needed for to use this module. + +### APIs + +Projects/APIs are not required to use this module. + +## Contributing + +Refer to the [contribution guidelines](./CONTRIBUTING.md) for +information on contributing to this module. + +[iam-module]: https://registry.terraform.io/modules/terraform-google-modules/iam/google +[project-factory-module]: https://registry.terraform.io/modules/terraform-google-modules/project-factory/google +[terraform-provider-gcp]: https://www.terraform.io/docs/providers/google/index.html +[terraform]: https://www.terraform.io/downloads.html diff --git a/build/int.cloudbuild.yaml b/build/int.cloudbuild.yaml new file mode 100644 index 0000000..acdecf8 --- /dev/null +++ b/build/int.cloudbuild.yaml @@ -0,0 +1,41 @@ +# Copyright 2019 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. + +timeout: 3600s +steps: +- id: prepare + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && prepare_environment'] + env: + - 'TF_VAR_org_id=$_ORG_ID' + - 'TF_VAR_folder_id=$_FOLDER_ID' + - 'TF_VAR_billing_account=$_BILLING_ACCOUNT' +- id: create + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create'] +- id: converge + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do converge'] +- id: verify + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do verify'] +- id: destroy + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy'] +tags: +- 'ci' +- 'integration' +substitutions: + _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' + _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0' diff --git a/build/lint.cloudbuild.yaml b/build/lint.cloudbuild.yaml new file mode 100644 index 0000000..693e917 --- /dev/null +++ b/build/lint.cloudbuild.yaml @@ -0,0 +1,24 @@ +# Copyright 2019 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. + +steps: +- name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + id: 'lint' + args: ['/usr/local/bin/test_lint.sh'] +tags: +- 'ci' +- 'lint' +substitutions: + _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' + _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.4.5' diff --git a/examples/simple_example/README.md b/examples/simple_example/README.md new file mode 100644 index 0000000..af2d37e --- /dev/null +++ b/examples/simple_example/README.md @@ -0,0 +1,25 @@ +# Simple Example + +This example illustrates how to use the `utils` module. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| bucket\_name | The name of the bucket to create. | string | n/a | yes | +| project\_id | The ID of the project in which to provision resources. | string | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| bucket\_name | The name of the bucket. | + + + +To provision this example, run the following from 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/main.tf b/examples/simple_example/main.tf new file mode 100644 index 0000000..ca0cbcc --- /dev/null +++ b/examples/simple_example/main.tf @@ -0,0 +1,25 @@ +/** + * 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. + */ + +provider "google" { + version = "~> 2.0" +} + +module "utils" { + source = "../.." + region = "${var.region}" + regions = "${var.regions}" +} diff --git a/examples/simple_example/outputs.tf b/examples/simple_example/outputs.tf new file mode 100644 index 0000000..88a0d2d --- /dev/null +++ b/examples/simple_example/outputs.tf @@ -0,0 +1,25 @@ +/** + * 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 "region_short_name_map" { + description = "The 4 or 5 character shortname of any given region." + value = "${module.utils.region_short_name_map}" +} + +output "region_short_name" { + description = "The 4 or 5 character shortname of a given region." + value = "${module.utils.region_short_name}" +} diff --git a/examples/simple_example/variables.tf b/examples/simple_example/variables.tf new file mode 100644 index 0000000..43b5eec --- /dev/null +++ b/examples/simple_example/variables.tf @@ -0,0 +1,27 @@ +/** + * 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. + */ + +variable "regions" { + description = "A list of GCP region codes for each of the given regions. i.e. [\"us-central1\"] => {\"us-central1\"=\"usc1\"}" + type = list + default = [] +} + +variable "region" { + description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" + type = string + default = null +} diff --git a/examples/simple_example/versions.tf b/examples/simple_example/versions.tf new file mode 100644 index 0000000..832ec1d --- /dev/null +++ b/examples/simple_example/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" +} diff --git a/kitchen.yml b/kitchen.yml new file mode 100644 index 0000000..3e648b4 --- /dev/null +++ b/kitchen.yml @@ -0,0 +1,35 @@ +# 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. + +--- +driver: + name: terraform + +provisioner: + name: terraform + +verifier: + name: terraform + systems: + - name: system + backend: gcloud + +platforms: + - name: default + +suites: + - name: simple_example + driver: + name: terraform + root_module_directory: test/fixtures/simple_example diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..1c4cd9e --- /dev/null +++ b/main.tf @@ -0,0 +1,53 @@ +/** + * 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.0" +} + +locals { + # Compute the regional shortname from component parts + continent_short_name = { + asia = "az" + australia = "au" + europe = "eu" + northamerica = "na" + southamerica = "sa" + us = "us" + } + parts = var.region == null ? [] : split("-", var.region) + region_short_name = var.region == null ? "" : join("", [ + local.continent_short_name[local.parts[0]], + replace(local.parts[1], "/(n)orth|(s)outh|(e)ast|(w)est|(c)entral/", "$1$2$3$4$5") + ]) + # Same computation but kick back a map + region_short_name_map = var.regions == [] ? {} : { for full_region in var.regions : full_region => + join( + "", [ + local.continent_short_name[split( + "-", + full_region + )[0]], + replace( + split( + "-", full_region)[1], + "/(n)orth|(s)outh|(e)ast|(w)est|(c)entral/", + "$1$2$3$4$5" + ) + ] + ) + } +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..6e2b31a --- /dev/null +++ b/outputs.tf @@ -0,0 +1,25 @@ +/** + * 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 "region_short_name_map" { + description = "The 4 or 5 character shortname of any given region." + value = "${local.region_short_name_map}" +} + +output "region_short_name" { + description = "The 4 or 5 character shortname of a given region." + value = "${local.region_short_name}" +} diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..d69ba0d --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +source.sh diff --git a/test/fixtures/simple_example/main.tf b/test/fixtures/simple_example/main.tf new file mode 100644 index 0000000..0a31c44 --- /dev/null +++ b/test/fixtures/simple_example/main.tf @@ -0,0 +1,22 @@ +/** + * 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. + */ + +module "example" { + source = "../../../examples/simple_example" + + region = "us-west1" + regions = ["us-west1", "us-central1"] +} diff --git a/test/fixtures/simple_example/outputs.tf b/test/fixtures/simple_example/outputs.tf new file mode 100644 index 0000000..e723c24 --- /dev/null +++ b/test/fixtures/simple_example/outputs.tf @@ -0,0 +1,25 @@ +/** + * 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 "region_short_name_map" { + description = "The 4 or 5 character shortname of any given region." + value = "${module.example.region_short_name_map}" +} + +output "region_short_name" { + description = "The 4 or 5 character shortname of a given region." + value = "${module.example.region_short_name}" +} diff --git a/test/fixtures/simple_example/variables.tf b/test/fixtures/simple_example/variables.tf new file mode 100644 index 0000000..43b5eec --- /dev/null +++ b/test/fixtures/simple_example/variables.tf @@ -0,0 +1,27 @@ +/** + * 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. + */ + +variable "regions" { + description = "A list of GCP region codes for each of the given regions. i.e. [\"us-central1\"] => {\"us-central1\"=\"usc1\"}" + type = list + default = [] +} + +variable "region" { + description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" + type = string + default = null +} diff --git a/test/fixtures/simple_example/versions.tf b/test/fixtures/simple_example/versions.tf new file mode 100644 index 0000000..832ec1d --- /dev/null +++ b/test/fixtures/simple_example/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" +} diff --git a/test/integration/simple_example/controls/gcloud.rb b/test/integration/simple_example/controls/gcloud.rb new file mode 100644 index 0000000..4a22257 --- /dev/null +++ b/test/integration/simple_example/controls/gcloud.rb @@ -0,0 +1,23 @@ +# 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. + +control "gcloud" do + title "gcloud" + + describe command("gcloud compute regions list") do + its(:exit_status) { should eq 0 } + its(:stderr) { should eq "" } + #its(:stdout) { should match "storage-api.googleapis.com" } + end +end diff --git a/test/integration/simple_example/inspec.yml b/test/integration/simple_example/inspec.yml new file mode 100644 index 0000000..4dc9296 --- /dev/null +++ b/test/integration/simple_example/inspec.yml @@ -0,0 +1,14 @@ +name: simple_example +depends: + - name: inspec-gcp + git: https://github.com/inspec/inspec-gcp.git + tag: v0.10.0 +supports: + - platform: gcp +attributes: + - name: region + required: false + type: string + - name: regions + required: false + type: array diff --git a/test/setup/make_source.sh b/test/setup/make_source.sh new file mode 100755 index 0000000..3c970cc --- /dev/null +++ b/test/setup/make_source.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +# 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. + +echo "#!/usr/bin/env bash" > ../source.sh + +echo "export TF_VAR_region='us-east1'" >> ../source.sh +echo "export TF_VAR_regions='[us-east1,us-west1]'" >> ../source.sh diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..43b5eec --- /dev/null +++ b/variables.tf @@ -0,0 +1,27 @@ +/** + * 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. + */ + +variable "regions" { + description = "A list of GCP region codes for each of the given regions. i.e. [\"us-central1\"] => {\"us-central1\"=\"usc1\"}" + type = list + default = [] +} + +variable "region" { + description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" + type = string + default = null +} 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" +} From 1af74e74c67ef166f51eaaae3ce1a9bb3951ed42 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Thu, 31 Oct 2019 13:52:13 -0600 Subject: [PATCH 02/19] bump developer tools image tag --- Makefile | 2 +- build/int.cloudbuild.yaml | 2 +- build/lint.cloudbuild.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 15472b6..ddb1eea 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ # Make will use bash instead of sh SHELL := /usr/bin/env bash -DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.1.0 +DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.4.6 DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools REGISTRY_URL := gcr.io/cloud-foundation-cicd diff --git a/build/int.cloudbuild.yaml b/build/int.cloudbuild.yaml index acdecf8..ff44db3 100644 --- a/build/int.cloudbuild.yaml +++ b/build/int.cloudbuild.yaml @@ -38,4 +38,4 @@ tags: - 'integration' substitutions: _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' - _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0' + _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.4.6' diff --git a/build/lint.cloudbuild.yaml b/build/lint.cloudbuild.yaml index 693e917..4e27761 100644 --- a/build/lint.cloudbuild.yaml +++ b/build/lint.cloudbuild.yaml @@ -21,4 +21,4 @@ tags: - 'lint' substitutions: _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' - _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.4.5' + _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.4.6' From 96de4f2471af75a0f68a1e0d6ed223d2808c6c5d Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Thu, 31 Oct 2019 14:16:21 -0600 Subject: [PATCH 03/19] update makefile --- Makefile | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index ddb1eea..39551f7 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,8 @@ REGISTRY_URL := gcr.io/cloud-foundation-cicd .PHONY: docker_run docker_run: docker run --rm -it \ - -v "$(CURDIR)":/workspace \ + -e SERVICE_ACCOUNT_JSON \ + -v $(CURDIR):/workspace \ $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ /bin/bash @@ -34,7 +35,11 @@ docker_run: .PHONY: docker_test_prepare docker_test_prepare: docker run --rm -it \ - -v "$(CURDIR)":/workspace \ + -e SERVICE_ACCOUNT_JSON \ + -e TF_VAR_org_id \ + -e TF_VAR_folder_id \ + -e TF_VAR_billing_account \ + -v $(CURDIR):/workspace \ $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ /usr/local/bin/execute_with_credentials.sh prepare_environment @@ -42,7 +47,11 @@ docker_test_prepare: .PHONY: docker_test_cleanup docker_test_cleanup: docker run --rm -it \ - -v "$(CURDIR)":/workspace \ + -e SERVICE_ACCOUNT_JSON \ + -e TF_VAR_org_id \ + -e TF_VAR_folder_id \ + -e TF_VAR_billing_account \ + -v $(CURDIR):/workspace \ $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ /usr/local/bin/execute_with_credentials.sh cleanup_environment @@ -50,7 +59,8 @@ docker_test_cleanup: .PHONY: docker_test_integration docker_test_integration: docker run --rm -it \ - -v "$(CURDIR)":/workspace \ + -e SERVICE_ACCOUNT_JSON \ + -v $(CURDIR):/workspace \ $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ /usr/local/bin/test_integration.sh @@ -58,7 +68,7 @@ docker_test_integration: .PHONY: docker_test_lint docker_test_lint: docker run --rm -it \ - -v "$(CURDIR)":/workspace \ + -v $(CURDIR):/workspace \ $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ /usr/local/bin/test_lint.sh @@ -66,7 +76,7 @@ docker_test_lint: .PHONY: docker_generate_docs docker_generate_docs: docker run --rm -it \ - -v "$(CURDIR)":/workspace \ + -v $(CURDIR):/workspace \ $(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \ /bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate_docs' From 7ee51a369b1b234f3894dd32d19a33354b455597 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Tue, 5 Nov 2019 10:44:15 -0700 Subject: [PATCH 04/19] make generate docs --- README.md | 7 ++++--- examples/simple_example/README.md | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a8aa81a..182009c 100644 --- a/README.md +++ b/README.md @@ -40,14 +40,15 @@ Functional examples are included in the | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| -| bucket\_name | The name of the bucket to create | string | n/a | yes | -| project\_id | The project ID to deploy to | string | n/a | yes | +| region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | +| regions | A list of GCP region codes for each of the given regions. i.e. ["us-central1"] => {"us-central1"="usc1"} | list | `` | no | ## Outputs | Name | Description | |------|-------------| -| bucket\_name | | +| region\_short\_name | The 4 or 5 character shortname of a given region. | +| region\_short\_name\_map | The 4 or 5 character shortname of any given region. | diff --git a/examples/simple_example/README.md b/examples/simple_example/README.md index af2d37e..8acc508 100644 --- a/examples/simple_example/README.md +++ b/examples/simple_example/README.md @@ -7,14 +7,15 @@ This example illustrates how to use the `utils` module. | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| -| bucket\_name | The name of the bucket to create. | string | n/a | yes | -| project\_id | The ID of the project in which to provision resources. | string | n/a | yes | +| region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | +| regions | A list of GCP region codes for each of the given regions. i.e. ["us-central1"] => {"us-central1"="usc1"} | list | `` | no | ## Outputs | Name | Description | |------|-------------| -| bucket\_name | The name of the bucket. | +| region\_short\_name | The 4 or 5 character shortname of a given region. | +| region\_short\_name\_map | The 4 or 5 character shortname of any given region. | From 29dc9ec1c433d52920cdd4f906d71c18409dd33f Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 6 Nov 2019 16:13:40 -0700 Subject: [PATCH 05/19] wip on adding separate examples for region & multi-regions --- Makefile | 2 +- README.md | 2 +- examples/multi_region/README.md | 26 +++++++++++++++++++ .../variables.tf => multi_region/main.tf} | 14 +++++----- .../multi_region/outputs.tf | 14 +++++----- examples/multi_region/versions.tf | 19 ++++++++++++++ examples/simple_example/main.tf | 5 ++-- kitchen.yml | 4 +++ test/fixtures/multi_region/main.tf | 19 ++++++++++++++ test/fixtures/multi_region/outputs.tf | 25 ++++++++++++++++++ test/fixtures/multi_region/versions.tf | 19 ++++++++++++++ test/fixtures/simple_example/main.tf | 3 --- .../multi_region/controls/gcloud.rb} | 21 ++++++++++----- test/integration/multi_region/inspec.yml | 14 ++++++++++ .../simple_example/controls/gcloud.rb | 9 +++++-- test/integration/simple_example/inspec.yml | 8 +++--- 16 files changed, 168 insertions(+), 36 deletions(-) create mode 100644 examples/multi_region/README.md rename examples/{simple_example/variables.tf => multi_region/main.tf} (62%) rename test/fixtures/simple_example/variables.tf => examples/multi_region/outputs.tf (62%) create mode 100644 examples/multi_region/versions.tf create mode 100644 test/fixtures/multi_region/main.tf create mode 100644 test/fixtures/multi_region/outputs.tf create mode 100644 test/fixtures/multi_region/versions.tf rename test/{setup/make_source.sh => integration/multi_region/controls/gcloud.rb} (55%) mode change 100755 => 100644 create mode 100644 test/integration/multi_region/inspec.yml diff --git a/Makefile b/Makefile index 39551f7..e01fd64 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ # Make will use bash instead of sh SHELL := /usr/bin/env bash -DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.4.6 +DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.5.3 DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools REGISTRY_URL := gcr.io/cloud-foundation-cicd diff --git a/README.md b/README.md index 182009c..0ed6a25 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ output "region_shortname_map" { module "utils" { source = "terraform-google-modules/utils/google" version = "~> 0.1" - regions = "us-central1" + region = "us-central1" } output "region_shortname" { diff --git a/examples/multi_region/README.md b/examples/multi_region/README.md new file mode 100644 index 0000000..8acc508 --- /dev/null +++ b/examples/multi_region/README.md @@ -0,0 +1,26 @@ +# Simple Example + +This example illustrates how to use the `utils` module. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | +| regions | A list of GCP region codes for each of the given regions. i.e. ["us-central1"] => {"us-central1"="usc1"} | list | `` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| region\_short\_name | The 4 or 5 character shortname of a given region. | +| region\_short\_name\_map | The 4 or 5 character shortname of any given region. | + + + +To provision this example, run the following from 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/variables.tf b/examples/multi_region/main.tf similarity index 62% rename from examples/simple_example/variables.tf rename to examples/multi_region/main.tf index 43b5eec..862ebd0 100644 --- a/examples/simple_example/variables.tf +++ b/examples/multi_region/main.tf @@ -14,14 +14,12 @@ * limitations under the License. */ -variable "regions" { - description = "A list of GCP region codes for each of the given regions. i.e. [\"us-central1\"] => {\"us-central1\"=\"usc1\"}" - type = list - default = [] +provider "google" { + version = "~> 2.0" } -variable "region" { - description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" - type = string - default = null +module "utils" { + source = "../.." + regions = ["us-central1", "us-east1"] } + diff --git a/test/fixtures/simple_example/variables.tf b/examples/multi_region/outputs.tf similarity index 62% rename from test/fixtures/simple_example/variables.tf rename to examples/multi_region/outputs.tf index 43b5eec..88a0d2d 100644 --- a/test/fixtures/simple_example/variables.tf +++ b/examples/multi_region/outputs.tf @@ -14,14 +14,12 @@ * limitations under the License. */ -variable "regions" { - description = "A list of GCP region codes for each of the given regions. i.e. [\"us-central1\"] => {\"us-central1\"=\"usc1\"}" - type = list - default = [] +output "region_short_name_map" { + description = "The 4 or 5 character shortname of any given region." + value = "${module.utils.region_short_name_map}" } -variable "region" { - description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" - type = string - default = null +output "region_short_name" { + description = "The 4 or 5 character shortname of a given region." + value = "${module.utils.region_short_name}" } diff --git a/examples/multi_region/versions.tf b/examples/multi_region/versions.tf new file mode 100644 index 0000000..832ec1d --- /dev/null +++ b/examples/multi_region/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" +} diff --git a/examples/simple_example/main.tf b/examples/simple_example/main.tf index ca0cbcc..5016e3b 100644 --- a/examples/simple_example/main.tf +++ b/examples/simple_example/main.tf @@ -19,7 +19,6 @@ provider "google" { } module "utils" { - source = "../.." - region = "${var.region}" - regions = "${var.regions}" + source = "../.." + region = "us-central1" } diff --git a/kitchen.yml b/kitchen.yml index 3e648b4..78a5ef2 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -33,3 +33,7 @@ suites: driver: name: terraform root_module_directory: test/fixtures/simple_example + - name: multi_region + driver: + name: terraform + root_module_directory: test/fixtures/multi_region diff --git a/test/fixtures/multi_region/main.tf b/test/fixtures/multi_region/main.tf new file mode 100644 index 0000000..cf0e581 --- /dev/null +++ b/test/fixtures/multi_region/main.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. + */ + +module "example" { + source = "../../../examples/multi_region" +} diff --git a/test/fixtures/multi_region/outputs.tf b/test/fixtures/multi_region/outputs.tf new file mode 100644 index 0000000..e723c24 --- /dev/null +++ b/test/fixtures/multi_region/outputs.tf @@ -0,0 +1,25 @@ +/** + * 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 "region_short_name_map" { + description = "The 4 or 5 character shortname of any given region." + value = "${module.example.region_short_name_map}" +} + +output "region_short_name" { + description = "The 4 or 5 character shortname of a given region." + value = "${module.example.region_short_name}" +} diff --git a/test/fixtures/multi_region/versions.tf b/test/fixtures/multi_region/versions.tf new file mode 100644 index 0000000..832ec1d --- /dev/null +++ b/test/fixtures/multi_region/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" +} diff --git a/test/fixtures/simple_example/main.tf b/test/fixtures/simple_example/main.tf index 0a31c44..bb4881b 100644 --- a/test/fixtures/simple_example/main.tf +++ b/test/fixtures/simple_example/main.tf @@ -16,7 +16,4 @@ module "example" { source = "../../../examples/simple_example" - - region = "us-west1" - regions = ["us-west1", "us-central1"] } diff --git a/test/setup/make_source.sh b/test/integration/multi_region/controls/gcloud.rb old mode 100755 new mode 100644 similarity index 55% rename from test/setup/make_source.sh rename to test/integration/multi_region/controls/gcloud.rb index 3c970cc..51c0801 --- a/test/setup/make_source.sh +++ b/test/integration/multi_region/controls/gcloud.rb @@ -1,12 +1,10 @@ -#!/usr/bin/env bash - # 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 +# 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, @@ -14,7 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -echo "#!/usr/bin/env bash" > ../source.sh +output_region_map = attribute("output_region_short_name_map") + +control "gcloud" do + title "gcloud" + + describe "outputs" do + its(:exit_status) { should eq 0 } + its(:stderr) { should eq "" } -echo "export TF_VAR_region='us-east1'" >> ../source.sh -echo "export TF_VAR_regions='[us-east1,us-west1]'" >> ../source.sh + it 'should have a valid region' do + expect(output_region_map["us-central1"]).to eq "usc1" + expect(output_region_map["us-east1"]).to eq "use1" + end + end +end diff --git a/test/integration/multi_region/inspec.yml b/test/integration/multi_region/inspec.yml new file mode 100644 index 0000000..41f2005 --- /dev/null +++ b/test/integration/multi_region/inspec.yml @@ -0,0 +1,14 @@ +name: simple_example +depends: + - name: inspec-gcp + git: https://github.com/inspec/inspec-gcp.git + tag: v0.10.0 +supports: + - platform: gcp +attributes: + - name: regions + required: true + type: array + - name: output_region_short_name_map + required: true + type: array diff --git a/test/integration/simple_example/controls/gcloud.rb b/test/integration/simple_example/controls/gcloud.rb index 4a22257..e915b86 100644 --- a/test/integration/simple_example/controls/gcloud.rb +++ b/test/integration/simple_example/controls/gcloud.rb @@ -12,12 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +output_region_shortname = attribute("output_region_shortname") + control "gcloud" do title "gcloud" - describe command("gcloud compute regions list") do + describe "outputs" do its(:exit_status) { should eq 0 } its(:stderr) { should eq "" } - #its(:stdout) { should match "storage-api.googleapis.com" } + + it 'should have a valid region' do + expect(output_region_shortname).to eq "usc1" + end end end diff --git a/test/integration/simple_example/inspec.yml b/test/integration/simple_example/inspec.yml index 4dc9296..32c654c 100644 --- a/test/integration/simple_example/inspec.yml +++ b/test/integration/simple_example/inspec.yml @@ -7,8 +7,8 @@ supports: - platform: gcp attributes: - name: region - required: false + required: true + type: string + - name: output_region_shortname + required: true type: string - - name: regions - required: false - type: array From 2af5456f7a0d2218b354312be1d98fee3b05b16d Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 6 Nov 2019 17:48:44 -0700 Subject: [PATCH 06/19] passed integration tests --- README.md | 4 ++-- examples/multi_region/README.md | 7 ------- examples/simple_example/README.md | 7 ------- kitchen.yml | 7 ++----- test/integration/multi_region/controls/gcloud.rb | 15 ++++----------- test/integration/multi_region/inspec.yml | 2 +- .../integration/simple_example/controls/gcloud.rb | 15 ++++----------- test/integration/simple_example/inspec.yml | 2 +- 8 files changed, 14 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 0ed6a25..87426ce 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ module "utils" { regions = ["us-central1", "us-west1"] } -output "region_shortname_map" { +output "region_short_name_map" { description = "Map of full to short name of all given regions" value = "${module.utils.region_short_name_map}" } @@ -26,7 +26,7 @@ module "utils" { region = "us-central1" } -output "region_shortname" { +output "region_short_name" { description = "Short name of the given region" value = "${module.utils.region_short_name}" } diff --git a/examples/multi_region/README.md b/examples/multi_region/README.md index 8acc508..0ff83aa 100644 --- a/examples/multi_region/README.md +++ b/examples/multi_region/README.md @@ -3,13 +3,6 @@ This example illustrates how to use the `utils` module. -## Inputs - -| Name | Description | Type | Default | Required | -|------|-------------|:----:|:-----:|:-----:| -| region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | -| regions | A list of GCP region codes for each of the given regions. i.e. ["us-central1"] => {"us-central1"="usc1"} | list | `` | no | - ## Outputs | Name | Description | diff --git a/examples/simple_example/README.md b/examples/simple_example/README.md index 8acc508..0ff83aa 100644 --- a/examples/simple_example/README.md +++ b/examples/simple_example/README.md @@ -3,13 +3,6 @@ This example illustrates how to use the `utils` module. -## Inputs - -| Name | Description | Type | Default | Required | -|------|-------------|:----:|:-----:|:-----:| -| region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | -| regions | A list of GCP region codes for each of the given regions. i.e. ["us-central1"] => {"us-central1"="usc1"} | list | `` | no | - ## Outputs | Name | Description | diff --git a/kitchen.yml b/kitchen.yml index 78a5ef2..0f37216 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -13,9 +13,6 @@ # limitations under the License. --- -driver: - name: terraform - provisioner: name: terraform @@ -23,10 +20,10 @@ verifier: name: terraform systems: - name: system - backend: gcloud + backend: gcp platforms: - - name: default + - name: local suites: - name: simple_example diff --git a/test/integration/multi_region/controls/gcloud.rb b/test/integration/multi_region/controls/gcloud.rb index 51c0801..ab4a446 100644 --- a/test/integration/multi_region/controls/gcloud.rb +++ b/test/integration/multi_region/controls/gcloud.rb @@ -14,16 +14,9 @@ output_region_map = attribute("output_region_short_name_map") -control "gcloud" do - title "gcloud" - - describe "outputs" do - its(:exit_status) { should eq 0 } - its(:stderr) { should eq "" } - - it 'should have a valid region' do - expect(output_region_map["us-central1"]).to eq "usc1" - expect(output_region_map["us-east1"]).to eq "use1" - end +describe "outputs" do + it 'should have a valid region' do + expect(output_region_map["us-central1"]).to eq "usc1" + expect(output_region_map["us-east1"]).to eq "use1" end end diff --git a/test/integration/multi_region/inspec.yml b/test/integration/multi_region/inspec.yml index 41f2005..0b35d3a 100644 --- a/test/integration/multi_region/inspec.yml +++ b/test/integration/multi_region/inspec.yml @@ -11,4 +11,4 @@ attributes: type: array - name: output_region_short_name_map required: true - type: array + type: hash diff --git a/test/integration/simple_example/controls/gcloud.rb b/test/integration/simple_example/controls/gcloud.rb index e915b86..0269871 100644 --- a/test/integration/simple_example/controls/gcloud.rb +++ b/test/integration/simple_example/controls/gcloud.rb @@ -12,17 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -output_region_shortname = attribute("output_region_shortname") +output_region_short_name = attribute("output_region_short_name") -control "gcloud" do - title "gcloud" - - describe "outputs" do - its(:exit_status) { should eq 0 } - its(:stderr) { should eq "" } - - it 'should have a valid region' do - expect(output_region_shortname).to eq "usc1" - end +describe "outputs" do + it 'should have a valid region' do + expect(output_region_short_name).to eq "usc1" end end diff --git a/test/integration/simple_example/inspec.yml b/test/integration/simple_example/inspec.yml index 32c654c..ce1da40 100644 --- a/test/integration/simple_example/inspec.yml +++ b/test/integration/simple_example/inspec.yml @@ -9,6 +9,6 @@ attributes: - name: region required: true type: string - - name: output_region_shortname + - name: output_region_short_name required: true type: string From 38231fd595191390df626e68f5f45a2d857eb57a Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 6 Nov 2019 17:53:42 -0700 Subject: [PATCH 07/19] update dev tools version for ci --- build/int.cloudbuild.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/int.cloudbuild.yaml b/build/int.cloudbuild.yaml index ff44db3..02fa976 100644 --- a/build/int.cloudbuild.yaml +++ b/build/int.cloudbuild.yaml @@ -38,4 +38,4 @@ tags: - 'integration' substitutions: _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' - _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.4.6' + _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.5.3' From ba9a72ac160976ec36974ff27bca0a3dfe386abe Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 6 Nov 2019 18:05:52 -0700 Subject: [PATCH 08/19] keep the test setup directory --- test/setup/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/setup/.gitkeep diff --git a/test/setup/.gitkeep b/test/setup/.gitkeep new file mode 100644 index 0000000..e69de29 From ddd35d2dedd0ef7fb9e80d509670829c3d71b684 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Thu, 7 Nov 2019 09:17:28 -0700 Subject: [PATCH 09/19] update platform to gcp --- kitchen.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kitchen.yml b/kitchen.yml index 0f37216..a8b4ee0 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -23,7 +23,7 @@ verifier: backend: gcp platforms: - - name: local + - name: gcp suites: - name: simple_example From 5728c323c611b38c5dee258ff7c9ccf111d3e4f4 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Thu, 7 Nov 2019 13:45:12 -0700 Subject: [PATCH 10/19] add a blank TF file to get tests to pass --- test/setup/main.tf | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/setup/main.tf diff --git a/test/setup/main.tf b/test/setup/main.tf new file mode 100644 index 0000000..ee50d7d --- /dev/null +++ b/test/setup/main.tf @@ -0,0 +1,16 @@ +/** + * 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. + */ + From 03d83ad3e91be0e2f8fef370f8ac34e5dfd63828 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 20 Nov 2019 09:57:07 -0700 Subject: [PATCH 11/19] remove regions and multi-region example --- README.md | 13 ---------- examples/multi_region/README.md | 19 -------------- examples/multi_region/main.tf | 25 ------------------- examples/multi_region/outputs.tf | 25 ------------------- examples/multi_region/versions.tf | 19 -------------- kitchen.yml | 4 --- main.tf | 17 ------------- outputs.tf | 5 ---- .../multi_region/controls/gcloud.rb | 22 ---------------- test/integration/multi_region/inspec.yml | 14 ----------- variables.tf | 6 ----- 11 files changed, 169 deletions(-) delete mode 100644 examples/multi_region/README.md delete mode 100644 examples/multi_region/main.tf delete mode 100644 examples/multi_region/outputs.tf delete mode 100644 examples/multi_region/versions.tf delete mode 100644 test/integration/multi_region/controls/gcloud.rb delete mode 100644 test/integration/multi_region/inspec.yml diff --git a/README.md b/README.md index 87426ce..33553da 100644 --- a/README.md +++ b/README.md @@ -9,17 +9,6 @@ This module does not communicate with GCP in any way. Basic usage of this module is as follows: ```hcl -module "utils" { - source = "terraform-google-modules/utils/google" - version = "~> 0.1" - regions = ["us-central1", "us-west1"] -} - -output "region_short_name_map" { - description = "Map of full to short name of all given regions" - value = "${module.utils.region_short_name_map}" -} - module "utils" { source = "terraform-google-modules/utils/google" version = "~> 0.1" @@ -41,14 +30,12 @@ Functional examples are included in the | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| | region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | -| regions | A list of GCP region codes for each of the given regions. i.e. ["us-central1"] => {"us-central1"="usc1"} | list | `` | no | ## Outputs | Name | Description | |------|-------------| | region\_short\_name | The 4 or 5 character shortname of a given region. | -| region\_short\_name\_map | The 4 or 5 character shortname of any given region. | diff --git a/examples/multi_region/README.md b/examples/multi_region/README.md deleted file mode 100644 index 0ff83aa..0000000 --- a/examples/multi_region/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# Simple Example - -This example illustrates how to use the `utils` module. - - -## Outputs - -| Name | Description | -|------|-------------| -| region\_short\_name | The 4 or 5 character shortname of a given region. | -| region\_short\_name\_map | The 4 or 5 character shortname of any given region. | - - - -To provision this example, run the following from 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/multi_region/main.tf b/examples/multi_region/main.tf deleted file mode 100644 index 862ebd0..0000000 --- a/examples/multi_region/main.tf +++ /dev/null @@ -1,25 +0,0 @@ -/** - * 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. - */ - -provider "google" { - version = "~> 2.0" -} - -module "utils" { - source = "../.." - regions = ["us-central1", "us-east1"] -} - diff --git a/examples/multi_region/outputs.tf b/examples/multi_region/outputs.tf deleted file mode 100644 index 88a0d2d..0000000 --- a/examples/multi_region/outputs.tf +++ /dev/null @@ -1,25 +0,0 @@ -/** - * 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 "region_short_name_map" { - description = "The 4 or 5 character shortname of any given region." - value = "${module.utils.region_short_name_map}" -} - -output "region_short_name" { - description = "The 4 or 5 character shortname of a given region." - value = "${module.utils.region_short_name}" -} diff --git a/examples/multi_region/versions.tf b/examples/multi_region/versions.tf deleted file mode 100644 index 832ec1d..0000000 --- a/examples/multi_region/versions.tf +++ /dev/null @@ -1,19 +0,0 @@ -/** - * 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" -} diff --git a/kitchen.yml b/kitchen.yml index a8b4ee0..b65446c 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -30,7 +30,3 @@ suites: driver: name: terraform root_module_directory: test/fixtures/simple_example - - name: multi_region - driver: - name: terraform - root_module_directory: test/fixtures/multi_region diff --git a/main.tf b/main.tf index 1c4cd9e..c1d7581 100644 --- a/main.tf +++ b/main.tf @@ -33,21 +33,4 @@ locals { local.continent_short_name[local.parts[0]], replace(local.parts[1], "/(n)orth|(s)outh|(e)ast|(w)est|(c)entral/", "$1$2$3$4$5") ]) - # Same computation but kick back a map - region_short_name_map = var.regions == [] ? {} : { for full_region in var.regions : full_region => - join( - "", [ - local.continent_short_name[split( - "-", - full_region - )[0]], - replace( - split( - "-", full_region)[1], - "/(n)orth|(s)outh|(e)ast|(w)est|(c)entral/", - "$1$2$3$4$5" - ) - ] - ) - } } diff --git a/outputs.tf b/outputs.tf index 6e2b31a..0476440 100644 --- a/outputs.tf +++ b/outputs.tf @@ -14,11 +14,6 @@ * limitations under the License. */ -output "region_short_name_map" { - description = "The 4 or 5 character shortname of any given region." - value = "${local.region_short_name_map}" -} - output "region_short_name" { description = "The 4 or 5 character shortname of a given region." value = "${local.region_short_name}" diff --git a/test/integration/multi_region/controls/gcloud.rb b/test/integration/multi_region/controls/gcloud.rb deleted file mode 100644 index ab4a446..0000000 --- a/test/integration/multi_region/controls/gcloud.rb +++ /dev/null @@ -1,22 +0,0 @@ -# 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. - -output_region_map = attribute("output_region_short_name_map") - -describe "outputs" do - it 'should have a valid region' do - expect(output_region_map["us-central1"]).to eq "usc1" - expect(output_region_map["us-east1"]).to eq "use1" - end -end diff --git a/test/integration/multi_region/inspec.yml b/test/integration/multi_region/inspec.yml deleted file mode 100644 index 0b35d3a..0000000 --- a/test/integration/multi_region/inspec.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: simple_example -depends: - - name: inspec-gcp - git: https://github.com/inspec/inspec-gcp.git - tag: v0.10.0 -supports: - - platform: gcp -attributes: - - name: regions - required: true - type: array - - name: output_region_short_name_map - required: true - type: hash diff --git a/variables.tf b/variables.tf index 43b5eec..09327b0 100644 --- a/variables.tf +++ b/variables.tf @@ -14,12 +14,6 @@ * limitations under the License. */ -variable "regions" { - description = "A list of GCP region codes for each of the given regions. i.e. [\"us-central1\"] => {\"us-central1\"=\"usc1\"}" - type = list - default = [] -} - variable "region" { description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" type = string From d96db265e5aed557e1c8624e5c0b3685f9745252 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Tue, 26 Nov 2019 15:59:52 -0800 Subject: [PATCH 12/19] Revert "remove regions and multi-region example" This reverts commit 03d83ad3e91be0e2f8fef370f8ac34e5dfd63828. --- README.md | 13 ++++++++++ examples/multi_region/README.md | 19 ++++++++++++++ examples/multi_region/main.tf | 25 +++++++++++++++++++ examples/multi_region/outputs.tf | 25 +++++++++++++++++++ examples/multi_region/versions.tf | 19 ++++++++++++++ kitchen.yml | 4 +++ main.tf | 17 +++++++++++++ outputs.tf | 5 ++++ .../multi_region/controls/gcloud.rb | 22 ++++++++++++++++ test/integration/multi_region/inspec.yml | 14 +++++++++++ variables.tf | 6 +++++ 11 files changed, 169 insertions(+) create mode 100644 examples/multi_region/README.md create mode 100644 examples/multi_region/main.tf create mode 100644 examples/multi_region/outputs.tf create mode 100644 examples/multi_region/versions.tf create mode 100644 test/integration/multi_region/controls/gcloud.rb create mode 100644 test/integration/multi_region/inspec.yml diff --git a/README.md b/README.md index 33553da..87426ce 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,17 @@ This module does not communicate with GCP in any way. Basic usage of this module is as follows: ```hcl +module "utils" { + source = "terraform-google-modules/utils/google" + version = "~> 0.1" + regions = ["us-central1", "us-west1"] +} + +output "region_short_name_map" { + description = "Map of full to short name of all given regions" + value = "${module.utils.region_short_name_map}" +} + module "utils" { source = "terraform-google-modules/utils/google" version = "~> 0.1" @@ -30,12 +41,14 @@ Functional examples are included in the | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| | region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | +| regions | A list of GCP region codes for each of the given regions. i.e. ["us-central1"] => {"us-central1"="usc1"} | list | `` | no | ## Outputs | Name | Description | |------|-------------| | region\_short\_name | The 4 or 5 character shortname of a given region. | +| region\_short\_name\_map | The 4 or 5 character shortname of any given region. | diff --git a/examples/multi_region/README.md b/examples/multi_region/README.md new file mode 100644 index 0000000..0ff83aa --- /dev/null +++ b/examples/multi_region/README.md @@ -0,0 +1,19 @@ +# Simple Example + +This example illustrates how to use the `utils` module. + + +## Outputs + +| Name | Description | +|------|-------------| +| region\_short\_name | The 4 or 5 character shortname of a given region. | +| region\_short\_name\_map | The 4 or 5 character shortname of any given region. | + + + +To provision this example, run the following from 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/multi_region/main.tf b/examples/multi_region/main.tf new file mode 100644 index 0000000..862ebd0 --- /dev/null +++ b/examples/multi_region/main.tf @@ -0,0 +1,25 @@ +/** + * 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. + */ + +provider "google" { + version = "~> 2.0" +} + +module "utils" { + source = "../.." + regions = ["us-central1", "us-east1"] +} + diff --git a/examples/multi_region/outputs.tf b/examples/multi_region/outputs.tf new file mode 100644 index 0000000..88a0d2d --- /dev/null +++ b/examples/multi_region/outputs.tf @@ -0,0 +1,25 @@ +/** + * 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 "region_short_name_map" { + description = "The 4 or 5 character shortname of any given region." + value = "${module.utils.region_short_name_map}" +} + +output "region_short_name" { + description = "The 4 or 5 character shortname of a given region." + value = "${module.utils.region_short_name}" +} diff --git a/examples/multi_region/versions.tf b/examples/multi_region/versions.tf new file mode 100644 index 0000000..832ec1d --- /dev/null +++ b/examples/multi_region/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" +} diff --git a/kitchen.yml b/kitchen.yml index b65446c..a8b4ee0 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -30,3 +30,7 @@ suites: driver: name: terraform root_module_directory: test/fixtures/simple_example + - name: multi_region + driver: + name: terraform + root_module_directory: test/fixtures/multi_region diff --git a/main.tf b/main.tf index c1d7581..1c4cd9e 100644 --- a/main.tf +++ b/main.tf @@ -33,4 +33,21 @@ locals { local.continent_short_name[local.parts[0]], replace(local.parts[1], "/(n)orth|(s)outh|(e)ast|(w)est|(c)entral/", "$1$2$3$4$5") ]) + # Same computation but kick back a map + region_short_name_map = var.regions == [] ? {} : { for full_region in var.regions : full_region => + join( + "", [ + local.continent_short_name[split( + "-", + full_region + )[0]], + replace( + split( + "-", full_region)[1], + "/(n)orth|(s)outh|(e)ast|(w)est|(c)entral/", + "$1$2$3$4$5" + ) + ] + ) + } } diff --git a/outputs.tf b/outputs.tf index 0476440..6e2b31a 100644 --- a/outputs.tf +++ b/outputs.tf @@ -14,6 +14,11 @@ * limitations under the License. */ +output "region_short_name_map" { + description = "The 4 or 5 character shortname of any given region." + value = "${local.region_short_name_map}" +} + output "region_short_name" { description = "The 4 or 5 character shortname of a given region." value = "${local.region_short_name}" diff --git a/test/integration/multi_region/controls/gcloud.rb b/test/integration/multi_region/controls/gcloud.rb new file mode 100644 index 0000000..ab4a446 --- /dev/null +++ b/test/integration/multi_region/controls/gcloud.rb @@ -0,0 +1,22 @@ +# 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. + +output_region_map = attribute("output_region_short_name_map") + +describe "outputs" do + it 'should have a valid region' do + expect(output_region_map["us-central1"]).to eq "usc1" + expect(output_region_map["us-east1"]).to eq "use1" + end +end diff --git a/test/integration/multi_region/inspec.yml b/test/integration/multi_region/inspec.yml new file mode 100644 index 0000000..0b35d3a --- /dev/null +++ b/test/integration/multi_region/inspec.yml @@ -0,0 +1,14 @@ +name: simple_example +depends: + - name: inspec-gcp + git: https://github.com/inspec/inspec-gcp.git + tag: v0.10.0 +supports: + - platform: gcp +attributes: + - name: regions + required: true + type: array + - name: output_region_short_name_map + required: true + type: hash diff --git a/variables.tf b/variables.tf index 09327b0..43b5eec 100644 --- a/variables.tf +++ b/variables.tf @@ -14,6 +14,12 @@ * limitations under the License. */ +variable "regions" { + description = "A list of GCP region codes for each of the given regions. i.e. [\"us-central1\"] => {\"us-central1\"=\"usc1\"}" + type = list + default = [] +} + variable "region" { description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" type = string From db0b4aa67c36a2aa84212cfdf2448a98b8876064 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Tue, 26 Nov 2019 16:28:23 -0800 Subject: [PATCH 13/19] re-add multi-region example with all regions output --- README.md | 1 - examples/multi_region/main.tf | 4 +--- main.tf | 24 ++++++++++++++++++- .../multi_region/controls/gcloud.rb | 18 ++++++++++++++ variables.tf | 6 ----- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 87426ce..8765abd 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,6 @@ Functional examples are included in the | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| | region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | -| regions | A list of GCP region codes for each of the given regions. i.e. ["us-central1"] => {"us-central1"="usc1"} | list | `` | no | ## Outputs diff --git a/examples/multi_region/main.tf b/examples/multi_region/main.tf index 862ebd0..8f08e28 100644 --- a/examples/multi_region/main.tf +++ b/examples/multi_region/main.tf @@ -19,7 +19,5 @@ provider "google" { } module "utils" { - source = "../.." - regions = ["us-central1", "us-east1"] + source = "../.." } - diff --git a/main.tf b/main.tf index 1c4cd9e..0c46189 100644 --- a/main.tf +++ b/main.tf @@ -19,6 +19,28 @@ terraform { } locals { + regions = [ + "asia-east1", + "asia-east2", + "asia-northeast1", + "asia-northeast2", + "asia-south1", + "asia-southeast1", + "australia-southeast1", + "europe-north1", + "europe-west1", + "europe-west2", + "europe-west3", + "europe-west4", + "europe-west6", + "northamerica-northeast1", + "southamerica-east1", + "us-central1", + "us-east1", + "us-east4", + "us-west1", + "us-west2" + ] # Compute the regional shortname from component parts continent_short_name = { asia = "az" @@ -34,7 +56,7 @@ locals { replace(local.parts[1], "/(n)orth|(s)outh|(e)ast|(w)est|(c)entral/", "$1$2$3$4$5") ]) # Same computation but kick back a map - region_short_name_map = var.regions == [] ? {} : { for full_region in var.regions : full_region => + region_short_name_map = { for full_region in local.regions : full_region => join( "", [ local.continent_short_name[split( diff --git a/test/integration/multi_region/controls/gcloud.rb b/test/integration/multi_region/controls/gcloud.rb index ab4a446..bc6fc07 100644 --- a/test/integration/multi_region/controls/gcloud.rb +++ b/test/integration/multi_region/controls/gcloud.rb @@ -16,7 +16,25 @@ describe "outputs" do it 'should have a valid region' do + expect(output_region_map["asia-east1"]).to eq "aze1" + expect(output_region_map["asia-east2"]).to eq "aze2" + expect(output_region_map["asia-northeast1"]).to eq "azne1" + expect(output_region_map["asia-northeast2"]).to eq "azne2" + expect(output_region_map["asia-south1"]).to eq "azs1" + expect(output_region_map["asia-southeast1"]).to eq "azse1" + expect(output_region_map["australia-southeast1"]).to eq "ause1" + expect(output_region_map["europe-north1"]).to eq "eun1" + expect(output_region_map["europe-west1"]).to eq "euw1" + expect(output_region_map["europe-west2"]).to eq "euw2" + expect(output_region_map["europe-west3"]).to eq "euw3" + expect(output_region_map["europe-west4"]).to eq "euw4" + expect(output_region_map["europe-west6"]).to eq "euw6" + expect(output_region_map["northamerica-northeast1"]).to eq "nane1" + expect(output_region_map["southamerica-east1"]).to eq "sae1" expect(output_region_map["us-central1"]).to eq "usc1" expect(output_region_map["us-east1"]).to eq "use1" + expect(output_region_map["us-east4"]).to eq "use4" + expect(output_region_map["us-west1"]).to eq "usw1" + expect(output_region_map["us-west2"]).to eq "usw2" end end diff --git a/variables.tf b/variables.tf index 43b5eec..09327b0 100644 --- a/variables.tf +++ b/variables.tf @@ -14,12 +14,6 @@ * limitations under the License. */ -variable "regions" { - description = "A list of GCP region codes for each of the given regions. i.e. [\"us-central1\"] => {\"us-central1\"=\"usc1\"}" - type = list - default = [] -} - variable "region" { description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" type = string From cc113f9adc261d419c8a7b73483384d1bd4dec8a Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 27 Nov 2019 13:56:50 -0800 Subject: [PATCH 14/19] Update variables.tf Co-Authored-By: Morgante Pell --- variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/variables.tf b/variables.tf index 09327b0..0adf9c9 100644 --- a/variables.tf +++ b/variables.tf @@ -15,7 +15,7 @@ */ variable "region" { - description = "A list of GCP region codes for each of the given regions. i.e. \"us-central1\"=\"usc1\"" + description = "The GCP region to retrieve a short name for (ex. `us-central1)." type = string default = null } From bc6218fb445a62672eebcb7bfb99f74ab4751fd6 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 27 Nov 2019 13:57:39 -0800 Subject: [PATCH 15/19] Update outputs.tf Co-Authored-By: Morgante Pell --- outputs.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/outputs.tf b/outputs.tf index 6e2b31a..d604cb3 100644 --- a/outputs.tf +++ b/outputs.tf @@ -20,6 +20,6 @@ output "region_short_name_map" { } output "region_short_name" { - description = "The 4 or 5 character shortname of a given region." + description = "The 4 or 5 character shortname of the region specified in var.region." value = "${local.region_short_name}" } From f08eb42ba9aaa792958f67e03c450f9d07bbde2d Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 27 Nov 2019 14:59:56 -0700 Subject: [PATCH 16/19] don't need this output in the multi-region example --- examples/multi_region/outputs.tf | 5 ----- 1 file changed, 5 deletions(-) diff --git a/examples/multi_region/outputs.tf b/examples/multi_region/outputs.tf index 88a0d2d..7c22fea 100644 --- a/examples/multi_region/outputs.tf +++ b/examples/multi_region/outputs.tf @@ -18,8 +18,3 @@ output "region_short_name_map" { description = "The 4 or 5 character shortname of any given region." value = "${module.utils.region_short_name_map}" } - -output "region_short_name" { - description = "The 4 or 5 character shortname of a given region." - value = "${module.utils.region_short_name}" -} From caad5f904aa1b4f55a0da9595945b412d57490b6 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 27 Nov 2019 15:08:44 -0700 Subject: [PATCH 17/19] update README without unnecessary output --- examples/multi_region/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/multi_region/README.md b/examples/multi_region/README.md index 0ff83aa..38e3160 100644 --- a/examples/multi_region/README.md +++ b/examples/multi_region/README.md @@ -7,7 +7,6 @@ This example illustrates how to use the `utils` module. | Name | Description | |------|-------------| -| region\_short\_name | The 4 or 5 character shortname of a given region. | | region\_short\_name\_map | The 4 or 5 character shortname of any given region. | From fb1c07fad21bf1c01d4942779c3cca80b62f073e Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Wed, 27 Nov 2019 15:09:01 -0700 Subject: [PATCH 18/19] update readme with better example of how to use this module --- README.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8765abd..50c7982 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # terraform-google-utils -This module provides a way to get the shortnames for given GCP region(s). For example, "us-central1" becomes "usc1". +This module provides a way to get the shortnames for a given GCP region. For example, "us-central1" becomes "usc1". This module also always outputs a map for every region, which allows you to do multiple at once. This module does not communicate with GCP in any way. @@ -12,23 +12,21 @@ Basic usage of this module is as follows: module "utils" { source = "terraform-google-modules/utils/google" version = "~> 0.1" - regions = ["us-central1", "us-west1"] + region = "us-central1" } -output "region_short_name_map" { - description = "Map of full to short name of all given regions" - value = "${module.utils.region_short_name_map}" +locals { + "us-central1" = module.utils.region_short_name + "asia-east1" = module.utils.region_short_name_map["asia-east1"] } +``` -module "utils" { - source = "terraform-google-modules/utils/google" - version = "~> 0.1" - region = "us-central1" -} +The above results in locals with computed values of: -output "region_short_name" { - description = "Short name of the given region" - value = "${module.utils.region_short_name}" +``` +locals { + "us-central1" = "usc1" + "asia-east1" = "aze1" } ``` @@ -40,13 +38,13 @@ Functional examples are included in the | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| -| region | A list of GCP region codes for each of the given regions. i.e. "us-central1"="usc1" | string | `"null"` | no | +| region | The GCP region to retrieve a short name for (ex. `us-central1). | string | `"null"` | no | ## Outputs | Name | Description | |------|-------------| -| region\_short\_name | The 4 or 5 character shortname of a given region. | +| region\_short\_name | The 4 or 5 character shortname of the region specified in var.region. | | region\_short\_name\_map | The 4 or 5 character shortname of any given region. | From c418df7b1e2596d434c8e13d503f0f8a529916b0 Mon Sep 17 00:00:00 2001 From: Miles Matthias Date: Sun, 15 Dec 2019 10:45:26 -0700 Subject: [PATCH 19/19] fix linting & use latest build tools docker image --- Makefile | 2 +- build/int.cloudbuild.yaml | 2 +- build/lint.cloudbuild.yaml | 2 +- examples/multi_region/README.md | 1 + examples/multi_region/outputs.tf | 5 +++++ 5 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index e01fd64..86e8882 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ # Make will use bash instead of sh SHELL := /usr/bin/env bash -DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.5.3 +DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0 DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools REGISTRY_URL := gcr.io/cloud-foundation-cicd diff --git a/build/int.cloudbuild.yaml b/build/int.cloudbuild.yaml index 02fa976..4761162 100644 --- a/build/int.cloudbuild.yaml +++ b/build/int.cloudbuild.yaml @@ -38,4 +38,4 @@ tags: - 'integration' substitutions: _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' - _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.5.3' + _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0' diff --git a/build/lint.cloudbuild.yaml b/build/lint.cloudbuild.yaml index 4e27761..0569972 100644 --- a/build/lint.cloudbuild.yaml +++ b/build/lint.cloudbuild.yaml @@ -21,4 +21,4 @@ tags: - 'lint' substitutions: _DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools' - _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.4.6' + _DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0' diff --git a/examples/multi_region/README.md b/examples/multi_region/README.md index 38e3160..0ff83aa 100644 --- a/examples/multi_region/README.md +++ b/examples/multi_region/README.md @@ -7,6 +7,7 @@ This example illustrates how to use the `utils` module. | Name | Description | |------|-------------| +| region\_short\_name | The 4 or 5 character shortname of a given region. | | region\_short\_name\_map | The 4 or 5 character shortname of any given region. | diff --git a/examples/multi_region/outputs.tf b/examples/multi_region/outputs.tf index 7c22fea..88a0d2d 100644 --- a/examples/multi_region/outputs.tf +++ b/examples/multi_region/outputs.tf @@ -18,3 +18,8 @@ output "region_short_name_map" { description = "The 4 or 5 character shortname of any given region." value = "${module.utils.region_short_name_map}" } + +output "region_short_name" { + description = "The 4 or 5 character shortname of a given region." + value = "${module.utils.region_short_name}" +}