Skip to content

Commit 227c642

Browse files
authored
Merge pull request #26 from walterdolce/feature/add-ability-to-specify-routing-mode
Add ability to choose network routing mode
2 parents 2e0ba9e + 84b3a05 commit 227c642

File tree

11 files changed

+329
-5
lines changed

11 files changed

+329
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module "vpc" {
1818
1919
project_id = "<PROJECT ID>"
2020
network_name = "example-vpc"
21+
routing_mode = "GLOBAL"
2122
2223
subnets = [
2324
{
@@ -80,6 +81,7 @@ Then perform the following commands on the root folder:
8081
| Name | Description | Type | Default | Required |
8182
|------|-------------|:----:|:-----:|:-----:|
8283
| network_name | The name of the network being created | string | - | yes |
84+
| routing_mode | The network routing mode. Can be either 'REGIONAL' or 'GLOBAL' | string | GLOBAL | no |
8385
| project_id | The ID of the project where this VPC will be created | string | - | yes |
8486
| shared_vpc_host | Makes this project a Shared VPC host if 'true' (default 'false') | string | `false` | no |
8587
| subnets | The list of subnets being created | list | - | yes |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Simple Project
2+
3+
This example configures a single simple regional VPC inside of a project.
4+
5+
This VPC has two subnets, with no secondary ranges.
6+
7+
[^]: (autogen_docs_start)
8+
9+
## Inputs
10+
11+
| Name | Description | Type | Default | Required |
12+
|------|-------------|:----:|:-----:|:-----:|
13+
| project\_id | The project ID to host the network in | string | - | yes |
14+
15+
## Outputs
16+
17+
| Name | Description |
18+
|------|-------------|
19+
| network\_name | The name of the VPC being created |
20+
| network\_self\_link | The URI of the VPC being created |
21+
| routes | The routes associated with this VPC |
22+
| subnets\_flow\_logs | Whether the subnets will have VPC flow logs enabled |
23+
| subnets\_ips | The IP and cidrs of the subnets being created |
24+
| subnets\_names | The names of the subnets being created |
25+
| subnets\_private\_access | Whether the subnets will have access to Google API's without a public IP |
26+
| subnets\_regions | The region where subnets will be created |
27+
| subnets\_secondary\_ranges | The secondary ranges associated with these subnets |
28+
29+
[^]: (autogen_docs_end)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 "test-vpc-module" {
18+
source = "../../"
19+
project_id = "${var.project_id}"
20+
network_name = "test-vpc-module"
21+
routing_mode = "REGIONAL"
22+
23+
subnets = [
24+
{
25+
subnet_name = "subnet-01"
26+
subnet_ip = "10.10.10.0/24"
27+
subnet_region = "us-west1"
28+
},
29+
{
30+
subnet_name = "subnet-02"
31+
subnet_ip = "10.10.20.0/24"
32+
subnet_region = "us-west1"
33+
subnet_private_access = "true"
34+
subnet_flow_logs = "true"
35+
},
36+
]
37+
38+
secondary_ranges = {
39+
subnet-01 = []
40+
subnet-02 = []
41+
}
42+
}
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+
}

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
resource "google_compute_network" "network" {
2121
name = "${var.network_name}"
2222
auto_create_subnetworks = "false"
23-
routing_mode = "GLOBAL"
23+
routing_mode = "${var.routing_mode}"
2424
project = "${var.project_id}"
2525
}
2626

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#!/usr/bin/env bats
2+
3+
# Copyright 2018 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# #################################### #
18+
# Terraform tests #
19+
# #################################### #
20+
21+
@test "Ensure that Terraform configures the dirs and download the plugins" {
22+
23+
run terraform init
24+
[ "$status" -eq 0 ]
25+
}
26+
27+
@test "Ensure that Terraform updates the plugins" {
28+
run terraform get
29+
[ "$status" -eq 0 ]
30+
}
31+
32+
@test "Terraform plan, ensure connection and creation of resources" {
33+
34+
run terraform plan
35+
[ "$status" -eq 0 ]
36+
[[ "$output" =~ 7\ to\ add ]]
37+
[[ "$output" =~ 0\ to\ change ]]
38+
[[ "$output" =~ 0\ to\ destroy ]]
39+
}
40+
41+
@test "Terraform apply" {
42+
43+
run terraform apply -auto-approve
44+
[ "$status" -eq 0 ]
45+
[[ "$output" =~ 7\ added ]]
46+
[[ "$output" =~ 0\ changed ]]
47+
[[ "$output" =~ 0\ destroyed ]]
48+
}
49+
50+
# #################################### #
51+
# gcloud tests #
52+
# #################################### #
53+
54+
@test "Test that the network was created with the correct settings" {
55+
56+
NETWORK_NAME="$(terraform output network_name)"
57+
58+
run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(autoCreateSubnetworks)'[no-pad]
59+
[ "$status" -eq 0 ]
60+
[[ "${lines[0]}" = "False" ]]
61+
62+
run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(routingConfig.routingMode)'[no-pad]
63+
[ "$status" -eq 0 ]
64+
[[ "${lines[0]}" = "REGIONAL" ]]
65+
66+
}
67+
68+
@test "Test that the subnets were created with the correct settings" {
69+
70+
NETWORK_NAME="$(terraform output network_name)"
71+
72+
# test-network-01-subnet-01
73+
SUBNET_NAME="$(terraform output -json subnets_names | jq '.value[0]' -r)"
74+
SUBNET_REGION="$(terraform output -json subnets_regions | jq '.value[0]' -r)"
75+
SUBNET_IP="$(terraform output -json subnets_ips | jq '.value[0]' -r)"
76+
SUBNET_SECONDARY_RANGE_IP="$(terraform output -json subnets_secondary_ranges | jq '.value[0] | .[0].ip_cidr_range' -r)"
77+
SUBNET_SECONDARY_RANGE_NAME="$(terraform output -json subnets_secondary_ranges | jq '.value[0] | .[0].range_name' -r)"
78+
79+
run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(subnetworks[0])'[no-pad]
80+
[ "$status" -eq 0 ]
81+
[[ "${lines[0]}" = "https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/regions/${SUBNET_REGION}/subnetworks/${SUBNET_NAME}" ]]
82+
83+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(ipCidrRange)'[no-pad]
84+
[ "$status" -eq 0 ]
85+
[[ "${lines[0]}" = "${SUBNET_IP}" ]]
86+
87+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(privateIpGoogleAccess)'[no-pad]
88+
[ "$status" -eq 0 ]
89+
[[ "${lines[0]}" = "False" ]]
90+
91+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(secondaryIpRanges[0].ipCidrRange)'[no-pad]
92+
[ "$status" -eq 0 ]
93+
[[ "${lines[0]}" = "${SUBNET_SECONDARY_RANGE_IP}" ]]
94+
95+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(secondaryIpRanges[0].rangeName)'[no-pad]
96+
[ "$status" -eq 0 ]
97+
[[ "${lines[0]}" = "${SUBNET_SECONDARY_RANGE_NAME}" ]]
98+
99+
# test-network-01-subnet-02
100+
SUBNET_NAME="$(terraform output -json subnets_names | jq '.value[1]' -r)"
101+
SUBNET_REGION="$(terraform output -json subnets_regions | jq '.value[1]' -r)"
102+
SUBNET_IP="$(terraform output -json subnets_ips | jq '.value[1]' -r)"
103+
SUBNET_SECONDARY_RANGE_IP="$(terraform output -json subnets_secondary_ranges | jq '.value[1] | .[0].ip_cidr_range' -r)"
104+
SUBNET_SECONDARY_RANGE_NAME="$(terraform output -json subnets_secondary_ranges | jq '.value[1] | .[0].range_name' -r)"
105+
106+
run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(subnetworks[1])'[no-pad]
107+
[ "$status" -eq 0 ]
108+
[[ "${lines[0]}" = "https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/regions/${SUBNET_REGION}/subnetworks/${SUBNET_NAME}" ]]
109+
110+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(ipCidrRange)'[no-pad]
111+
[ "$status" -eq 0 ]
112+
[[ "${lines[0]}" = "${SUBNET_IP}" ]]
113+
114+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(privateIpGoogleAccess)'[no-pad]
115+
[ "$status" -eq 0 ]
116+
[[ "${lines[0]}" = "False" ]]
117+
118+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(secondaryIpRanges[0].ipCidrRange)'[no-pad]
119+
[ "$status" -eq 0 ]
120+
[[ "${lines[0]}" = "${SUBNET_SECONDARY_RANGE_IP}" ]]
121+
122+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(secondaryIpRanges[0].rangeName)'[no-pad]
123+
[ "$status" -eq 0 ]
124+
[[ "${lines[0]}" = "${SUBNET_SECONDARY_RANGE_NAME}" ]]
125+
126+
# test-network-01-subnet-03
127+
SUBNET_NAME="$(terraform output -json subnets_names | jq '.value[2]' -r)"
128+
SUBNET_REGION="$(terraform output -json subnets_regions | jq '.value[2]' -r)"
129+
SUBNET_IP="$(terraform output -json subnets_ips | jq '.value[2]' -r)"
130+
131+
run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(subnetworks[2])'[no-pad]
132+
[ "$status" -eq 0 ]
133+
[[ "${lines[0]}" = "https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/regions/${SUBNET_REGION}/subnetworks/${SUBNET_NAME}" ]]
134+
135+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(ipCidrRange)'[no-pad]
136+
[ "$status" -eq 0 ]
137+
[[ "${lines[0]}" = "${SUBNET_IP}" ]]
138+
139+
run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(privateIpGoogleAccess)'[no-pad]
140+
[ "$status" -eq 0 ]
141+
[[ "${lines[0]}" = "False" ]]
142+
143+
}
144+
145+
# #################################### #
146+
# Terraform destroy test #
147+
# #################################### #
148+
149+
@test "Terraform destroy" {
150+
run terraform destroy -force
151+
[ "$status" -eq 0 ]
152+
[[ "$output" =~ 7\ destroyed ]]
153+
}

test/integration/gcloud-test/launch.sh

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ function clean_workdir() {
3838

3939
# Creates the main.tf file for Terraform
4040
function create_main_tf_file() {
41-
echo "Creating main.tf file"
41+
local ROUTING_MODE=$1
42+
echo "Creating main.tf file (for a ${ROUTING_MODE} network)"
4243
touch main.tf
4344
eval "cat <<EOF
4445
$(<templates/main.tf.tmpl)
@@ -54,14 +55,25 @@ $(<templates/outputs.tf.tmpl)
5455
EOF" | tee > outputs.tf
5556
}
5657

58+
## Testing Creation of Global Network
5759
# Preparing environment
5860
clean_workdir
59-
create_main_tf_file
61+
create_main_tf_file "GLOBAL"
6062
create_outputs_file
6163

6264
# Call to bats
63-
echo "Test to execute: $(bats integration.bats -c)"
64-
bats integration.bats
65+
echo "Test to execute: $(bats integration.global_network.bats -c)"
66+
bats integration.global_network.bats
67+
68+
## Testing Creation of Regional Network
69+
# Preparing environment
70+
clean_workdir
71+
create_main_tf_file "REGIONAL"
72+
create_outputs_file
73+
74+
# Call to bats
75+
echo "Test to execute: $(bats integration.regional_network.bats -c)"
76+
bats integration.regional_network.bats
6577

6678
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=""
6779
unset CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE

test/integration/gcloud-test/templates/main.tf.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module "test-vpc-module-01" {
2626
source = "../../../"
2727
project_id = "${PROJECT_ID}"
2828
network_name = "test-network-01"
29+
routing_mode = "${ROUTING_MODE}"
2930

3031
subnets = [
3132
{

0 commit comments

Comments
 (0)