-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide ability to delete default gateway route
This commit introduces the ability to delete the default gateway route that is created for the VPC network (issue terraform-google-modules#25). If the input variable `var.delete_default_internet_gateway_routes` is set then a null_resource uses the local-exec provisioner to execute a script that filters for all network routes within the `project_id` whose name begins with "default-route" and who contains a next hop of "default-internet-gateway" and then deletes them. This functionality is useful in the event that all egress traffic should be routed through a single device instead of directly to the default internet gateway. Without this change there is no way to automate the deletion of those routes.
- Loading branch information
Showing
16 changed files
with
385 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Delete Default Gateway Routes | ||
|
||
This example configures a single simple VPC inside of a project. | ||
|
||
This VPC has a single subnet with no secondary ranges, and ensures the default internet gateway route is deleted. | ||
|
||
[^]: (autogen_docs_start) | ||
|
||
|
||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|:----:|:-----:|:-----:| | ||
| project_id | The project ID to host the network in | string | - | yes | | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| network_name | The name of the VPC being created | | ||
| network_self_link | The URI of the VPC being created | | ||
| routes | The routes associated with this VPC | | ||
| subnets_flow_logs | Whether the subnets will have VPC flow logs enabled | | ||
| subnets_ips | The IP and cidrs of the subnets being created | | ||
| subnets_names | The names of the subnets being created | | ||
| subnets_private_access | Whether the subnets will have access to Google API's without a public IP | | ||
| subnets_regions | The region where subnets will be created | | ||
| subnets_secondary_ranges | The secondary ranges associated with these subnets | | ||
|
||
[^]: (autogen_docs_end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
locals { | ||
network_name = "test-network-${random_string.random_suffix.result}" | ||
} | ||
|
||
resource "random_string" "random_suffix" { | ||
length = 4 | ||
upper = "false" | ||
special = "false" | ||
} | ||
|
||
module "test-vpc-module" { | ||
source = "../../" | ||
project_id = "${var.project_id}" | ||
network_name = "${local.network_name}" | ||
delete_default_internet_gateway_routes = "true" | ||
|
||
subnets = [ | ||
{ | ||
subnet_name = "subnet-41" | ||
subnet_ip = "10.20.30.0/24" | ||
subnet_region = "us-west1" | ||
}, | ||
] | ||
|
||
secondary_ranges = { | ||
subnet-41 = [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
output "network_name" { | ||
value = "${module.test-vpc-module.network_name}" | ||
description = "The name of the VPC being created" | ||
} | ||
|
||
output "network_self_link" { | ||
value = "${module.test-vpc-module.network_self_link}" | ||
description = "The URI of the VPC being created" | ||
} | ||
|
||
output "subnets_names" { | ||
value = "${module.test-vpc-module.subnets_names}" | ||
description = "The names of the subnets being created" | ||
} | ||
|
||
output "subnets_ips" { | ||
value = "${module.test-vpc-module.subnets_ips}" | ||
description = "The IP and cidrs of the subnets being created" | ||
} | ||
|
||
output "subnets_regions" { | ||
value = "${module.test-vpc-module.subnets_regions}" | ||
description = "The region where subnets will be created" | ||
} | ||
|
||
output "subnets_private_access" { | ||
value = "${module.test-vpc-module.subnets_private_access}" | ||
description = "Whether the subnets will have access to Google API's without a public IP" | ||
} | ||
|
||
output "subnets_flow_logs" { | ||
value = "${module.test-vpc-module.subnets_flow_logs}" | ||
description = "Whether the subnets will have VPC flow logs enabled" | ||
} | ||
|
||
output "subnets_secondary_ranges" { | ||
value = "${module.test-vpc-module.subnets_secondary_ranges}" | ||
description = "The secondary ranges associated with these subnets" | ||
} | ||
|
||
output "routes" { | ||
value = "${module.test-vpc-module.routes}" | ||
description = "The routes associated with this VPC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
variable "project_id" { | ||
description = "The project ID to host the network in" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/bin/bash | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
set -e | ||
|
||
PROJECT_ID=$1 | ||
NETWORK_ID=$2 | ||
FILTERED_ROUTES=$(gcloud compute routes list \ | ||
--project="${PROJECT_ID}" \ | ||
--format="value(name)" \ | ||
--filter=" \ | ||
nextHopGateway:https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/global/gateways/default-internet-gateway \ | ||
AND network:https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/global/networks/${NETWORK_ID} \ | ||
" | ||
) | ||
|
||
function delete_internet_gateway_routes { | ||
local routes="${1}" | ||
echo "${routes}" | while read -r line; do | ||
if [[ "${line}" =~ ^default-route ]]; then | ||
echo "Deleting route ${line}..." | ||
gcloud compute routes delete "${line}" --quiet --project="${PROJECT_ID}" | ||
fi | ||
done | ||
} | ||
|
||
|
||
if [ -n "${FILTERED_ROUTES}" ]; then | ||
delete_internet_gateway_routes "${FILTERED_ROUTES}" | ||
else | ||
echo "Default internet gateway route(s) not found; exiting..." | ||
fi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
module "example" { | ||
source = "../../../examples/delete_default_gateway_routes" | ||
project_id = "${var.project_id}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
output "project_id" { | ||
value = "${var.project_id}" | ||
description = "The ID of the project being used" | ||
} | ||
|
||
output "network_name" { | ||
value = "${module.example.network_name}" | ||
description = "The name of the VPC being created" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
# This fixture defines a default internet gateway route that DOESN'T start | ||
# with 'default-route' to test the behavior of the script that deletes | ||
# the default internet gateway routes. | ||
|
||
resource "google_compute_route" "alternative_gateway" { | ||
project = "${var.project_id}" | ||
network = "${module.example.network_name}" | ||
|
||
name = "alternative-gateway-route" | ||
description = "Alternative gateway route" | ||
dest_range = "0.0.0.0/0" | ||
tags = ["egress-inet"] | ||
next_hop_gateway = "default-internet-gateway" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
variable "project_id" { | ||
description = "The GCP project to use for integration tests" | ||
} |
Oops, something went wrong.