Skip to content

Commit a7f05b6

Browse files
Merge pull request #257 from sassoftware/PSKD-1632
feat(postgres): support edition param for PG 16+ with defaults
2 parents d850e80 + 2e63004 commit a7f05b6

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

docs/CONFIG-VARS.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,31 @@ 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:
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-"`
280+
281+
*NOTE*: The default = {} elements is always required when creating external databases. This is the systems default database server.
282+
283+
If you provide an empty block for default, the following default values will be used:
284+
terraform
285+
postgres_servers = {
286+
default = {
287+
server_version = "15"
288+
edition = "ENTERPRISE"
289+
machine_type = "db-custom-4-16384"
290+
}
291+
}
273292

274293
| Name | Description | Type | Default | Notes |
275294
| :--- | ---: | ---: | ---: | ---: |
276-
| 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. |
295+
| 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. |
296+
| edition | Cloud SQL edition type | string | "ENTERPRISE" | Must be `"ENTERPRISE"` for PostgreSQL < 16 or `"ENTERPRISE_PLUS"` for PostgreSQL 16+. |
277297
| storage_gb | Minimum storage allowed for the PostgreSQL server | number | 128 | |
278298
| backups_enabled | Enables postgres backups | bool | true | |
279299
| backups_start_time | Start time for postgres backups | string | "21:00" | |
@@ -289,15 +309,18 @@ Each server element, like `foo = {}`, can contain none, some, or all of the para
289309

290310
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.
291311

292-
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:
312+
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:
293313

294314
```terraform
295315
postgres_servers = {
296316
default = {
297317
administrator_password = "D0ntL00kTh1sWay"
318+
server_version = "16" # PostgreSQL 16
319+
machine_type = "db-perf-optimized-N-8" # Required for PostgreSQL 16+
320+
edition = "ENTERPRISE_PLUS" # Required for PostgreSQL 16+
298321
},
299322
cds-postgres = {
300-
machine_type = "db-custom-4-16384"
323+
machine_type = "db-custom-4-16384" # Required for PostgreSQL < 16
301324
storage_gb = 128
302325
backups_enabled = true
303326
backups_start_time = "21:00"
@@ -306,7 +329,8 @@ postgres_servers = {
306329
backup_count = 7 # Number of backups to retain, not in days
307330
administrator_login = "cdsadmin"
308331
administrator_password = "my$up3rS3cretPassw0rd"
309-
server_version = "15"
332+
server_version = "15" # PostgreSQL 15
333+
edition = "ENTERPRISE" # Required for PostgreSQL < 16
310334
availability_type = "ZONAL"
311335
ssl_enforcement_enabled = true
312336
database_flags = [{ name = "cloudsql.enable_pg_cron", value = "true"}, { name = "cloudsql.enable_pgaudit", value = "true"}]

examples/sample-input.tfvars

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ tags = {} # e.g., { "key1" = "value1", "key2" = "value2" }
2222
# Postgres config - By having this entry a database server is created. If you do not
2323
# need an external database server remove the 'postgres_servers'
2424
# block below.
25+
2526
postgres_servers = {
2627
default = {},
27-
}
28+
},
2829

2930
# GKE config
3031
kubernetes_version = "1.31"

main.tf

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

259-
tier = each.value.machine_type
259+
edition = each.value.edition
260+
tier = each.value.machine_type
261+
260262
disk_size = each.value.storage_gb
261263

262264
enable_default_db = false
@@ -317,4 +319,4 @@ module "google_netapp" {
317319
volume_path = "${var.prefix}-${var.netapp_volume_path}"
318320
allowed_clients = join(",", [local.gke_subnet_cidr, local.misc_subnet_cidr])
319321
depends_on = [ module.gke ]
320-
}
322+
}

variables.tf

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ variable "node_pools" {
332332
# Potentially we upgrade Terraform modules and versions and we bump our minimum required terraform version to be >1.3
333333
# then at that time I can deprecate this variable and instead allow the user to configure node_locations per node pool.
334334
# Refer to https://github.com/hashicorp/terraform/issues/29407#issuecomment-1150491619
335+
335336
variable "nodepools_locations" {
336337
description = "GCP zone(s) where the additional node pools will allocate nodes in. Comma separated list."
337338
type = string
@@ -382,6 +383,7 @@ variable "postgres_server_defaults" {
382383
availability_type = "ZONAL"
383384
ssl_enforcement_enabled = true
384385
database_flags = []
386+
edition = "ENTERPRISE"
385387
}
386388
}
387389

@@ -390,7 +392,7 @@ variable "postgres_servers" {
390392
description = "Map of PostgreSQL server objects"
391393
type = any
392394
default = null
393-
395+
394396
# Checking for user provided "default" server
395397
validation {
396398
condition = var.postgres_servers != null ? length(var.postgres_servers) != 0 ? contains(keys(var.postgres_servers), "default") : false : true
@@ -409,9 +411,23 @@ variable "postgres_servers" {
409411
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."
410412
}
411413

412-
# Checking user provided login
413-
414-
# Checking user provided password
414+
# Validate edition and machine type based on PostgreSQL version
415+
validation {
416+
condition = var.postgres_servers != null ? length(var.postgres_servers) != 0 ? alltrue([
417+
for k, v in var.postgres_servers : (
418+
# If the object is empty, use default values
419+
length(keys(v)) == 0 ? true : (
420+
can(try(v.server_version, null)) &&
421+
can(try(v.edition, null)) &&
422+
can(try(v.machine_type, null)) && (
423+
(tonumber(try(v.server_version, "15")) >= 16 && try(v.edition, "ENTERPRISE") == "ENTERPRISE_PLUS" && can(regex("^db-perf-optimized-N-", try(v.machine_type, "")))) ||
424+
(tonumber(try(v.server_version, "15")) < 16 && try(v.edition, "ENTERPRISE") == "ENTERPRISE" && can(regex("^db-custom-", try(v.machine_type, ""))))
425+
)
426+
)
427+
)
428+
]) : false : true
429+
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"
430+
}
415431
}
416432

417433
## filestore
@@ -578,7 +594,6 @@ variable "gke_network_policy" {
578594
default = false
579595
}
580596

581-
582597
variable "create_static_kubeconfig" {
583598
description = "Allows the user to create a provider / service account based kube config file"
584599
type = bool
@@ -589,4 +604,4 @@ variable "cluster_node_pool_mode" {
589604
description = "Flag for predefined cluster node configurations - Values : default, minimal"
590605
type = string
591606
default = "default"
592-
}
607+
}

0 commit comments

Comments
 (0)