Skip to content

Commit 22c3eda

Browse files
committed
feat(cloud_sql): Add cloud sql psa+psc connectivity samples
1 parent 2ace15d commit 22c3eda

File tree

9 files changed

+409
-1
lines changed

9 files changed

+409
-1
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Copyright 2022 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+
# [START cloud_sql_mysql_instance_psa_psc]
18+
19+
# [START vpc_mysql_instance_private_ip_network]
20+
resource "google_compute_network" "peering_network" {
21+
name = "private-network"
22+
auto_create_subnetworks = "false"
23+
}
24+
# [END vpc_mysql_instance_private_ip_network]
25+
26+
# [START vpc_mysql_instance_private_ip_address]
27+
resource "google_compute_global_address" "private_ip_address" {
28+
name = "private-ip-address"
29+
purpose = "VPC_PEERING"
30+
address_type = "INTERNAL"
31+
prefix_length = 16
32+
network = google_compute_network.peering_network.id
33+
}
34+
# [END vpc_mysql_instance_private_ip_address]
35+
36+
# [START vpc_mysql_instance_private_ip_service_connection]
37+
resource "google_service_networking_connection" "default" {
38+
network = google_compute_network.peering_network.id
39+
service = "servicenetworking.googleapis.com"
40+
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
41+
}
42+
# [END vpc_mysql_instance_private_ip_service_connection]
43+
44+
# [START cloud_sql_mysql_instance_psa_psc_instance]
45+
resource "google_sql_database_instance" "default" {
46+
name = "mysql-instance"
47+
region = "us-central1"
48+
database_version = "MYSQL_8_0"
49+
50+
depends_on = [google_service_networking_connection.default]
51+
52+
settings {
53+
tier = "db-f1-micro"
54+
ip_configuration {
55+
psc_config {
56+
psc_enabled = true
57+
allowed_consumer_projects = [] # Add consumer project IDs here.
58+
}
59+
ipv4_enabled = false
60+
private_network = google_compute_network.peering_network.id
61+
}
62+
}
63+
# set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by
64+
# use of Terraform whereas `deletion_protection_enabled` flag protects this instance at the GCP level.
65+
deletion_protection = false
66+
}
67+
# [END cloud_sql_mysql_instance_psa_psc_instance]
68+
69+
# [START cloud_sql_mysql_instance_private_ip_routes]
70+
resource "google_compute_network_peering_routes_config" "peering_routes" {
71+
peering = google_service_networking_connection.default.peering
72+
network = google_compute_network.peering_network.name
73+
import_custom_routes = true
74+
export_custom_routes = true
75+
}
76+
# [END cloud_sql_mysql_instance_private_ip_routes]
77+
78+
# [START cloud_sql_mysql_instance_psc_endpoint]
79+
resource "google_compute_address" "default" {
80+
name = "psc-compute-address-${google_sql_database_instance.default.name}"
81+
region = "us-central1"
82+
address_type = "INTERNAL"
83+
subnetwork = "default" # Replace value with the name of the subnet here.
84+
address = "10.128.0.43" # Replace value with the IP address to reserve.
85+
}
86+
87+
data "google_sql_database_instance" "default" {
88+
name = resource.google_sql_database_instance.default.name
89+
}
90+
91+
resource "google_compute_forwarding_rule" "default" {
92+
name = "psc-forwarding-rule-${google_sql_database_instance.default.name}"
93+
region = "us-central1"
94+
network = "default"
95+
ip_address = google_compute_address.default.self_link
96+
load_balancing_scheme = ""
97+
target = data.google_sql_database_instance.default.psc_service_attachment_link
98+
}
99+
# [END cloud_sql_mysql_instance_psc_endpoint]
100+
101+
# [END cloud_sql_mysql_instance_psa_psc]
102+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: sql_mysql_instance_psa_psc
19+
spec:
20+
skip: true

cloud_sql/mysql_instance_psc/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ resource "google_sql_database_instance" "default" {
2929
ip_configuration {
3030
psc_config {
3131
psc_enabled = true
32-
allowed_consumer_projects = []
32+
allowed_consumer_projects = [] # Add consumer project IDs here.
3333
}
3434
ipv4_enabled = false
3535
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: sql_mysql_instance_psc
19+
spec:
20+
skip: true
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Copyright 2022 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+
# [START cloud_sql_postgres_instance_psa_psc]
18+
19+
# [START vpc_postgres_instance_private_ip_network]
20+
resource "google_compute_network" "peering_network" {
21+
name = "private-network"
22+
auto_create_subnetworks = "false"
23+
}
24+
# [END vpc_postgres_instance_private_ip_network]
25+
26+
# [START vpc_postgres_instance_private_ip_address]
27+
resource "google_compute_global_address" "private_ip_address" {
28+
name = "private-ip-address"
29+
purpose = "VPC_PEERING"
30+
address_type = "INTERNAL"
31+
prefix_length = 16
32+
network = google_compute_network.peering_network.id
33+
}
34+
# [END vpc_postgres_instance_private_ip_address]
35+
36+
# [START vpc_postgres_instance_private_ip_service_connection]
37+
resource "google_service_networking_connection" "default" {
38+
network = google_compute_network.peering_network.id
39+
service = "servicenetworking.googleapis.com"
40+
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
41+
}
42+
# [END vpc_postgres_instance_private_ip_service_connection]
43+
44+
# [START cloud_sql_postgres_instance_psa_psc_instance]
45+
resource "google_sql_database_instance" "default" {
46+
name = "postgres-instance"
47+
region = "us-central1"
48+
database_version = "POSTGRES_14"
49+
50+
depends_on = [google_service_networking_connection.default]
51+
52+
settings {
53+
tier = "db-custom-2-7680"
54+
availability_type = "REGIONAL"
55+
backup_configuration {
56+
enabled = true
57+
}
58+
ip_configuration {
59+
psc_config {
60+
psc_enabled = true
61+
allowed_consumer_projects = [] # Add consumer project IDs here.
62+
}
63+
ipv4_enabled = false
64+
private_network = google_compute_network.peering_network.id
65+
}
66+
}
67+
deletion_protection = false # Set to "true" to prevent destruction of the resource
68+
}
69+
# [END cloud_sql_postgres_instance_psa_psc_instance]
70+
71+
# [START cloud_sql_postgres_instance_private_ip_routes]
72+
resource "google_compute_network_peering_routes_config" "peering_routes" {
73+
peering = google_service_networking_connection.default.peering
74+
network = google_compute_network.peering_network.name
75+
import_custom_routes = true
76+
export_custom_routes = true
77+
}
78+
# [END cloud_sql_postgres_instance_private_ip_routes]
79+
80+
# [START cloud_sql_postgres_instance_psc_endpoint]
81+
resource "google_compute_address" "default" {
82+
name = "psc-compute-address"
83+
region = "us-central1"
84+
address_type = "INTERNAL"
85+
subnetwork = "default" # Replace value with the name of the subnet here.
86+
address = "10.128.0.42" # Replace value with the IP address to reserve.
87+
}
88+
89+
data "google_sql_database_instance" "default" {
90+
name = resource.google_sql_database_instance.default.name
91+
}
92+
93+
resource "google_compute_forwarding_rule" "default" {
94+
name = "psc-forwarding-rule-${google_sql_database_instance.default.name}"
95+
region = "us-central1"
96+
network = "default"
97+
ip_address = google_compute_address.default.self_link
98+
load_balancing_scheme = ""
99+
target = data.google_sql_database_instance.default.psc_service_attachment_link
100+
}
101+
# [END cloud_sql_postgres_instance_psc_endpoint]
102+
103+
# [END cloud_sql_postgres_instance_psa_psc]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: sql_postgres_instance_psa_psc
19+
spec:
20+
skip: true
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: sql_postgres_instance_psc
19+
spec:
20+
skip: true

0 commit comments

Comments
 (0)