Skip to content

Commit

Permalink
Merge pull request #3 from confusdcodr/t12
Browse files Browse the repository at this point in the history
Upgrade to terraform 0.12.x
  • Loading branch information
confusdcodr authored Sep 19, 2019
2 parents 668ce03 + 7b0d87f commit 32540a5
Show file tree
Hide file tree
Showing 20 changed files with 320 additions and 132 deletions.
3 changes: 2 additions & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[bumpversion]
current_version = 0.0.0
current_version = 1.0.0
commit = True
message = Bumps version to {new_version}
tag = False
tag_name = {new_version}

12 changes: 12 additions & 0 deletions .dependabot/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ update_configs:
- package_manager: "terraform"
directory: "/"
update_schedule: "daily"

- package_manager: "terraform"
directory: "/tests/create_pcx"
update_schedule: "daily"

- package_manager: "terraform"
directory: "/tests/no_create"
update_schedule: "daily"

- package_manager: "go:modules"
directory: "/tests"
update_schedule: "daily"
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ insert_final_newline = true
indent_style = space
indent_size = 2
charset = utf-8
tab_width = 4

[*.md]
trim_trailing_whitespace = false

[*.py]
indent_size = 4

[go.mod]
indent_style = tab
indent_size = 1

[*.go]
indent_style = tab
indent_size = 1

[Makefile]
indent_style = tab
indent_size = 1
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

### 1.0.0

**Released**: 2019.09.11

**Commit Delta**: [Change from 0.0.0 release](https://github.com/plus3it/terraform-aws-tardigrade-pcx/compare/0.0.0...1.0.0)

**Summary**:

* Upgrade to terraform 0.12.x
* Add test cases

### 0.0.0

**Commit Delta**: N/A
Expand Down
24 changes: 15 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,30 @@ json/format: | guard/program/jq
$(FIND_JSON) | $(XARGS) bash -c 'echo "$$(jq --indent 4 -S . "{}")" > "{}"'
@ echo "[$@]: Successfully formatted JSON files!"

docs/%: README_PARTS := _docs/MAIN.md <(echo) <(terraform-docs markdown table .)
tfdocs-awk/install: $(BIN_DIR)
tfdocs-awk/install: ARCHIVE := https://github.com/plus3it/tfdocs-awk/archive/master.tar.gz
tfdocs-awk/install:
$(CURL) $(ARCHIVE) | tar -C $(BIN_DIR) --strip-components=1 --wildcards '*.sh' --wildcards '*.awk' -xzvf -

docs/%: README_PARTS := _docs/MAIN.md <(echo) <($(BIN_DIR)/terraform-docs.sh markdown table .)
docs/%: README_FILE ?= README.md

docs/lint: | guard/program/terraform-docs
docs/lint: | guard/program/terraform-docs tfdocs-awk/install
@ echo "[$@]: Linting documentation files.."
diff $(README_FILE) <(cat $(README_PARTS))
@ echo "[$@]: Documentation files PASSED lint test!"

docs/generate: | guard/program/terraform-docs
docs/generate: | guard/program/terraform-docs tfdocs-awk/install
@ echo "[$@]: Creating documentation files.."
cat $(README_PARTS) > $(README_FILE)
@ echo "[$@]: Documentation files creation complete!"

dep/install: guard/program/curl
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

terratest/install: | guard/program/go guard/program/dep
cd tests && dep ensure
terratest/install: | guard/program/go
cd tests && go mod init terraform-aws-tardigrade-pcx/tests
cd tests && go build ./...
cd tests && go mod tidy

terratest/test: | guard/program/go guard/program/dep
terratest/test: | guard/program/go
cd tests && go test -timeout 20m

test: terratest/test
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ Terraform module to create a peering connection
| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| create\_peering\_connection | Controls whether to create the VPC Peering Connection | string | `"true"` | no |
| name | Name of the account; used to construct PCX Name tag | string | `""` | no |
| peer\_alias | Alias of the peer account; used to construct PCX Name tag | string | `""` | no |
| peer\_owner\_id | The AWS account ID of the owner of the peer VPC | string | `""` | no |
| peer\_route\_tables | List of IDs of route tables in the peer account to route to the account VPC CIDR | list | `<list>` | no |
| peer\_vpc\_id | The ID of the VPC with which you are creating the VPC Peering Connection | string | `""` | no |
| private\_route\_tables | List of IDs of private route tables to route to the peer VPC CIDR | list | `<list>` | no |
| private\_subnets | A list of private subnets inside the VPC | list | `<list>` | no |
| public\_route\_tables | List of IDs of public route tables to route to the peer VPC CIDR | list | `<list>` | no |
| public\_subnets | A list of public subnets inside the VPC | list | `<list>` | no |
| tags | A map of tags to add to the VPC peering connection | map | `<map>` | no |
| vpc\_cidr | CIDR for the VPC | string | `""` | no |
| vpc\_id | The ID of the requester VPC | string | `""` | no |
| name | Name of the account; used to construct PCX Name tag | string | `"null"` | no |
| peer\_alias | Alias of the peer account; used to construct PCX Name tag | string | `"null"` | no |
| peer\_owner\_id | The AWS account ID of the owner of the peer VPC. Required when `create_peering_connection` is `true` | string | `"null"` | no |
| peer\_route\_tables | List of IDs of route tables in the peer account to route to the account VPC CIDR | list(string) | `<list>` | no |
| peer\_vpc\_id | The ID of the VPC with which you are creating the VPC Peering Connection. Required when `create_peering_connection` is `true` | string | `"null"` | no |
| private\_route\_tables | List of IDs of private route tables to route to the peer VPC CIDR | list(string) | `<list>` | no |
| private\_subnets | A list of private subnets inside the VPC | list(string) | `<list>` | no |
| public\_route\_tables | List of IDs of public route tables to route to the peer VPC CIDR | list(string) | `<list>` | no |
| public\_subnets | A list of public subnets inside the VPC | list(string) | `<list>` | no |
| tags | A map of tags to add to the VPC peering connection | map(string) | `<map>` | no |
| vpc\_cidr | CIDR for the VPC. Required when `create_peering_connection` is `true` | string | `"null"` | no |
| vpc\_id | The ID of the requester VPC. Required when `create_peering_connection` is `true` | string | `"null"` | no |

## Outputs

Expand Down
83 changes: 38 additions & 45 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,77 +1,70 @@
provider "aws" {}
provider "aws" {
}

provider "aws" {
alias = "peer"
}

data "aws_vpc" "peer" {
count = "${var.create_peering_connection ? 1 : 0}"
count = var.create_peering_connection ? 1 : 0

provider = "aws.peer"
provider = aws.peer

id = "${var.peer_vpc_id}"
id = var.peer_vpc_id
}

resource "aws_vpc_peering_connection" "this" {
count = "${var.create_peering_connection ? 1 : 0}"

peer_owner_id = "${var.peer_owner_id}"
peer_vpc_id = "${var.peer_vpc_id}"
vpc_id = "${var.vpc_id}"
tags = "${merge(var.tags, map("Name", "${var.name}-${var.vpc_cidr}<->${var.peer_alias}-${data.aws_vpc.peer.cidr_block}"))}"
count = var.create_peering_connection ? 1 : 0

peer_owner_id = var.peer_owner_id
peer_vpc_id = var.peer_vpc_id
vpc_id = var.vpc_id
tags = merge(
var.tags,
{
"Name" = "${format("%v", var.name)}-${format("%v", var.vpc_cidr)}<->${format("%v", var.peer_alias)}-${data.aws_vpc.peer[0].cidr_block}"
},
)
}

resource "aws_vpc_peering_connection_accepter" "this" {
count = "${var.create_peering_connection ? 1 : 0}"
count = var.create_peering_connection ? 1 : 0

provider = "aws.peer"
provider = aws.peer

vpc_peering_connection_id = "${aws_vpc_peering_connection.this.id}"
vpc_peering_connection_id = aws_vpc_peering_connection.this[0].id
auto_accept = true

tags {
Name = "${var.peer_alias}-${data.aws_vpc.peer.cidr_block}<->${var.name}-${var.vpc_cidr}"
tags = {
Name = "${format("%v", var.peer_alias)}-${data.aws_vpc.peer[0].cidr_block}<->${format("%v", var.name)}-${format("%v", var.vpc_cidr)}"
}
}

resource "aws_route" "public" {
// Must pin count to avoid error "value of 'count' cannot be computed"
// See:
// * <https://docs.cloudposse.com/troubleshooting/terraform-value-of-count-cannot-be-computed/>
// * <https://github.com/hashicorp/terraform/issues/12570>
// Proper value once #12570 has some reasonable resolution:
// count = "${var.create_peering_connection ? length(var.public_route_tables) : 0}"
count = "${var.create_peering_connection ? 1 : 0}"

route_table_id = "${var.public_route_tables[count.index]}"
destination_cidr_block = "${data.aws_vpc.peer.cidr_block}"
vpc_peering_connection_id = "${aws_vpc_peering_connection.this.id}"
count = var.create_peering_connection ? length(var.public_route_tables) : 0

route_table_id = var.public_route_tables[count.index]
destination_cidr_block = data.aws_vpc.peer[0].cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.this[0].id
}

resource "aws_route" "private" {
// Must pin count to avoid error "value of 'count' cannot be computed"
// See:
// * <https://docs.cloudposse.com/troubleshooting/terraform-value-of-count-cannot-be-computed/>
// * <https://github.com/hashicorp/terraform/issues/12570>
// Proper value once #12570 has some reasonable resolution:
// count = "${var.create_peering_connection ? length(var.private_route_tables) : 0}"
count = "${var.create_peering_connection ? 1 : 0}"

route_table_id = "${var.private_route_tables[count.index]}"
destination_cidr_block = "${data.aws_vpc.peer.cidr_block}"
vpc_peering_connection_id = "${aws_vpc_peering_connection.this.id}"
count = var.create_peering_connection ? length(var.private_route_tables) : 0

route_table_id = var.private_route_tables[count.index]
destination_cidr_block = data.aws_vpc.peer[0].cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.this[0].id
}

resource "aws_route" "peer" {
// No need to pin this one, since we pass this value in
// I.e. We are not _creating_ the route tables in the same state...
count = "${var.create_peering_connection ? length(var.peer_route_tables) : 0}"
count = var.create_peering_connection ? length(var.peer_route_tables) : 0

provider = "aws.peer"
provider = aws.peer

route_table_id = "${var.peer_route_tables[count.index]}"
destination_cidr_block = "${var.vpc_cidr}"
vpc_peering_connection_id = "${aws_vpc_peering_connection_accepter.this.id}"
route_table_id = var.peer_route_tables[count.index]
destination_cidr_block = var.vpc_cidr
vpc_peering_connection_id = aws_vpc_peering_connection_accepter.this[0].id

depends_on = ["aws_vpc_peering_connection_accepter.this"]
depends_on = [aws_vpc_peering_connection_accepter.this]
}

5 changes: 3 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
output "vpc_peering_connection_id" {
description = "The ID of the VPC Peering Connection"
value = "${join("", aws_vpc_peering_connection_accepter.this.*.id)}"
value = join("", aws_vpc_peering_connection_accepter.this.*.id)
}

output "vpc_peering_connection_status" {
description = "The status of the VPC Peering Connection request"
value = "${join("", aws_vpc_peering_connection_accepter.this.*.accept_status)}"
value = join("", aws_vpc_peering_connection_accepter.this.*.accept_status)
}

48 changes: 48 additions & 0 deletions tests/create_pcx/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
provider aws {
region = "us-east-1"
}

data "aws_caller_identity" "current" {}

resource "random_string" "this" {
length = 6
number = false
special = false
upper = false
}

module "vpc_pcx_requester" {
source = "github.com/terraform-aws-modules/terraform-aws-vpc?ref=v2.15.0"

providers = {
aws = aws
}

name = "tardigrade-pcx-requester-${random_string.this.result}"
cidr = "10.0.0.0/16"
}

module "vpc_pcx_requestee" {
source = "github.com/terraform-aws-modules/terraform-aws-vpc?ref=v2.15.0"

providers = {
aws = aws
}

name = "tardigrade-pcx-requestee-${random_string.this.result}"
cidr = "10.1.0.0/16"
}

module "create_pcx" {
source = "../../"
providers = {
aws = aws
aws.peer = aws
}

create_peering_connection = true
name = "tardigrade-pcx-${random_string.this.result}"
vpc_id = module.vpc_pcx_requester.vpc_id
peer_owner_id = data.aws_caller_identity.current.account_id
peer_vpc_id = module.vpc_pcx_requestee.vpc_id
}
3 changes: 3 additions & 0 deletions tests/create_pcx/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}
7 changes: 0 additions & 7 deletions tests/example_testcase/main.tf

This file was deleted.

53 changes: 53 additions & 0 deletions tests/generated_route_tables/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
provider aws {
region = "us-east-1"
}

data "aws_caller_identity" "current" {}

resource "random_string" "this" {
length = 6
number = false
special = false
upper = false
}

module "vpc_pcx_requester" {
source = "github.com/terraform-aws-modules/terraform-aws-vpc?ref=v2.15.0"

providers = {
aws = aws
}

name = "tardigrade-pcx-requester-${random_string.this.result}"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b", "us-east-1c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
}

module "vpc_pcx_requestee" {
source = "github.com/terraform-aws-modules/terraform-aws-vpc?ref=v2.15.0"

providers = {
aws = aws
}

name = "tardigrade-pcx-requestee-${random_string.this.result}"
cidr = "10.1.0.0/16"
}

module "generated_route_tables" {
source = "../../"
providers = {
aws = aws
aws.peer = aws
}

create_peering_connection = true
name = "tardigrade-pcx-${random_string.this.result}"
vpc_id = module.vpc_pcx_requester.vpc_id
peer_owner_id = data.aws_caller_identity.current.account_id
peer_vpc_id = module.vpc_pcx_requestee.vpc_id
public_route_tables = module.vpc_pcx_requester.public_route_table_ids
private_route_tables = module.vpc_pcx_requester.private_route_table_ids
}
3 changes: 3 additions & 0 deletions tests/generated_route_tables/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}
11 changes: 11 additions & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module terraform-aws-tardigrade-pcx/tests

go 1.13

require (
github.com/gruntwork-io/terratest v0.19.0
github.com/magiconair/properties v1.8.1 // indirect
github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 // indirect
golang.org/x/net v0.0.0-20190918130420-a8b05e9114ab // indirect
)
Loading

0 comments on commit 32540a5

Please sign in to comment.