Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/103 submodules #116

Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
dedcf4c
fix makefile
taylorludwig Nov 25, 2019
0a135c0
move vpc creation to its own module
taylorludwig Nov 25, 2019
987b4e4
move subnets to own module
taylorludwig Nov 25, 2019
28a26a2
move routes to its own submodule
taylorludwig Nov 25, 2019
211a516
use for_each for route creation
taylorludwig Nov 25, 2019
2dd1fad
update developer tools version to fix generate doc errors
taylorludwig Nov 25, 2019
6e0faef
generate docs
taylorludwig Nov 25, 2019
2c8ed09
add readme to submodules
taylorludwig Nov 25, 2019
f566110
formatting fix
taylorludwig Nov 25, 2019
f3e5e99
changelog
taylorludwig Nov 25, 2019
605269a
lock provider for google_project_services removal error
taylorludwig Nov 25, 2019
3a3a408
specify provider versions for each module for terraform init/validate
taylorludwig Nov 25, 2019
fec8285
Revert "specify provider versions for each module for terraform init/…
taylorludwig Nov 25, 2019
560bbb2
required provider versions on modules
taylorludwig Nov 25, 2019
fc02b37
Update CHANGELOG.md
taylorludwig Nov 25, 2019
188bf1c
remove compatability from submodules
taylorludwig Nov 26, 2019
2831941
update variable descriptions
taylorludwig Nov 26, 2019
8649e43
switch routes back to list type with map generated within module
taylorludwig Nov 26, 2019
d567d89
default rotue values to null so google_compute_route defaults are used
taylorludwig Nov 26, 2019
b5b9ad8
update output names per pr
taylorludwig Nov 26, 2019
e95a6b7
switch shared_vpc_host to bool
taylorludwig Nov 26, 2019
a351412
simpler way to return project id for shared vpc
taylorludwig Nov 26, 2019
4146283
update changelog and readme for list structure
taylorludwig Nov 26, 2019
c9a6090
fix terraform fmt issues
taylorludwig Nov 26, 2019
db0448e
name isn't required anymore
taylorludwig Nov 26, 2019
84119a8
default output needs to be to match original value so tests and back…
taylorludwig Nov 26, 2019
babfafa
fix readme examples
taylorludwig Nov 26, 2019
6b10555
remove unused leftover var
taylorludwig Nov 26, 2019
f0443f7
remove network var for route depedency. Its inherit by passing in net…
taylorludwig Nov 26, 2019
f86ad43
remove description
taylorludwig Nov 26, 2019
2f6aee7
rename svpc_project_id to project_id
taylorludwig Nov 26, 2019
6e7ffd1
use a module_depends_on list instead of using explicit subnet
taylorludwig Nov 26, 2019
da5372d
lint/validate fixes
taylorludwig Nov 26, 2019
7ec22d8
initial migrate.py from gke module
taylorludwig Nov 27, 2019
99a3819
update migrate.py to work with google-network resources and for_each …
taylorludwig Nov 27, 2019
69615da
remove old sh migrate script
taylorludwig Nov 27, 2019
2d4027a
update readme for new py migrate script
taylorludwig Nov 27, 2019
3afde82
python format
taylorludwig Nov 27, 2019
9342a26
update migration doc to mention issue with single count mvs
taylorludwig Dec 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning][semver-site].
## [Unreleased]
v2.0.0 is a backwards-incompatible release. Please see the [upgrading guide](./docs/upgrading_to_v2.0.md).

### Added

- Split main module up into vpc, subnets, and routes submodules. Changed `routes` input to be comptiable with `for_each` `maps` [#103]
taylorludwig marked this conversation as resolved.
Show resolved Hide resolved

### Fixed

- Fixes subnet recreation when a subnet is updated. [#73]
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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.6.0
DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools
REGISTRY_URL := gcr.io/cloud-foundation-cicd

Expand All @@ -42,7 +42,7 @@ docker_test_prepare:

# Clean up test environment within the docker container
.PHONY: docker_test_cleanup
docker_test_prepare:
docker_test_cleanup:
docker run --rm -it \
-e SERVICE_ACCOUNT_JSON \
-e TF_VAR_org_id \
Expand Down
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ It supports creating:
- Subnets within the VPC
- Secondary ranges for the subnets (if applicable)

Sub modules are provided for creating individual vpc, subnets, and routes. See the modules directory for the various sub modules usage.

## Compatibility

This module is meant for use with Terraform 0.12. If you haven't [upgraded](https://www.terraform.io/upgrade-guides/0-12.html) and need a Terraform 0.11.x-compatible version of this module, the last released version intended for Terraform 0.11.x is [0.8.0](https://registry.terraform.io/modules/terraform-google-modules/network/google/0.8.0).
Expand Down Expand Up @@ -60,23 +62,21 @@ module "vpc" {
subnet-02 = []
}

routes = [
{
name = "egress-internet"
routes = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to change this into a map. We can still maintain the desired behavior by constructing the map (for for_each) internally within the module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, makes sense.

Ill create a local var map with the original default name as the key.

"egress-internet" = {
description = "route through IGW to access internet"
destination_range = "0.0.0.0/0"
tags = "egress-inet"
next_hop_internet = "true"
},
{
name = "app-proxy"
}
"app-proxy" = {
description = "route through proxy to reach app"
destination_range = "10.50.10.0/24"
tags = "app-proxy"
next_hop_instance = "app-proxy-instance"
next_hop_instance_zone = "us-west1-a"
},
]
}
}
}
```

Expand All @@ -97,7 +97,7 @@ Then perform the following commands on the root folder:
| description | An optional description of this resource. The resource must be recreated to modify this field. | string | `""` | no |
| network\_name | The name of the network being created | string | n/a | yes |
| project\_id | The ID of the project where this VPC will be created | string | n/a | yes |
| routes | List of routes being created in this VPC | list(map(string)) | `<list>` | no |
| routes | Map of routes being created in this VPC | map(any) | `<map>` | no |
| routing\_mode | The network routing mode (default 'GLOBAL') | string | `"GLOBAL"` | no |
| secondary\_ranges | Secondary ranges that will be used in some of the subnets | object | `<map>` | no |
| shared\_vpc\_host | Makes this project a Shared VPC host if 'true' (default 'false') | string | `"false"` | no |
Expand All @@ -122,6 +122,7 @@ Then perform the following commands on the root folder:
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

### Subnet Inputs

The subnets list contains maps, where each object represents a subnet. Each map has the following inputs (please see examples folder for additional references):

| Name | Description | Type | Default | Required |
Expand All @@ -133,11 +134,12 @@ The subnets list contains maps, where each object represents a subnet. Each map
| subnet\_flow\_logs | Whether the subnet will record and send flow log data to logging | string | `"false"` | no |

### Route Inputs
The routes list contains maps, where each object represents a route. For the next\_hop\_* inputs, only one is possible to be used in each route. Having two next_hop_* inputs will produce an error. Each map has the following inputs (please see examples folder for additional references):

The `routes` map `key` is the unique route `name` and the `value` object represents the route input options. For the next\_hop\_* inputs, only one is possible to be used in each route. Having two next_hop_* inputs will produce an error. Each route has the following inputs (please see examples folder for additional references):

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| name | The name of the route being created | string | - | no |
| key | The name of the route being created | string | - | yes |
| description | The description of the route being created | string | - | no |
| tags | The network tags assigned to this route. This is a list in string format. Eg. "tag-01,tag-02"| string | - | yes |
| destination\_range | The destination range of outgoing packets that this route applies to. Only IPv4 is supported | string | - | yes
Expand Down
2 changes: 1 addition & 1 deletion build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.6.0'
2 changes: 1 addition & 1 deletion build/lint.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ tags:
- 'lint'
substitutions:
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.6.0'
23 changes: 10 additions & 13 deletions examples/multi_vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,29 @@ locals {
network_02_subnet_01 = "${var.network_02_name}-subnet-01"
network_02_subnet_02 = "${var.network_02_name}-subnet-02"

network_01_routes = [
{
name = "${var.network_01_name}-egress-inet"
network_01_routes = {
"${var.network_01_name}-egress-inet" = {
description = "route through IGW to access internet"
destination_range = "0.0.0.0/0"
tags = "egress-inet"
next_hop_internet = "true"
},
]
}
}

network_02_routes = [
{
name = "${var.network_02_name}-egress-inet"
network_02_routes = {
"${var.network_02_name}-egress-inet" = {
description = "route through IGW to access internet"
destination_range = "0.0.0.0/0"
tags = "egress-inet"
next_hop_internet = "true"
},
{
name = "${var.network_02_name}-testapp-proxy"
}
"${var.network_02_name}-testapp-proxy" = {
description = "route through proxy to reach app"
destination_range = "10.50.10.0/24"
tags = "app-proxy"
next_hop_ip = "10.10.40.10"
},
]
}
}
}

module "test-vpc-module-01" {
Expand Down
107 changes: 20 additions & 87 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,107 +14,40 @@
* limitations under the License.
*/

locals {
subnets = {
for x in var.subnets :
"${x.subnet_region}/${x.subnet_name}" => x
}
}

/******************************************
VPC configuration
*****************************************/
resource "google_compute_network" "network" {
name = var.network_name
module "vpc" {
source = "./modules/vpc"
network_name = var.network_name
auto_create_subnetworks = var.auto_create_subnetworks
routing_mode = var.routing_mode
project = var.project_id
project_id = var.project_id
description = var.description
}

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

/******************************************
Subnet configuration
*****************************************/
resource "google_compute_subnetwork" "subnetwork" {
for_each = local.subnets
name = each.value.subnet_name
ip_cidr_range = each.value.subnet_ip
region = each.value.subnet_region
private_ip_google_access = lookup(each.value, "subnet_private_access", "false")
dynamic "log_config" {
for_each = lookup(each.value, "subnet_flow_logs", false) ? [{
aggregation_interval = lookup(each.value, "subnet_flow_logs_interval", null)
flow_sampling = lookup(each.value, "subnet_flow_logs_sampling", null)
metadata = lookup(each.value, "subnet_flow_logs_metadata", null)
}] : []
content {
aggregation_interval = log_config.value.aggregation_interval
flow_sampling = log_config.value.flow_sampling
metadata = log_config.value.metadata
}
}
network = google_compute_network.network.name
project = var.project_id
description = lookup(each.value, "description", null)
secondary_ip_range = [
for i in range(
length(
contains(
keys(var.secondary_ranges), each.value.subnet_name) == true
? var.secondary_ranges[each.value.subnet_name]
: []
)) :
var.secondary_ranges[each.value.subnet_name][i]
]
module "subnets" {
source = "./modules/subnets"
project_id = var.project_id
network_name = module.vpc.network_name
description = var.description
subnets = var.subnets
secondary_ranges = var.secondary_ranges
}

/******************************************
Routes
*****************************************/
resource "google_compute_route" "route" {
count = length(var.routes)
project = var.project_id
network = var.network_name
name = lookup(var.routes[count.index], "name", format("%s-%s-%d", lower(var.network_name), "route", count.index))
morgante marked this conversation as resolved.
Show resolved Hide resolved
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", "")
next_hop_gateway = lookup(var.routes[count.index], "next_hop_internet", "false") == "true" ? "default-internet-gateway" : ""
next_hop_ip = lookup(var.routes[count.index], "next_hop_ip", "")
next_hop_instance = lookup(var.routes[count.index], "next_hop_instance", "")
next_hop_instance_zone = lookup(var.routes[count.index], "next_hop_instance_zone", "")
next_hop_vpn_tunnel = lookup(var.routes[count.index], "next_hop_vpn_tunnel", "")
priority = lookup(var.routes[count.index], "priority", "1000")

depends_on = [
google_compute_network.network,
google_compute_subnetwork.subnetwork,
]
}

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}"
}

triggers = {
number_of_routes = length(var.routes)
}

depends_on = [
google_compute_network.network,
google_compute_subnetwork.subnetwork,
google_compute_route.route,
]
module "routes" {
source = "./modules/routes"
project_id = var.project_id
network_name = module.vpc.network_name
routes = var.routes
delete_default_internet_gateway_routes = var.delete_default_internet_gateway_routes
network = module.vpc.network
subnets = module.subnets.subnets
}
6 changes: 5 additions & 1 deletion modules/fabric-net-firewall/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module "net-firewall" {
|------|-------------|:----:|:-----:|:-----:|
| admin\_ranges | IP CIDR ranges that have complete access to all subnets. | list | `<list>` | no |
| admin\_ranges\_enabled | Enable admin ranges-based rules. | string | `"false"` | no |
| custom\_rules | List of custom rule definitions (refer to variables file for syntax). | map | `<map>` | no |
| custom\_rules | List of custom rule definitions (refer to variables file for syntax). | object | `<map>` | no |
| http\_source\_ranges | List of IP CIDR ranges for tag-based HTTP rule, defaults to 0.0.0.0/0. | list | `<list>` | no |
| https\_source\_ranges | List of IP CIDR ranges for tag-based HTTPS rule, defaults to 0.0.0.0/0. | list | `<list>` | no |
| internal\_allow | Allow rules for internal ranges. | list | `<list>` | no |
Expand All @@ -89,6 +89,10 @@ module "net-firewall" {
| Name | Description |
|------|-------------|
| admin\_ranges | Admin ranges data. |
| custom\_egress\_allow\_rules | Custom egress rules with allow blocks. |
| custom\_egress\_deny\_rules | Custom egress rules with allow blocks. |
| custom\_ingress\_allow\_rules | Custom ingress rules with allow blocks. |
| custom\_ingress\_deny\_rules | Custom ingress rules with deny blocks. |
| internal\_ranges | Internal ranges. |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
4 changes: 4 additions & 0 deletions modules/fabric-net-firewall/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
terraform {
required_version = "~> 0.12.0"
}

provider "google" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a required version constraint, not a direct module invocation.

version = "~> 2.18.0"
}
4 changes: 4 additions & 0 deletions modules/fabric-net-svpc-access/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
terraform {
required_version = "~> 0.12.0"
}

provider "google" {
version = "~> 2.18.0"
}
Loading