Skip to content

Commit

Permalink
Merge pull request #96 from averbuks/averbuks-95
Browse files Browse the repository at this point in the history
Fixed issue with depending on outputs introduced in 1.4.1
  • Loading branch information
morgante authored Oct 31, 2019
2 parents 73a1d4b + 11a39c5 commit e273b5e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 156 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning][semver-site].

## [Unreleased]

## [1.4.3] - 2019-10-31

### Fixed

- Fixed issue with depending on outputs introduced in 1.4.1. [#95]

## [1.4.2] - 2019-10-30

### Fixed
Expand Down Expand Up @@ -140,7 +146,8 @@ and this project adheres to [Semantic Versioning][semver-site].
- Subnets within the VPC
- Secondary ranges for the subnets (if applicable)

[Unreleased]: https://github.com/terraform-google-modules/terraform-google-network/compare/v1.4.2...HEAD
[Unreleased]: https://github.com/terraform-google-modules/terraform-google-network/compare/v1.4.3...HEAD
[1.4.3]: https://github.com/terraform-google-modules/terraform-google-network/compare/v1.4.2...v1.4.3
[1.4.2]: https://github.com/terraform-google-modules/terraform-google-network/compare/v1.4.1...v1.4.2
[1.4.1]: https://github.com/terraform-google-modules/terraform-google-network/compare/v1.4.0...v1.4.1
[1.4.0]: https://github.com/terraform-google-modules/terraform-google-network/compare/v1.3.0...v1.4.0
Expand All @@ -157,6 +164,7 @@ and this project adheres to [Semantic Versioning][semver-site].
[0.2.0]: https://github.com/terraform-google-modules/terraform-google-network/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/terraform-google-modules/terraform-google-network/releases/tag/v0.1.0

[#95]: https://github.com/terraform-google-modules/terraform-google-network/issues/95
[#94]: https://github.com/terraform-google-modules/terraform-google-network/pull/94
[#92]: https://github.com/terraform-google-modules/terraform-google-network/issues/92
[#88]: https://github.com/terraform-google-modules/terraform-google-network/issues/88
Expand Down
202 changes: 82 additions & 120 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,137 +1,99 @@
## File structure
The project has the following folders and files:
# Contributing

- /: root folder
- /examples: examples for using this module
- /test: Folders with files for testing the module (see Testing section on this file)
- /main.tf: main file for this module, contains all the resources to create
- /variables.tf: all the variables for the module
- /output.tf: the outputs of the module
- /README.md: this file
This document provides guidelines for contributing to the module.

## Testing and documentation generation
## Dependencies

### Requirements
- [docker](https://docker.com/)
The following dependencies must be installed on the development system:

### Integration testing
##### Terraform integration tests
- [Docker Engine][docker-engine]
- [Google Cloud SDK][google-cloud-sdk]
- [make]

The module's integration tests are designed to be run within a Docker
container containing all the dependencies required for testing. The
`docker_test_integration` make target wraps this behavior but requires the
following configuration to execute properly:
## Generating Documentation for Inputs and Outputs

- Configure a service account with the roles documented above and export the JSON key to the `SERVICE_ACCOUNT_JSON` environment variable
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.

export SERVICE_ACCOUNT_JSON=$(< /path/to/credentials.json)
### Execution

- Create `test/fixtures/shared/terraform.tfvars` and populate with the required Terraform input variables (see `test/fixtures/shared/terraform.tfvars.sample` for more information)
Run `make generate_docs` to generate new Inputs and Outputs tables.

Once those steps have been completed run `make docker_test_integration` from
the root of the repository to execute the tests within the `project_id`
provided. Infrastructure from `test/fixtures/*` will be provisioned,
integration tests from `test/integration/*` will be executed, and the
infrastructure will be deprovisioned to complete the process.
## Integration Testing

### Autogeneration of documentation from .tf files
Run
```
make generate_docs
```
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.

### Lint testing
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.

Lint testing is also performed within a Docker container containing all the
dependencies required for lint tests. Execute those tests by running `make
docker_test_lint` from the root of the repository.
### Test Environment
The easiest way to test the module is in an isolated test project. The setup for such a project is defined in [test/setup](./test/setup/) directory.

Successful output looks similar to the following:
To use this setup, you need a service account with Project Creator access on a folder. Export the Service Account credentials to your environment like so:

```
export SERVICE_ACCOUNT_JSON=$(< credentials.json)
```

You will also need to set a few environment variables:
```
Checking for trailing whitespace
Checking for missing newline at end of file
Running shellcheck
Checking file headers
Running flake8
Running terraform fmt
terraform fmt -diff -check=true -write=false .
terraform fmt -diff -check=true -write=false ./codelabs/simple
terraform fmt -diff -check=true -write=false ./examples/delete_default_gateway_routes
terraform fmt -diff -check=true -write=false ./examples/multi_vpc
terraform fmt -diff -check=true -write=false ./examples/secondary_ranges
terraform fmt -diff -check=true -write=false ./examples/simple_project
terraform fmt -diff -check=true -write=false ./examples/simple_project_with_regional_network
terraform fmt -diff -check=true -write=false ./examples/submodule_firewall
terraform fmt -diff -check=true -write=false ./examples/submodule_svpc_access
terraform fmt -diff -check=true -write=false ./modules/fabric-net-firewall
terraform fmt -diff -check=true -write=false ./modules/fabric-net-svpc-access
terraform fmt -diff -check=true -write=false ./test/fixtures/all_examples
terraform fmt -diff -check=true -write=false ./test/fixtures/delete_default_gateway_routes
terraform fmt -diff -check=true -write=false ./test/fixtures/multi_vpc
terraform fmt -diff -check=true -write=false ./test/fixtures/secondary_ranges
terraform fmt -diff -check=true -write=false ./test/fixtures/shared
terraform fmt -diff -check=true -write=false ./test/fixtures/simple_project
terraform fmt -diff -check=true -write=false ./test/fixtures/simple_project_with_regional_network
terraform fmt -diff -check=true -write=false ./test/fixtures/simulated_ci_environment
terraform fmt -diff -check=true -write=false ./test/fixtures/submodule_firewall
Running terraform validate
terraform_validate .
Success! The configuration is valid.
terraform_validate ./codelabs/simple
Success! The configuration is valid.
terraform_validate ./examples/delete_default_gateway_routes
Success! The configuration is valid.
terraform_validate ./examples/multi_vpc
Success! The configuration is valid.
terraform_validate ./examples/secondary_ranges
Success! The configuration is valid.
terraform_validate ./examples/simple_project
Success! The configuration is valid.
terraform_validate ./examples/simple_project_with_regional_network
Success! The configuration is valid.
terraform_validate ./examples/submodule_firewall
Success! The configuration is valid.
terraform_validate ./examples/submodule_svpc_access
Success! The configuration is valid.
terraform_validate ./modules/fabric-net-firewall
Success! The configuration is valid.
terraform_validate ./modules/fabric-net-svpc-access
Success! The configuration is valid.
terraform_validate ./test/fixtures/all_examples
Success! The configuration is valid.
terraform_validate ./test/fixtures/delete_default_gateway_routes
Success! The configuration is valid.
terraform_validate ./test/fixtures/multi_vpc
Success! The configuration is valid.
terraform_validate ./test/fixtures/secondary_ranges
Success! The configuration is valid.
terraform_validate ./test/fixtures/simple_project
Success! The configuration is valid.
terraform_validate ./test/fixtures/simple_project_with_regional_network
Success! The configuration is valid.
terraform_validate ./test/fixtures/simulated_ci_environment
Success! The configuration is valid.
terraform_validate ./test/fixtures/submodule_firewall
Success! The configuration is valid.
export TF_VAR_org_id="your_org_id"
export TF_VAR_folder_id="your_folder_id"
export TF_VAR_billing_account="your_billing_account_id"
```

[terraform-provider-google]: https://github.com/terraform-providers/terraform-provider-google
With these settings in place, you can prepare a test project using Docker:
```
make docker_test_prepare
```

### 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 <EXAMPLE_NAME>` to initialize the working
directory for an example module.

1. Run `kitchen_do converge <EXAMPLE_NAME>` to apply the example module.

1. Run `kitchen_do verify <EXAMPLE_NAME>` to test the example module.

1. Run `kitchen_do destroy <EXAMPLE_NAME>` 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/
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,8 @@ If you are going to manage a Shared VPC, you must have either:
In order to operate with the Service Account you must activate the following API on the project where the Service Account was created:

- Compute Engine API - compute.googleapis.com

## Contributing

Refer to the [contribution guidelines](./CONTRIBUTING.md) for
information on contributing to this module.
21 changes: 13 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
* limitations under the License.
*/

locals {
network_self_link = var.create_network ? google_compute_network.network[0].self_link : data.google_compute_network.network[0].self_link
network_name = var.create_network ? google_compute_network.network[0].name : data.google_compute_network.network[0].name
}

/******************************************
VPC configuration
*****************************************/
Expand All @@ -27,17 +32,17 @@ resource "google_compute_network" "network" {
}

data "google_compute_network" "network" {
name = var.network_name
project = var.project_id
depends_on = [google_compute_network.network]
count = var.create_network ? 0 : 1
name = var.network_name
project = var.project_id
}

/******************************************
Shared VPC
*****************************************/
resource "google_compute_shared_vpc_host_project" "shared_vpc_host" {
count = var.shared_vpc_host == "true" ? 1 : 0
project = data.google_compute_network.network.project
project = var.project_id
}

/******************************************
Expand All @@ -51,7 +56,7 @@ resource "google_compute_subnetwork" "subnetwork" {
region = var.subnets[count.index]["subnet_region"]
private_ip_google_access = lookup(var.subnets[count.index], "subnet_private_access", "false")
enable_flow_logs = lookup(var.subnets[count.index], "subnet_flow_logs", "false")
network = data.google_compute_network.network.name
network = local.network_self_link
project = var.project_id
secondary_ip_range = [for i in range(length(contains(keys(var.secondary_ranges), var.subnets[count.index]["subnet_name"]) == true ? var.secondary_ranges[var.subnets[count.index]["subnet_name"]] : [])) : var.secondary_ranges[var.subnets[count.index]["subnet_name"]][i]]
description = lookup(var.subnets[count.index], "description", null)
Expand All @@ -71,8 +76,8 @@ data "google_compute_subnetwork" "created_subnets" {
resource "google_compute_route" "route" {
count = length(var.routes)
project = var.project_id
network = data.google_compute_network.network.name
name = lookup(var.routes[count.index], "name", format("%s-%s-%d", lower(var.network_name), "route", count.index))
network = local.network_name
name = lookup(var.routes[count.index], "name", format("%s-%s-%d", lower(local.network_name), "route", count.index))
description = lookup(var.routes[count.index], "description", "")
tags = compact(split(",", lookup(var.routes[count.index], "tags", "")))
dest_range = lookup(var.routes[count.index], "destination_range", "")
Expand All @@ -92,7 +97,7 @@ resource "null_resource" "delete_default_internet_gateway_routes" {
count = var.delete_default_internet_gateway_routes ? 1 : 0

provisioner "local-exec" {
command = "${path.module}/scripts/delete-default-gateway-routes.sh ${var.project_id} ${var.network_name}"
command = "${path.module}/scripts/delete-default-gateway-routes.sh ${var.project_id} ${local.network_name}"
}

triggers = {
Expand Down
4 changes: 2 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/

output "network_name" {
value = var.create_network ? google_compute_network.network[0].name : data.google_compute_network.network.name
value = local.network_name
description = "The name of the VPC being created"
}

output "network_self_link" {
value = var.create_network ? google_compute_network.network[0].self_link : data.google_compute_network.network.self_link
value = local.network_self_link
description = "The URI of the VPC being created"
}

Expand Down
2 changes: 1 addition & 1 deletion test/setup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

module "project" {
source = "terraform-google-modules/project-factory/google"
version = "~> 3.0"
version = "~> 4.0"

name = "ci-network"
random_project_id = "true"
Expand Down
24 changes: 0 additions & 24 deletions test/setup/make_source.sh

This file was deleted.

0 comments on commit e273b5e

Please sign in to comment.