Skip to content

Commit

Permalink
manage switchover
Browse files Browse the repository at this point in the history
  • Loading branch information
simonebruzzechesse committed Nov 27, 2024
1 parent 5e08789 commit 839357c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
38 changes: 19 additions & 19 deletions modules/alloydb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,31 +257,31 @@ module "alloydb" {
| name | description | type | required | default |
|---|---|:---:|:---:|:---:|
| [cluster_name](variables.tf#L99) | Name of the primary cluster. | <code>string</code> || |
| [instance_name](variables.tf#L184) | Name of primary instance. | <code>string</code> || |
| [location](variables.tf#L195) | Region or zone of the cluster and instance. | <code>string</code> || |
| [network_config](variables.tf#L251) | Network configuration for cluster and instance. Only one between psa_config and psc_config can be used. | <code title="object&#40;&#123;&#10; psa_config &#61; optional&#40;object&#40;&#123;&#10; network &#61; optional&#40;string&#41;&#10; allocated_ip_range &#61; optional&#40;string&#41;&#10; authorized_external_networks &#61; optional&#40;list&#40;string&#41;, &#91;&#93;&#41;&#10; enable_public_ip &#61; optional&#40;bool, false&#41;&#10; &#125;&#41;&#41;&#10; psc_config &#61; optional&#40;object&#40;&#123;&#10; allowed_consumer_projects &#61; optional&#40;list&#40;string&#41;, &#91;&#93;&#41;&#10; &#125;&#41;, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> || |
| [project_id](variables.tf#L294) | The ID of the project where this instances will be created. | <code>string</code> || |
| [instance_name](variables.tf#L192) | Name of primary instance. | <code>string</code> || |
| [location](variables.tf#L203) | Region or zone of the cluster and instance. | <code>string</code> || |
| [network_config](variables.tf#L259) | Network configuration for cluster and instance. Only one between psa_config and psc_config can be used. | <code title="object&#40;&#123;&#10; psa_config &#61; optional&#40;object&#40;&#123;&#10; network &#61; optional&#40;string&#41;&#10; allocated_ip_range &#61; optional&#40;string&#41;&#10; authorized_external_networks &#61; optional&#40;list&#40;string&#41;, &#91;&#93;&#41;&#10; enable_public_ip &#61; optional&#40;bool, false&#41;&#10; &#125;&#41;&#41;&#10; psc_config &#61; optional&#40;object&#40;&#123;&#10; allowed_consumer_projects &#61; optional&#40;list&#40;string&#41;, &#91;&#93;&#41;&#10; &#125;&#41;, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> || |
| [project_id](variables.tf#L302) | The ID of the project where this instances will be created. | <code>string</code> || |
| [annotations](variables.tf#L17) | Map FLAG_NAME=>VALUE for annotations which allow client tools to store small amount of arbitrary data. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [automated_backup_configuration](variables.tf#L23) | Automated backup settings for cluster. | <code title="object&#40;&#123;&#10; enabled &#61; optional&#40;bool, false&#41;&#10; backup_window &#61; optional&#40;string, &#34;1800s&#34;&#41;&#10; location &#61; optional&#40;string&#41;&#10; weekly_schedule &#61; optional&#40;object&#40;&#123;&#10; days_of_week &#61; optional&#40;list&#40;string&#41;, &#91;&#10; &#34;MONDAY&#34;, &#34;TUESDAY&#34;, &#34;WEDNESDAY&#34;, &#34;THURSDAY&#34;, &#34;FRIDAY&#34;, &#34;SATURDAY&#34;, &#34;SUNDAY&#34;&#10; &#93;&#41;&#10; start_times &#61; optional&#40;object&#40;&#123;&#10; hours &#61; optional&#40;number, 23&#41;&#10; minutes &#61; optional&#40;number, 0&#41;&#10; seconds &#61; optional&#40;number, 0&#41;&#10; nanos &#61; optional&#40;number, 0&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; retention_count &#61; optional&#40;number, 7&#41;&#10; retention_period &#61; optional&#40;string, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; enabled &#61; false&#10; backup_window &#61; &#34;1800s&#34;&#10; location &#61; null&#10; weekly_schedule &#61; &#123;&#10; days_of_week &#61; &#91;&#34;MONDAY&#34;, &#34;TUESDAY&#34;, &#34;WEDNESDAY&#34;, &#34;THURSDAY&#34;, &#34;FRIDAY&#34;, &#34;SATURDAY&#34;, &#34;SUNDAY&#34;&#93;&#10; start_times &#61; &#123;&#10; hours &#61; 23&#10; minutes &#61; 0&#10; seconds &#61; 0&#10; nanos &#61; 0&#10; &#125;&#10; &#125;&#10; retention_count &#61; 7&#10; retention_period &#61; null&#10;&#125;">&#123;&#8230;&#125;</code> |
| [availability_type](variables.tf#L76) | Availability type for the primary replica. Either `ZONAL` or `REGIONAL`. | <code>string</code> | | <code>&#34;REGIONAL&#34;</code> |
| [client_connection_config](variables.tf#L82) | Client connection config. | <code title="object&#40;&#123;&#10; require_connectors &#61; optional&#40;bool, false&#41;&#10; ssl_config &#61; optional&#40;object&#40;&#123;&#10; ssl_mode &#61; string&#10; &#125;&#41;, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [cluster_display_name](variables.tf#L93) | Display name of the primary cluster. | <code>string</code> | | <code>null</code> |
| [continuous_backup_configuration](variables.tf#L104) | Continuous backup settings for cluster. | <code title="object&#40;&#123;&#10; enabled &#61; optional&#40;bool, false&#41;&#10; recovery_window_days &#61; optional&#40;number, 14&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; enabled &#61; true&#10; recovery_window_days &#61; 14&#10;&#125;">&#123;&#8230;&#125;</code> |
| [cross_region_replication](variables.tf#L117) | Cross region replication config. | <code title="object&#40;&#123;&#10; enabled &#61; optional&#40;bool, false&#41;&#10; promote_secondary &#61; optional&#40;bool, false&#41;&#10; region &#61; optional&#40;string, null&#41;&#10; secondary_cluster_display_name &#61; optional&#40;string, null&#41;&#10; secondary_cluster_name &#61; optional&#40;string, null&#41;&#10; secondary_instance_display_name &#61; optional&#40;string, null&#41;&#10; secondary_instance_name &#61; optional&#40;string, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [database_version](variables.tf#L135) | Database type and version to create. | <code>string</code> | | <code>&#34;POSTGRES_15&#34;</code> |
| [deletion_policy](variables.tf#L141) | AlloyDB cluster and instance deletion policy. | <code>string</code> | | <code>null</code> |
| [display_name](variables.tf#L147) | AlloyDB instance display name. | <code>string</code> | | <code>null</code> |
| [encryption_config](variables.tf#L153) | Set encryption configuration. KMS name format: 'projects/[PROJECT]/locations/[REGION]/keyRings/[RING]/cryptoKeys/[KEY_NAME]'. | <code title="object&#40;&#123;&#10; primary_kms_key_name &#61; string&#10; secondary_kms_key_name &#61; optional&#40;string, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [flags](variables.tf#L163) | Map FLAG_NAME=>VALUE for database-specific tuning. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [gce_zone](variables.tf#L169) | The GCE zone that the instance should serve from. This can ONLY be specified for ZONAL instances. If present for a REGIONAL instance, an error will be thrown. | <code>string</code> | | <code>null</code> |
| [initial_user](variables.tf#L175) | AlloyDB cluster initial user credentials. | <code title="object&#40;&#123;&#10; user &#61; optional&#40;string, &#34;root&#34;&#41;&#10; password &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [labels](variables.tf#L189) | Labels to be attached to all instances. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [machine_config](variables.tf#L200) | AlloyDB machine config. | <code title="object&#40;&#123;&#10; cpu_count &#61; optional&#40;number, 2&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; cpu_count &#61; 2&#10;&#125;">&#123;&#8230;&#125;</code> |
| [maintenance_config](variables.tf#L211) | Set maintenance window configuration. | <code title="object&#40;&#123;&#10; enabled &#61; optional&#40;bool, false&#41;&#10; day &#61; optional&#40;string, &#34;SUNDAY&#34;&#41;&#10; start_time &#61; optional&#40;object&#40;&#123;&#10; hours &#61; optional&#40;number, 23&#41;&#10; minutes &#61; optional&#40;number, 0&#41;&#10; seconds &#61; optional&#40;number, 0&#41;&#10; nanos &#61; optional&#40;number, 0&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; enabled &#61; false&#10; day &#61; &#34;SUNDAY&#34;&#10; start_time &#61; &#123;&#10; hours &#61; 23&#10; minutes &#61; 0&#10; seconds &#61; 0&#10; nanos &#61; 0&#10; &#125;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [prefix](variables.tf#L284) | Optional prefix used to generate instance names. | <code>string</code> | | <code>null</code> |
| [query_insights_config](variables.tf#L299) | Query insights config. | <code title="object&#40;&#123;&#10; query_string_length &#61; optional&#40;number, 1024&#41;&#10; record_application_tags &#61; optional&#40;bool, true&#41;&#10; record_client_address &#61; optional&#40;bool, true&#41;&#10; query_plans_per_minute &#61; optional&#40;number, 5&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; query_string_length &#61; 1024&#10; record_application_tags &#61; true&#10; record_client_address &#61; true&#10; query_plans_per_minute &#61; 5&#10;&#125;">&#123;&#8230;&#125;</code> |
| [tag_bindings](variables.tf#L315) | Tag bindings for this service, in key => tag value id format. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> |
| [users](variables.tf#L322) | Map of users to create in the primary instance (and replicated to other replicas). Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'ALLOYDB_BUILT_IN' or 'ALLOYDB_IAM_USER'. | <code title="map&#40;object&#40;&#123;&#10; password &#61; optional&#40;string&#41;&#10; roles &#61; optional&#40;list&#40;string&#41;, &#91;&#34;alloydbsuperuser&#34;&#93;&#41;&#10; type &#61; optional&#40;string&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>null</code> |
| [cross_region_replication](variables.tf#L117) | Cross region replication config. | <code title="object&#40;&#123;&#10; enabled &#61; optional&#40;bool, false&#41;&#10; promote_secondary &#61; optional&#40;bool, false&#41;&#10; switchover_mode &#61; optional&#40;bool, false&#41;&#10; region &#61; optional&#40;string, null&#41;&#10; secondary_cluster_display_name &#61; optional&#40;string, null&#41;&#10; secondary_cluster_name &#61; optional&#40;string, null&#41;&#10; secondary_instance_display_name &#61; optional&#40;string, null&#41;&#10; secondary_instance_name &#61; optional&#40;string, null&#41;&#10; secondary_machine_config &#61; optional&#40;object&#40;&#123;&#10; cpu_count &#61; number&#10; &#125;&#41;, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [database_version](variables.tf#L143) | Database type and version to create. | <code>string</code> | | <code>&#34;POSTGRES_15&#34;</code> |
| [deletion_policy](variables.tf#L149) | AlloyDB cluster and instance deletion policy. | <code>string</code> | | <code>null</code> |
| [display_name](variables.tf#L155) | AlloyDB instance display name. | <code>string</code> | | <code>null</code> |
| [encryption_config](variables.tf#L161) | Set encryption configuration. KMS name format: 'projects/[PROJECT]/locations/[REGION]/keyRings/[RING]/cryptoKeys/[KEY_NAME]'. | <code title="object&#40;&#123;&#10; primary_kms_key_name &#61; string&#10; secondary_kms_key_name &#61; optional&#40;string, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [flags](variables.tf#L171) | Map FLAG_NAME=>VALUE for database-specific tuning. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [gce_zone](variables.tf#L177) | The GCE zone that the instance should serve from. This can ONLY be specified for ZONAL instances. If present for a REGIONAL instance, an error will be thrown. | <code>string</code> | | <code>null</code> |
| [initial_user](variables.tf#L183) | AlloyDB cluster initial user credentials. | <code title="object&#40;&#123;&#10; user &#61; optional&#40;string, &#34;root&#34;&#41;&#10; password &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [labels](variables.tf#L197) | Labels to be attached to all instances. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [machine_config](variables.tf#L208) | AlloyDB machine config. | <code title="object&#40;&#123;&#10; cpu_count &#61; optional&#40;number, 2&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; cpu_count &#61; 2&#10;&#125;">&#123;&#8230;&#125;</code> |
| [maintenance_config](variables.tf#L219) | Set maintenance window configuration. | <code title="object&#40;&#123;&#10; enabled &#61; optional&#40;bool, false&#41;&#10; day &#61; optional&#40;string, &#34;SUNDAY&#34;&#41;&#10; start_time &#61; optional&#40;object&#40;&#123;&#10; hours &#61; optional&#40;number, 23&#41;&#10; minutes &#61; optional&#40;number, 0&#41;&#10; seconds &#61; optional&#40;number, 0&#41;&#10; nanos &#61; optional&#40;number, 0&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; enabled &#61; false&#10; day &#61; &#34;SUNDAY&#34;&#10; start_time &#61; &#123;&#10; hours &#61; 23&#10; minutes &#61; 0&#10; seconds &#61; 0&#10; nanos &#61; 0&#10; &#125;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [prefix](variables.tf#L292) | Optional prefix used to generate instance names. | <code>string</code> | | <code>null</code> |
| [query_insights_config](variables.tf#L307) | Query insights config. | <code title="object&#40;&#123;&#10; query_string_length &#61; optional&#40;number, 1024&#41;&#10; record_application_tags &#61; optional&#40;bool, true&#41;&#10; record_client_address &#61; optional&#40;bool, true&#41;&#10; query_plans_per_minute &#61; optional&#40;number, 5&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; query_string_length &#61; 1024&#10; record_application_tags &#61; true&#10; record_client_address &#61; true&#10; query_plans_per_minute &#61; 5&#10;&#125;">&#123;&#8230;&#125;</code> |
| [tag_bindings](variables.tf#L323) | Tag bindings for this service, in key => tag value id format. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> |
| [users](variables.tf#L330) | Map of users to create in the primary instance (and replicated to other replicas). Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'ALLOYDB_BUILT_IN' or 'ALLOYDB_IAM_USER'. | <code title="map&#40;object&#40;&#123;&#10; password &#61; optional&#40;string&#41;&#10; roles &#61; optional&#40;list&#40;string&#41;, &#91;&#34;alloydbsuperuser&#34;&#93;&#41;&#10; type &#61; optional&#40;string&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>null</code> |

## Outputs

Expand Down
19 changes: 13 additions & 6 deletions modules/alloydb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ resource "google_alloydb_cluster" "primary" {
project = var.project_id
annotations = var.annotations
cluster_id = local.primary_cluster_name
cluster_type = "PRIMARY"
cluster_type = var.cross_region_replication.switchover_mode ? "SECONDARY" : "PRIMARY"
database_version = var.database_version
deletion_policy = var.deletion_policy
display_name = coalesce(var.cluster_display_name, local.primary_cluster_name)
Expand Down Expand Up @@ -144,6 +144,13 @@ resource "google_alloydb_cluster" "primary" {
psc_enabled = var.network_config.psc_config != null ? true : null
}

dynamic "secondary_config" {
for_each = var.cross_region_replication.switchover_mode ? [""] : []
content {
primary_cluster_name = "projects/${var.project_id}/locations/${var.cross_region_replication.region}/clusters/${local.secondary_cluster_name}"
}
}

# waiting to fix this issue https://github.com/hashicorp/terraform-provider-google/issues/14944
lifecycle {
ignore_changes = [
Expand All @@ -161,7 +168,7 @@ resource "google_alloydb_instance" "primary" {
database_flags = var.flags
display_name = coalesce(var.display_name, local.primary_instance_name)
instance_id = local.primary_instance_name
instance_type = "PRIMARY"
instance_type = var.cross_region_replication.switchover_mode ? "SECONDARY" : "PRIMARY"
gce_zone = local.is_regional ? null : var.gce_zone
labels = var.labels

Expand Down Expand Up @@ -228,7 +235,7 @@ resource "google_alloydb_cluster" "secondary" {
project = var.project_id
annotations = var.annotations
cluster_id = local.secondary_cluster_name
cluster_type = var.cross_region_replication.promote_secondary ? "PRIMARY" : "SECONDARY"
cluster_type = var.cross_region_replication.promote_secondary || var.cross_region_replication.switchover_mode ? "PRIMARY" : "SECONDARY"
database_version = var.database_version
deletion_policy = "FORCE"
display_name = coalesce(var.cross_region_replication.secondary_cluster_display_name, local.secondary_cluster_name)
Expand Down Expand Up @@ -322,7 +329,7 @@ resource "google_alloydb_cluster" "secondary" {
}

dynamic "secondary_config" {
for_each = var.cross_region_replication.promote_secondary ? [] : [""]
for_each = var.cross_region_replication.promote_secondary || var.cross_region_replication.switchover_mode ? [] : [""]
content {
primary_cluster_name = google_alloydb_cluster.primary.id
}
Expand Down Expand Up @@ -365,9 +372,9 @@ resource "google_alloydb_instance" "secondary" {
}

dynamic "machine_config" {
for_each = var.machine_config != null ? [""] : []
for_each = var.machine_config != null || var.cross_region_replication.secondary_machine_config != null ? [""] : []
content {
cpu_count = var.machine_config.cpu_count
cpu_count = coalesce(try(var.machine_config.cpu_count, null), try(var.cross_region_replication.secondary_machine_config.cpu_count, null))
}
}

Expand Down
9 changes: 6 additions & 3 deletions modules/alloydb/tags.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
* limitations under the License.
*/

data "google_project" "project" {
project_id = var.project_id
}

resource "google_tags_location_tag_binding" "primary_binding" {
for_each = var.tag_bindings
parent = (
"//alloydb.googleapis.com/${google_alloydb_cluster.primary.id}"
"//alloydb.googleapis.com/projects/${data.google_project.project.number}/locations/${var.location}/clusters/${google_alloydb_cluster.primary.cluster_id}"
)
tag_value = each.value
location = var.location
Expand All @@ -26,9 +30,8 @@ resource "google_tags_location_tag_binding" "primary_binding" {
resource "google_tags_location_tag_binding" "secondary_binding" {
for_each = var.cross_region_replication.enabled ? var.tag_bindings : {}
parent = (
"//alloydb.googleapis.com/${google_alloydb_cluster.secondary[0].id}"
"//alloydb.googleapis.com/projects/${data.google_project.project.number}/locations/${var.cross_region_replication.region}/clusters/${google_alloydb_cluster.secondary[0].cluster_id}"
)
tag_value = each.value
location = var.cross_region_replication.region
}

Loading

0 comments on commit 839357c

Please sign in to comment.