Skip to content

Commit 2ea5923

Browse files
Changed to manual approach
Signed-off-by: PhanikumarThodupunoori <[email protected]>
1 parent b8db620 commit 2ea5923

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

docs/CONFIG-VARS.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,19 @@ postgres_servers = {
269269

270270
**NOTE**: The `default = {}` elements is always required when creating external databases. This is the systems default database server.
271271

272-
Each server element, like `foo = {}`, can contain none, some, or all of the parameters listed below. If `machine_type` or `edition` are not provided, default values will be applied based on the `server_version`. For example:
273-
- If `server_version >= 16`, defaults to `edition = "ENTERPRISE_PLUS"` and `machine_type = "db-perf-optimized-N-*"`
274-
- If `server_version < 16`, defaults to `edition = "ENTERPRISE"` and `machine_type = "db-custom-*"`
272+
Each server element, like `foo = {}`, can contain none, some, or all of the parameters listed below. The `machine_type` and `edition` parameters must be explicitly specified and will be validated based on the `server_version`:
273+
274+
- For PostgreSQL 16+:
275+
- `edition` must be `"ENTERPRISE_PLUS"`
276+
- `machine_type` must start with `"db-perf-optimized-N-"`
277+
- For PostgreSQL < 16:
278+
- `edition` must be `"ENTERPRISE"`
279+
- `machine_type` must start with `"db-custom-"`
275280

276281
| Name | Description | Type | Default | Notes |
277282
| :--- | ---: | ---: | ---: | ---: |
278-
| machine_type| The machine type for the PostgreSQL server VMs" | string | "db-custom-4-16384" | Google Cloud Postgres supports only shared-core machine types such as db-f1-micro, and custom machine types such as db-custom-2-13312. |
279-
| edition | Cloud SQL edition type | string | *(auto-determined)* | Accepts `"ENTERPRISE"` or `"ENTERPRISE_PLUS"`. Automatically set based on `server_version` if omitted. |
283+
| machine_type| The machine type for the PostgreSQL server VMs" | string | "db-custom-4-16384" | Google Cloud Postgres supports only shared-core machine types such as db-f1-micro, and custom machine types such as db-custom-2-13312. Must match the PostgreSQL version requirements. |
284+
| edition | Cloud SQL edition type | string | null | Must be `"ENTERPRISE"` for PostgreSQL < 16 or `"ENTERPRISE_PLUS"` for PostgreSQL 16+. |
280285
| storage_gb | Minimum storage allowed for the PostgreSQL server | number | 128 | |
281286
| backups_enabled | Enables postgres backups | bool | true | |
282287
| backups_start_time | Start time for postgres backups | string | "21:00" | |
@@ -292,18 +297,18 @@ Each server element, like `foo = {}`, can contain none, some, or all of the para
292297

293298
Multiple SAS offerings require a second PostgreSQL instance referred to as SAS Common Data Store, or CDS PostgreSQL. For more information, see [Common Customizations](https://documentation.sas.com/?cdcId=itopscdc&cdcVersion=default&docsetId=dplyml0phy0dkr&docsetTarget=n08u2yg8tdkb4jn18u8zsi6yfv3d.htm#p0wkxxi9s38zbzn19ukjjaxsc0kl). A list of SAS offerings that require CDS PostgreSQL is provided in [SAS Common Data Store Requirements](https://documentation.sas.com/?cdcId=itopscdc&cdcVersion=default&docsetId=itopssr&docsetTarget=p05lfgkwib3zxbn1t6nyihexp12n.htm#n03wzanutmc6gon1val5fykas9aa). To create and configure an external CDS PostgreSQL instance in addition to the external platform PostgreSQL instance named `default`, specify `cds-postgres` as a second PostgreSQL instance, as shown in the example below.
294299

295-
Here is an example of the `postgres_servers` variable with the `default` server entry overriding only the `administrator_password` parameter and the `cds-postgres` entry overriding all of the parameters:
300+
Here is an example of the `postgres_servers` variable with the `default` server entry and the `cds-postgres` entry, both with explicit edition and machine type configurations that match their PostgreSQL versions:
296301

297302
```terraform
298303
postgres_servers = {
299304
default = {
300305
administrator_password = "D0ntL00kTh1sWay"
301-
server_version = "" # Specify PostgreSQL version here
302-
machine_type = "" # Specify the machine type here
303-
edition = "" # Specify the edition here (e.g., ENTERPRISE, ENTERPRISE_PLUS)
306+
server_version = "16" # PostgreSQL 16
307+
machine_type = "db-perf-optimized-N-8" # Required for PostgreSQL 16+
308+
edition = "ENTERPRISE_PLUS" # Required for PostgreSQL 16+
304309
},
305310
cds-postgres = {
306-
machine_type = "db-custom-4-16384"
311+
machine_type = "db-custom-4-16384" # Required for PostgreSQL < 16
307312
storage_gb = 128
308313
backups_enabled = true
309314
backups_start_time = "21:00"
@@ -312,7 +317,8 @@ postgres_servers = {
312317
backup_count = 7 # Number of backups to retain, not in days
313318
administrator_login = "cdsadmin"
314319
administrator_password = "my$up3rS3cretPassw0rd"
315-
server_version = "15"
320+
server_version = "15" # PostgreSQL 15
321+
edition = "ENTERPRISE" # Required for PostgreSQL < 16
316322
availability_type = "ZONAL"
317323
ssl_enforcement_enabled = true
318324
database_flags = [{ name = "cloudsql.enable_pg_cron", value = "true"}, { name = "cloudsql.enable_pgaudit", value = "true"}]

main.tf

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,18 +256,8 @@ module "postgresql" {
256256
deletion_protection = false
257257
module_depends_on = [google_service_networking_connection.private_vpc_connection]
258258

259-
edition = tonumber(each.value.server_version) >= 16 ? "ENTERPRISE_PLUS" : "ENTERPRISE"
260-
261-
// If semver is 16+ and machine_type is not db-perf-optimized-*, then use db-perf-optimized-N-8
262-
// If semver is < 16 and machine_type is not db-custom-*, then use db-custom-4-16384
263-
// Otherwise, use the machine_type from the input
264-
tier = (
265-
tonumber(each.value.server_version) >= 16 && !can(regex("^db-perf-optimized-", each.value.machine_type))
266-
) || (
267-
tonumber(each.value.server_version) < 16 && !can(regex("^db-custom-", each.value.machine_type))
268-
) ? (
269-
tonumber(each.value.server_version) >= 16 ? "db-perf-optimized-N-8" : "db-custom-4-16384"
270-
) : each.value.machine_type
259+
edition = each.value.edition
260+
tier = each.value.machine_type
271261

272262
disk_size = each.value.storage_gb
273263

@@ -329,4 +319,4 @@ module "google_netapp" {
329319
volume_path = "${var.prefix}-${var.netapp_volume_path}"
330320
allowed_clients = join(",", [local.gke_subnet_cidr, local.misc_subnet_cidr])
331321
depends_on = [ module.gke ]
332-
}
322+
}

variables.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,18 @@ variable "postgres_servers" {
410410
error_message = "ERROR: The database server name must start with a letter, cannot end with a hyphen, must be between 1-88 characters in length, and can only contain hyphens, letters, and numbers."
411411
}
412412

413-
# Checking user provided login
414-
415-
# Checking user provided password
413+
# Validate edition and machine type based on PostgreSQL version
414+
validation {
415+
condition = var.postgres_servers != null ? length(var.postgres_servers) != 0 ? alltrue([
416+
for k, v in var.postgres_servers : (
417+
v.server_version != null && (
418+
(tonumber(v.server_version) >= 16 && v.edition == "ENTERPRISE_PLUS" && can(regex("^db-perf-optimized-N-", v.machine_type))) ||
419+
(tonumber(v.server_version) < 16 && v.edition == "ENTERPRISE" && can(regex("^db-custom-", v.machine_type)))
420+
)
421+
)
422+
]) : false : true
423+
error_message = "ERROR: Invalid PostgreSQL configuration:\n* PostgreSQL 16+ requires ENTERPRISE_PLUS edition and db-perf-optimized-N-* machine type\n* PostgreSQL < 16 requires ENTERPRISE edition and db-custom-* machine type"
424+
}
416425
}
417426

418427
## filestore
@@ -590,4 +599,4 @@ variable "cluster_node_pool_mode" {
590599
description = "Flag for predefined cluster node configurations - Values : default, minimal"
591600
type = string
592601
default = "default"
593-
}
602+
}

0 commit comments

Comments
 (0)