generated from equinix-labs/terraform-equinix-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
147 lines (117 loc) · 5.17 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
locals {
gcp_compute_router_name = coalesce(var.gcp_compute_router_name, lower(format("router-%s", random_string.this.result)))
gcp_compute_router_id = var.gcp_compute_create_router ? google_compute_router.this[0].id : data.google_compute_router.this[0].id
gcp_region = coalesce(var.gcp_region, data.google_client_config.this.region)
gcp_project = coalesce(var.gcp_project, data.google_client_config.this.project)
gcp_network = data.google_compute_network.this.id
gcp_bgp_addresses = try(jsondecode(data.local_file.this[0].content), null)
gcp_bgp_addresses_file_path = "${path.module}/gcp_peering_addresses.json"
}
data "google_client_config" "this" {}
data "google_compute_network" "this" {
name = var.gcp_compute_network_name
}
data "google_compute_router" "this" {
count = var.gcp_compute_create_router ? 0 : 1
name = local.gcp_compute_router_name
network = local.gcp_network
}
resource "google_compute_router" "this" {
count = var.gcp_compute_create_router ? 1 : 0
name = local.gcp_compute_router_name
network = local.gcp_network
region = local.gcp_region
bgp {
asn = 16550
advertise_mode = "CUSTOM"
advertised_groups = ["ALL_SUBNETS"]
}
}
resource "random_string" "this" {
length = 3
special = false
}
resource "google_compute_interconnect_attachment" "this" {
name = coalesce(var.gcp_compute_interconnect_name, lower(format("interconnect-%s", random_string.this.result)))
type = "PARTNER"
router = local.gcp_compute_router_id
region = local.gcp_region
edge_availability_domain = format("AVAILABILITY_DOMAIN_%d", var.gcp_availability_domain)
}
module "equinix-fabric-connection" {
source = "equinix-labs/fabric-connection/equinix"
version = "0.6.0"
# required variables
notification_users = var.fabric_notification_users
# optional variables
name = var.fabric_connection_name
seller_profile_name = format("Google Cloud Partner Interconnect Zone %d", var.gcp_availability_domain)
seller_metro_code = var.fabric_destination_metro_code
seller_metro_name = var.fabric_destination_metro_name
seller_region = local.gcp_region
seller_authorization_key = google_compute_interconnect_attachment.this.pairing_key
network_edge_id = var.network_edge_device_id
network_edge_interface_id = var.network_edge_device_interface_id
port_name = var.fabric_port_name
vlan_stag = var.fabric_vlan_stag
service_token_id = var.fabric_service_token_id
speed = var.fabric_speed
purchase_order_number = var.fabric_purchase_order_number
}
module "gcloud-configure-bgp" {
source = "terraform-google-modules/gcloud/google"
version = "3.4.0"
enabled = anytrue([var.gcp_configure_bgp, var.network_edge_configure_bgp])
skip_download = var.gcp_gcloud_skip_download
platform = var.platform
upgrade = false
create_cmd_body = join(" ", ["compute routers update-bgp-peer ${local.gcp_compute_router_id}",
"--peer-name=$(gcloud compute routers describe ${local.gcp_compute_router_id} --region=${local.gcp_region} --project=${local.gcp_project} --format=\"value(bgpPeers.name)\")",
"--peer-asn=${var.gcp_interconnect_customer_asn}",
"--advertisement-mode=CUSTOM",
"--set-advertisement-groups=ALL_SUBNETS",
"--region=${local.gcp_region}",
"--project=${local.gcp_project}"])
module_depends_on = [
module.equinix-fabric-connection,
]
}
module "gcloud-get-bgp-addresses" {
source = "terraform-google-modules/gcloud/google"
version = "3.4.0"
enabled = anytrue([var.gcp_configure_bgp, var.network_edge_configure_bgp])
platform = var.platform
skip_download = true
upgrade = false
create_cmd_entrypoint = "${path.module}/scripts/gcp_get_bgp_addresses.sh"
create_cmd_body = join(" ", [
google_compute_interconnect_attachment.this.name,
local.gcp_region,
local.gcp_project,
local.gcp_bgp_addresses_file_path,
var.gcp_gcloud_skip_download ? "gcloud" : abspath("${module.gcloud-configure-bgp.bin_dir}/gcloud"),
var.gcp_gcloud_skip_download ? "jq" : abspath("${module.gcloud-configure-bgp.bin_dir}/jq")])
destroy_cmd_entrypoint = "echo"
destroy_cmd_body = "'# This file is auto-generated by equinix-labs/fabric-connection-gcp/equinix terraform module.' > ${local.gcp_bgp_addresses_file_path}"
module_depends_on = [
module.gcloud-configure-bgp.wait,
]
}
data "local_file" "this" {
count = anytrue([var.gcp_configure_bgp, var.network_edge_configure_bgp]) ? 1 : 0
filename = local.gcp_bgp_addresses_file_path
depends_on = [
module.gcloud-get-bgp-addresses.wait,
]
}
resource "equinix_network_bgp" "this" {
count = alltrue([var.network_edge_device_id != "", var.network_edge_configure_bgp]) ? 1 : 0
connection_id = module.equinix-fabric-connection.primary_connection.uuid
local_ip_address = local.gcp_bgp_addresses.customer_router_ip
local_asn = tonumber(var.gcp_interconnect_customer_asn)
remote_ip_address = split("/", local.gcp_bgp_addresses.cloud_router_ip)[0]
remote_asn = 16550
depends_on = [
module.gcloud-get-bgp-addresses.wait,
]
}