Skip to content

Commit 0093763

Browse files
Fixed datastream connection profile test (#12881) (#904)
[upstream:7cef963f2096ac835195a31728dcb22b457ff5d8] Signed-off-by: Modular Magician <[email protected]>
1 parent c1d6f64 commit 0093763

File tree

1 file changed

+94
-49
lines changed
  • datastream_connection_profile_postgresql_private_connection

1 file changed

+94
-49
lines changed
Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
resource "google_datastream_private_connection" "private_connection" {
2-
display_name = "Connection profile"
3-
location = "us-central1"
4-
private_connection_id = "my-connection-${local.name_suffix}"
5-
6-
labels = {
7-
key = "value"
8-
}
9-
10-
vpc_peering_config {
11-
vpc = google_compute_network.default.id
12-
subnet = "10.0.0.0/29"
13-
}
1+
resource "google_compute_network" "default" {
2+
name = "my-network-${local.name_suffix}"
3+
auto_create_subnetworks = false
144
}
155

16-
resource "google_compute_network" "default" {
17-
name = "my-network-${local.name_suffix}"
6+
resource "google_compute_subnetwork" "default" {
7+
name = "my-subnetwork-${local.name_suffix}"
8+
ip_cidr_range = "10.1.0.0/16"
9+
region = "us-central1"
10+
network = google_compute_network.default.id
11+
}
12+
13+
resource "google_datastream_private_connection" "private_connection" {
14+
display_name = "Private connection"
15+
location = "us-central1"
16+
private_connection_id = "my-connection-${local.name_suffix}"
17+
18+
vpc_peering_config {
19+
vpc = google_compute_network.default.id
20+
subnet = "10.0.0.0/29"
21+
}
1822
}
1923

2024
resource "google_sql_database_instance" "instance" {
@@ -23,28 +27,9 @@ resource "google_sql_database_instance" "instance" {
2327
region = "us-central1"
2428
settings {
2529
tier = "db-f1-micro"
26-
2730
ip_configuration {
28-
29-
// Datastream IPs will vary by region.
30-
authorized_networks {
31-
value = "34.71.242.81"
32-
}
33-
34-
authorized_networks {
35-
value = "34.72.28.29"
36-
}
37-
38-
authorized_networks {
39-
value = "34.67.6.157"
40-
}
41-
4231
authorized_networks {
43-
value = "34.67.234.134"
44-
}
45-
46-
authorized_networks {
47-
value = "34.72.239.218"
32+
value = google_compute_address.nat_vm_ip.address
4833
}
4934
}
5035
}
@@ -68,19 +53,79 @@ resource "google_sql_user" "user" {
6853
password = random_password.pwd.result
6954
}
7055

56+
resource "google_compute_address" "nat_vm_ip" {
57+
name = "nat-vm-ip-${local.name_suffix}"
58+
}
59+
60+
resource "google_compute_instance" "nat_vm" {
61+
name = "nat-vm-${local.name_suffix}"
62+
machine_type = "e2-medium"
63+
zone = "us-central1-a"
64+
desired_status = "RUNNING"
65+
66+
boot_disk {
67+
initialize_params {
68+
image = "debian-cloud/debian-12"
69+
}
70+
}
71+
72+
network_interface {
73+
network = google_datastream_private_connection.private_connection.vpc_peering_config.0.vpc
74+
subnetwork = google_compute_subnetwork.default.self_link
75+
access_config {
76+
nat_ip = google_compute_address.nat_vm_ip.address
77+
}
78+
}
79+
80+
metadata_startup_script = <<EOT
81+
#! /bin/bash
82+
# See https://cloud.google.com/datastream/docs/private-connectivity#set-up-reverse-proxy
83+
export DB_ADDR=${google_sql_database_instance.instance.public_ip_address}
84+
export DB_PORT=5432
85+
echo 1 > /proc/sys/net/ipv4/ip_forward
86+
md_url_prefix="http://169.254.169.254/computeMetadata/v1/instance"
87+
vm_nic_ip="$(curl -H "Metadata-Flavor: Google" $${md_url_prefix}/network-interfaces/0/ip)"
88+
iptables -t nat -F
89+
iptables -t nat -A PREROUTING \
90+
-p tcp --dport $DB_PORT \
91+
-j DNAT \
92+
--to-destination $DB_ADDR
93+
iptables -t nat -A POSTROUTING \
94+
-p tcp --dport $DB_PORT \
95+
-j SNAT \
96+
--to-source $vm_nic_ip
97+
iptables-save
98+
EOT
99+
}
100+
101+
resource "google_compute_firewall" "rules" {
102+
name = "ingress-rule-${local.name_suffix}"
103+
network = google_datastream_private_connection.private_connection.vpc_peering_config.0.vpc
104+
description = "Allow traffic into NAT VM"
105+
direction = "INGRESS"
106+
107+
allow {
108+
protocol = "tcp"
109+
ports = ["5432"]
110+
}
111+
112+
source_ranges = [google_datastream_private_connection.private_connection.vpc_peering_config.0.subnet]
113+
}
114+
71115
resource "google_datastream_connection_profile" "default" {
72-
display_name = "Connection profile"
73-
location = "us-central1"
74-
connection_profile_id = "my-profile-${local.name_suffix}"
75-
76-
postgresql_profile {
77-
hostname = google_sql_database_instance.instance.public_ip_address
78-
username = google_sql_user.user.name
79-
password = google_sql_user.user.password
80-
database = google_sql_database.db.name
81-
}
82-
83-
private_connectivity {
84-
private_connection = google_datastream_private_connection.private_connection.id
85-
}
116+
display_name = "Connection profile"
117+
location = "us-central1"
118+
connection_profile_id = "my-profile-${local.name_suffix}"
119+
120+
postgresql_profile {
121+
hostname = google_compute_instance.nat_vm.network_interface.0.network_ip
122+
username = google_sql_user.user.name
123+
password = google_sql_user.user.password
124+
database = google_sql_database.db.name
125+
port = 5432
126+
}
127+
128+
private_connectivity {
129+
private_connection = google_datastream_private_connection.private_connection.id
130+
}
86131
}

0 commit comments

Comments
 (0)