Skip to content

Commit 9acd741

Browse files
authored
Merge pull request #29 from glarizza/gl/25_delete_default_route
Provide ability to delete default gateway route
2 parents f43dd92 + d31726f commit 9acd741

File tree

16 files changed

+384
-4
lines changed

16 files changed

+384
-4
lines changed

.kitchen.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,16 @@ suites:
8080
backend: local
8181
controls:
8282
- gcloud
83+
- name: "delete_default_gateway_routes"
84+
driver:
85+
name: "terraform"
86+
command_timeout: 1800
87+
root_module_directory: test/fixtures/delete_default_gateway_routes/
88+
verifier:
89+
name: terraform
90+
color: true
91+
systems:
92+
- name: local
93+
backend: local
94+
controls:
95+
- gcloud

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Then perform the following commands on the root folder:
8080

8181
| Name | Description | Type | Default | Required |
8282
|------|-------------|:----:|:-----:|:-----:|
83+
| delete_default_internet_gateway_routes | If set, ensure that all routes within the network specified whose names begin with 'default-route' and with a next hop of 'default-internet-gateway' are deleted | string | `false` | no |
8384
| network_name | The name of the network being created | string | - | yes |
8485
| project_id | The ID of the project where this VPC will be created | string | - | yes |
8586
| routes | List of routes being created in this VPC | list | `<list>` | no |
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Delete Default Gateway Routes
2+
3+
This example configures a single simple VPC inside of a project.
4+
5+
This VPC has a single subnet with no secondary ranges, and ensures the default internet gateway route is deleted.
6+
7+
[^]: (autogen_docs_start)
8+
9+
10+
## Inputs
11+
12+
| Name | Description | Type | Default | Required |
13+
|------|-------------|:----:|:-----:|:-----:|
14+
| project_id | The project ID to host the network in | string | - | yes |
15+
16+
## Outputs
17+
18+
| Name | Description |
19+
|------|-------------|
20+
| network_name | The name of the VPC being created |
21+
| network_self_link | The URI of the VPC being created |
22+
| routes | The routes associated with this VPC |
23+
| subnets_flow_logs | Whether the subnets will have VPC flow logs enabled |
24+
| subnets_ips | The IP and cidrs of the subnets being created |
25+
| subnets_names | The names of the subnets being created |
26+
| subnets_private_access | Whether the subnets will have access to Google API's without a public IP |
27+
| subnets_regions | The region where subnets will be created |
28+
| subnets_secondary_ranges | The secondary ranges associated with these subnets |
29+
30+
[^]: (autogen_docs_end)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
network_name = "test-network-${random_string.random_suffix.result}"
19+
}
20+
21+
resource "random_string" "random_suffix" {
22+
length = 4
23+
upper = "false"
24+
special = "false"
25+
}
26+
27+
module "test-vpc-module" {
28+
source = "../../"
29+
project_id = "${var.project_id}"
30+
network_name = "${local.network_name}"
31+
delete_default_internet_gateway_routes = "true"
32+
33+
subnets = [
34+
{
35+
subnet_name = "subnet-41"
36+
subnet_ip = "10.20.30.0/24"
37+
subnet_region = "us-west1"
38+
},
39+
]
40+
41+
secondary_ranges = {
42+
subnet-41 = []
43+
}
44+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "network_name" {
18+
value = "${module.test-vpc-module.network_name}"
19+
description = "The name of the VPC being created"
20+
}
21+
22+
output "network_self_link" {
23+
value = "${module.test-vpc-module.network_self_link}"
24+
description = "The URI of the VPC being created"
25+
}
26+
27+
output "subnets_names" {
28+
value = "${module.test-vpc-module.subnets_names}"
29+
description = "The names of the subnets being created"
30+
}
31+
32+
output "subnets_ips" {
33+
value = "${module.test-vpc-module.subnets_ips}"
34+
description = "The IP and cidrs of the subnets being created"
35+
}
36+
37+
output "subnets_regions" {
38+
value = "${module.test-vpc-module.subnets_regions}"
39+
description = "The region where subnets will be created"
40+
}
41+
42+
output "subnets_private_access" {
43+
value = "${module.test-vpc-module.subnets_private_access}"
44+
description = "Whether the subnets will have access to Google API's without a public IP"
45+
}
46+
47+
output "subnets_flow_logs" {
48+
value = "${module.test-vpc-module.subnets_flow_logs}"
49+
description = "Whether the subnets will have VPC flow logs enabled"
50+
}
51+
52+
output "subnets_secondary_ranges" {
53+
value = "${module.test-vpc-module.subnets_secondary_ranges}"
54+
description = "The secondary ranges associated with these subnets"
55+
}
56+
57+
output "routes" {
58+
value = "${module.test-vpc-module.routes}"
59+
description = "The routes associated with this VPC"
60+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
description = "The project ID to host the network in"
19+
}

examples/multi_vpc/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ This example configures a host network project with two separate networks.
1616
| Name | Description |
1717
|------|-------------|
1818
| network_01_name | The name of the VPC network-01 |
19-
| network_01_route_data | The route data for network 01 that was passed into the network module |
2019
| network_01_routes | The routes associated with network-01 |
2120
| network_01_self_link | The URI of the VPC network-01 |
2221
| network_01_subnets | The names of the subnets being created on network-01 |
@@ -26,7 +25,6 @@ This example configures a host network project with two separate networks.
2625
| network_01_subnets_regions | The region where the subnets will be created on network-01 |
2726
| network_01_subnets_secondary_ranges | The secondary ranges associated with these subnets on network-01 |
2827
| network_02_name | The name of the VPC network-02 |
29-
| network_02_route_data | The route data for network 02 that was passed into the network module |
3028
| network_02_routes | The routes associated with network-02 |
3129
| network_02_self_link | The URI of the VPC network-02 |
3230
| network_02_subnets | The names of the subnets being created on network-02 |

main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,21 @@ resource "google_compute_route" "route" {
8181
"google_compute_subnetwork.subnetwork",
8282
]
8383
}
84+
85+
resource "null_resource" "delete_default_internet_gateway_routes" {
86+
count = "${var.delete_default_internet_gateway_routes ? 1 : 0}"
87+
88+
provisioner "local-exec" {
89+
command = "${path.module}/scripts/delete-default-gateway-routes.sh ${var.project_id} ${var.network_name}"
90+
}
91+
92+
triggers {
93+
number_of_routes = "${length(var.routes)}"
94+
}
95+
96+
depends_on = [
97+
"google_compute_network.network",
98+
"google_compute_subnetwork.subnetwork",
99+
"google_compute_route.route",
100+
]
101+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
# Copyright 2018 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
17+
set -e
18+
19+
PROJECT_ID=$1
20+
NETWORK_ID=$2
21+
FILTERED_ROUTES=$(gcloud compute routes list \
22+
--project="${PROJECT_ID}" \
23+
--format="value(name)" \
24+
--filter=" \
25+
nextHopGateway:(https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/global/gateways/default-internet-gateway) \
26+
AND network:(https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/global/networks/${NETWORK_ID}) \
27+
AND name~^default-route \
28+
"
29+
)
30+
31+
function delete_internet_gateway_routes {
32+
local routes="${1}"
33+
echo "${routes}" | while read -r line; do
34+
echo "Deleting route ${line}..."
35+
gcloud compute routes delete "${line}" --quiet --project="${PROJECT_ID}"
36+
done
37+
}
38+
39+
40+
if [ -n "${FILTERED_ROUTES}" ]; then
41+
delete_internet_gateway_routes "${FILTERED_ROUTES}"
42+
else
43+
echo "Default internet gateway route(s) not found; exiting..."
44+
fi
45+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module "example" {
18+
source = "../../../examples/delete_default_gateway_routes"
19+
project_id = "${var.project_id}"
20+
}

0 commit comments

Comments
 (0)