-
Notifications
You must be signed in to change notification settings - Fork 948
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
984 additions
and
133 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
locals { | ||
psc_network_configs = { | ||
for project_id, psc_config in var.psc_consumers : | ||
project_id => merge( | ||
psc_config, | ||
{ | ||
attachment_project = coalesce(psc_config.attachment_project, project_id) | ||
} | ||
) if psc_config != null | ||
} | ||
# this has to be separate from psc_network_configs to avoid cyclic dependencies | ||
psc_address_self_links = { | ||
for project_id, psc_config in local.psc_network_configs : | ||
project_id => ( | ||
psc_config.create_address ? | ||
google_compute_address.psc_address[project_id].self_link | ||
: psc_config.address | ||
) | ||
} | ||
|
||
# Replicas support | ||
# Endpoint for primary instance is separated from replicas endpoints to allow | ||
# easy copy of the PSC endpoint code between modules | ||
replicas_psc_network_configs = merge([ | ||
for replica_name, replica_config in var.replicas : | ||
{ | ||
for project_id, psc_config in coalesce(replica_config.psc_consumers, {}) : | ||
"${replica_name}-${project_id}" => merge( | ||
psc_config, | ||
{ | ||
attachment_project = coalesce(psc_config.attachment_project, project_id) | ||
region = replica_config.region | ||
replica_name = replica_name | ||
} | ||
) if psc_config != null | ||
} | ||
]...) | ||
replicas_psc_address_self_links = { | ||
for key, psc_config in local.replicas_psc_network_configs : | ||
key => ( | ||
psc_config.create_address ? | ||
google_compute_address.replica_psc_address[key].self_link | ||
: psc_config.address | ||
) | ||
} | ||
} | ||
|
||
|
||
resource "google_compute_address" "psc_address" { | ||
for_each = { | ||
for project_id, psc_config in local.psc_network_configs : project_id => psc_config if psc_config.create_address | ||
} | ||
name = coalesce(each.value.address, "psc-cloudsql-primary-${var.name}-${each.key}") | ||
project = each.value.attachment_project | ||
region = var.region | ||
address_type = "INTERNAL" | ||
subnetwork = each.value.subnet_self_link | ||
address = each.value.ipv4_address | ||
} | ||
|
||
resource "google_compute_forwarding_rule" "main" { | ||
for_each = local.psc_network_configs | ||
name = "psc-cloudsql-primary-${var.name}-${each.key}" | ||
project = each.value.attachment_project | ||
region = var.region | ||
subnetwork = each.value.subnet_self_link | ||
ip_address = local.psc_address_self_links[each.key] | ||
load_balancing_scheme = "" | ||
target = google_sql_database_instance.primary.psc_service_attachment_link | ||
} | ||
|
||
|
||
### Replicas support | ||
|
||
resource "google_compute_address" "replica_psc_address" { | ||
for_each = { | ||
for key, psc_config in local.replicas_psc_network_configs : key => psc_config if psc_config.create_address | ||
} | ||
name = coalesce(each.value.address, "psc-cloudsql-primary-${var.name}-${each.key}") | ||
project = each.value.attachment_project | ||
region = each.value.region | ||
address_type = "INTERNAL" | ||
subnetwork = each.value.subnet_self_link | ||
address = each.value.ipv4_address | ||
} | ||
|
||
resource "google_compute_forwarding_rule" "replicas" { | ||
for_each = local.replicas_psc_network_configs | ||
name = "psc-cloudsql-primary-${var.name}-${each.key}" | ||
project = each.value.attachment_project | ||
region = each.value.region | ||
subnetwork = each.value.subnet_self_link | ||
ip_address = local.replicas_psc_address_self_links[each.key] | ||
load_balancing_scheme = "" | ||
target = google_sql_database_instance.replicas[each.value.replica_name].psc_service_attachment_link | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
variable "psc_consumers" { | ||
default = {} | ||
description = "Map of PSC Consumers with project_id as a key, and PSC attachment configuration as a value. Use null as value to skip creating a service attachment for this project, but define project as consumer." | ||
nullable = false | ||
type = map(object({ | ||
subnet_self_link = string | ||
address = optional(string) # name if create_address == true self_link if create_address == false | ||
attachment_project = optional(string) # defaults to consumer project | ||
create_address = optional(bool, true) | ||
ipv4_address = optional(string) | ||
})) | ||
validation { | ||
error_message = "ipv4_address is allowed only when create_address is true" | ||
condition = alltrue([ | ||
for consumer in values( | ||
var.psc_consumers | ||
) : ( | ||
try(consumer.ipv4_address, null) == null || try(consumer.create_address, true) | ||
) | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
resource "google_kms_crypto_key_iam_binding" "encrypt_decrypt" { | ||
crypto_key_id = var.kms_key.id | ||
members = [ | ||
"serviceAccount:service-${var.project_number}@gcp-sa-cloud-sql.iam.gserviceaccount.com" | ||
] | ||
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
name = "db" | ||
network_config = { | ||
connectivity = {} | ||
} | ||
project_id = "db-project" | ||
region = "europe-west4" | ||
availability_type = "REGIONAL" | ||
database_version = "POSTGRES_13" | ||
tier = "db-g1-small" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.