Skip to content

Commit

Permalink
Fix case where nat_gateway_enabled is false (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuru authored May 4, 2021
1 parent 4e3780d commit 0027646
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
19 changes: 17 additions & 2 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ provider "aws" {
}

locals {
public_cidr_block = cidrsubnet(var.cidr_block, 1, 0)
private_cidr_block = cidrsubnet(var.cidr_block, 1, 1)
public_cidr_block = cidrsubnet(var.cidr_block, 2, 0)
public_only_cidr_block = cidrsubnet(var.cidr_block, 2, 1)
private_cidr_block = cidrsubnet(var.cidr_block, 2, 2)
}

module "vpc" {
Expand All @@ -30,6 +31,20 @@ module "public_subnets" {
context = module.this.context
}

module "public_only_subnets" {
source = "../../"

enabled = var.enabled
availability_zones = var.availability_zones
vpc_id = module.vpc.vpc_id
cidr_block = local.public_only_cidr_block
type = "public"
igw_id = module.vpc.igw_id
nat_gateway_enabled = false

context = module.this.context
}

module "private_subnets" {
source = "../../"

Expand Down
4 changes: 4 additions & 0 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ output "public_az_ngw_ids" {
value = module.public_subnets.az_ngw_ids
}

output "public_only_az_ngw_ids" {
value = module.public_only_subnets.az_ngw_ids
}

output "private_az_route_table_ids" {
value = module.private_subnets.az_route_table_ids
}
Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ locals {
subnet_id = local.public_enabled ? aws_subnet.public[az].id : aws_subnet.private[az].id
subnet_arn = local.public_enabled ? aws_subnet.public[az].arn : aws_subnet.private[az].arn
route_table_id = local.public_enabled ? aws_route_table.public[az].id : aws_route_table.private[az].id
ngw_id = local.public_enabled ? aws_nat_gateway.public[az].id : null
ngw_id = local.public_enabled && var.nat_gateway_enabled ? aws_nat_gateway.public[az].id : null
}
}
}
16 changes: 16 additions & 0 deletions test/src/examples_complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ func getKeys(m map[string]string) []string {
return keys
}

// Get values of a map in the same order that getKeys gets the keys of the map,
// which is lexicographically sored by keys.
func getValues(m map[string]string) []string {
values := make([]string, 0, len(m))
keys := getKeys(m)
for _, k := range keys {
values = append(values, m[k])
}
return values
}

func assertValueStartsWith(t *testing.T, m map[string]string, rx interface{}) {
for _, v := range m {
assert.Regexp(t, rx, v)
Expand Down Expand Up @@ -78,18 +89,23 @@ func TestExamplesComplete(t *testing.T) {
// Run `terraform output` to get the value of an output variable
publicNATGateWayIds := terraform.OutputMap(t, terraformOptions, "public_az_ngw_ids")
// Run `terraform output` to get the value of an output variable
publicOnlyNATGateWayIds := terraform.OutputMap(t, terraformOptions, "public_only_az_ngw_ids")
// Run `terraform output` to get the value of an output variable
publicRouteTableIds := terraform.OutputMap(t, terraformOptions, "public_az_route_table_ids")
// Run `terraform output` to get the value of an output variable
publicSubnetIds := terraform.OutputMap(t, terraformOptions, "public_az_subnet_ids")

expectedAZs := []string{"us-east-2a", "us-east-2b", "us-east-2c"}
expectedNulls := []string{"<nil>", "<nil>", "<nil>"}
// Verify we're getting back the outputs we expect
assert.Equal(t, expectedAZs, getKeys(privateSubnetIds))
assertValueStartsWith(t, privateSubnetIds, "^subnet-.*")
assert.Equal(t, expectedAZs, getKeys(privateRouteTableIds))
assertValueStartsWith(t, privateRouteTableIds, "^rtb-.*")
assert.Equal(t, expectedAZs, getKeys(publicNATGateWayIds))
assertValueStartsWith(t, publicNATGateWayIds, "^nat-.*")
assert.Equal(t, expectedAZs, getKeys(publicOnlyNATGateWayIds))
assert.Equal(t, expectedNulls, getValues(publicOnlyNATGateWayIds))
assert.Equal(t, expectedAZs, getKeys(publicRouteTableIds))
assertValueStartsWith(t, publicRouteTableIds, "^rtb-.*")
assert.Equal(t, expectedAZs, getKeys(publicSubnetIds))
Expand Down

0 comments on commit 0027646

Please sign in to comment.