Skip to content

Commit 3f06a23

Browse files
authored
Merge pull request #89 from ideasculptor/master
Make network creation optional in root module
2 parents 0f84447 + ec1aac9 commit 3f06a23

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ docker_test_prepare:
4242

4343
# Clean up test environment within the docker container
4444
.PHONY: docker_test_cleanup
45-
docker_test_prepare:
45+
docker_test_cleanup:
4646
docker run --rm -it \
4747
-e SERVICE_ACCOUNT_JSON \
4848
-e TF_VAR_org_id \

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Then perform the following commands on the root folder:
8484
| Name | Description | Type | Default | Required |
8585
|------|-------------|:----:|:-----:|:-----:|
8686
| auto\_create\_subnetworks | When set to true, the network is created in 'auto subnet mode' and it will create a subnet for each region automatically across the 10.128.0.0/9 address range. When set to false, the network is created in 'custom subnet mode' so the user can explicitly connect subnetwork resources. | bool | `"false"` | no |
87+
| create\_network | Specify whether to create a new network or just assume it already exists. | string | `"true"` | no |
8788
| 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 |
8889
| description | An optional description of this resource. The resource must be recreated to modify this field. | string | `""` | no |
8990
| network\_name | The name of the network being created | string | n/a | yes |

main.tf

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@
1818
VPC configuration
1919
*****************************************/
2020
resource "google_compute_network" "network" {
21+
count = var.create_network == "true" ? 1 : 0
2122
name = var.network_name
2223
auto_create_subnetworks = var.auto_create_subnetworks
2324
routing_mode = var.routing_mode
2425
project = var.project_id
2526
description = var.description
2627
}
2728

29+
data "google_compute_network" "network" {
30+
name = var.network_name
31+
project = var.project_id
32+
depends_on = [google_compute_network.network]
33+
}
34+
2835
/******************************************
2936
Shared VPC
3037
*****************************************/
3138
resource "google_compute_shared_vpc_host_project" "shared_vpc_host" {
32-
count = var.shared_vpc_host == "true" ? 1 : 0
33-
project = var.project_id
34-
depends_on = [google_compute_network.network]
39+
count = var.shared_vpc_host == "true" ? 1 : 0
40+
project = data.google_compute_network.network.project
3541
}
3642

3743
/******************************************
@@ -45,10 +51,11 @@ resource "google_compute_subnetwork" "subnetwork" {
4551
region = var.subnets[count.index]["subnet_region"]
4652
private_ip_google_access = lookup(var.subnets[count.index], "subnet_private_access", "false")
4753
enable_flow_logs = lookup(var.subnets[count.index], "subnet_flow_logs", "false")
48-
network = google_compute_network.network.name
54+
network = data.google_compute_network.network.name
4955
project = var.project_id
5056
secondary_ip_range = [for i in range(length(contains(keys(var.secondary_ranges), var.subnets[count.index]["subnet_name"]) == true ? var.secondary_ranges[var.subnets[count.index]["subnet_name"]] : [])) : var.secondary_ranges[var.subnets[count.index]["subnet_name"]][i]]
5157
description = lookup(var.subnets[count.index], "description", null)
58+
depends_on = [google_compute_network.network]
5259
}
5360

5461
data "google_compute_subnetwork" "created_subnets" {
@@ -65,7 +72,7 @@ data "google_compute_subnetwork" "created_subnets" {
6572
resource "google_compute_route" "route" {
6673
count = length(var.routes)
6774
project = var.project_id
68-
network = var.network_name
75+
network = data.google_compute_network.network.name
6976
name = lookup(var.routes[count.index], "name", format("%s-%s-%d", lower(var.network_name), "route", count.index))
7077
description = lookup(var.routes[count.index], "description", "")
7178
tags = compact(split(",", lookup(var.routes[count.index], "tags", "")))
@@ -78,7 +85,6 @@ resource "google_compute_route" "route" {
7885
priority = lookup(var.routes[count.index], "priority", "1000")
7986

8087
depends_on = [
81-
google_compute_network.network,
8288
google_compute_subnetwork.subnetwork,
8389
]
8490
}

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616

1717
output "network_name" {
18-
value = google_compute_network.network.name
18+
value = data.google_compute_network.network.name
1919
description = "The name of the VPC being created"
2020
}
2121

2222
output "network_self_link" {
23-
value = google_compute_network.network.self_link
23+
value = data.google_compute_network.network.self_link
2424
description = "The URI of the VPC being created"
2525
}
2626

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ variable "project_id" {
1818
description = "The ID of the project where this VPC will be created"
1919
}
2020

21+
variable "create_network" {
22+
type = string
23+
default = "true"
24+
description = "Specify whether to create a new network or just assume it already exists."
25+
}
26+
2127
variable "network_name" {
2228
description = "The name of the network being created"
2329
}

0 commit comments

Comments
 (0)