Skip to content

Commit

Permalink
feat: add master_instance_name and instance_type to mysql module (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarVanL committed Dec 18, 2023
1 parent ca9cf76 commit 6cda644
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
2 changes: 2 additions & 0 deletions modules/mysql/README.md
Expand Up @@ -36,10 +36,12 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
| follow\_gae\_application | A Google App Engine application whose zone to remain in. Must be in the same region as this instance. | `string` | `null` | no |
| iam\_users | A list of IAM users to be created in your CloudSQL instance | <pre>list(object({<br> id = string,<br> email = string<br> }))</pre> | `[]` | no |
| insights\_config | The insights\_config settings for the database. | <pre>object({<br> query_plans_per_minute = number<br> query_string_length = number<br> record_application_tags = bool<br> record_client_address = bool<br> })</pre> | `null` | no |
| instance\_type | Users can upgrade a read replica instance to a stand-alone Cloud SQL instance with the help of instance\_type. To promote, users have to set the instance\_type property as CLOUD\_SQL\_INSTANCE and remove/unset master\_instance\_name and replica\_configuration from instance configuration. This operation might cause your instance to restart. | `string` | `null` | no |
| ip\_configuration | The ip\_configuration settings subblock | <pre>object({<br> authorized_networks = optional(list(map(string)), [])<br> ipv4_enabled = optional(bool, true)<br> private_network = optional(string)<br> require_ssl = optional(bool)<br> allocated_ip_range = optional(string)<br> enable_private_path_for_google_cloud_services = optional(bool, false)<br> psc_enabled = optional(bool, false)<br> psc_allowed_consumer_projects = optional(list(string), [])<br> })</pre> | `{}` | no |
| maintenance\_window\_day | The day of week (1-7) for the master instance maintenance. | `number` | `1` | no |
| maintenance\_window\_hour | The hour of day (0-23) maintenance window for the master instance maintenance. | `number` | `23` | no |
| maintenance\_window\_update\_track | The update track of maintenance window for the master instance maintenance. Can be either `canary` or `stable`. | `string` | `"canary"` | no |
| master\_instance\_name | The name of the existing instance that will act as the master in the replication setup. | `string` | `null` | no |
| module\_depends\_on | List of modules or resources this module depends on. | `list(any)` | `[]` | no |
| name | The name of the Cloud SQL resources | `string` | n/a | yes |
| password\_validation\_policy\_config | The password validation policy settings for the database instance. | <pre>object({<br> enable_password_policy = bool<br> min_length = number<br> complexity = string<br> disallow_username_substring = bool<br> })</pre> | `null` | no |
Expand Down
31 changes: 19 additions & 12 deletions modules/mysql/main.tf
Expand Up @@ -50,14 +50,16 @@ resource "random_id" "suffix" {
}

resource "google_sql_database_instance" "default" {
provider = google-beta
project = var.project_id
name = local.master_instance_name
database_version = var.database_version
region = var.region
encryption_key_name = var.encryption_key_name
deletion_protection = var.deletion_protection
root_password = var.root_password != "" ? var.root_password : null
provider = google-beta
project = var.project_id
name = local.master_instance_name
database_version = var.database_version
region = var.region
master_instance_name = var.master_instance_name
instance_type = var.instance_type
encryption_key_name = var.encryption_key_name
deletion_protection = var.deletion_protection
root_password = var.root_password != "" ? var.root_password : null

settings {
tier = var.tier
Expand Down Expand Up @@ -173,10 +175,15 @@ resource "google_sql_database_instance" "default" {
}
}

maintenance_window {
day = var.maintenance_window_day
hour = var.maintenance_window_hour
update_track = var.maintenance_window_update_track
// Maintenance windows cannot be set for read replicas: https://cloud.google.com/sql/docs/mysql/instance-settings#maintenance-window-2ndgen
dynamic "maintenance_window" {
for_each = var.master_instance_name != null ? [] : ["true"]

content {
day = var.maintenance_window_day
hour = var.maintenance_window_hour
update_track = var.maintenance_window_update_track
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions modules/mysql/metadata.yaml
Expand Up @@ -199,6 +199,9 @@ spec:
id = string
email = string
}))
- name: instance_type
description: Users can upgrade a read replica instance to a stand-alone Cloud SQL instance with the help of instance_type. To promote, users have to set the instance_type property as CLOUD_SQL_INSTANCE and remove/unset master_instance_name and replica_configuration from instance configuration. This operation might cause your instance to restart.
type: string
- name: ip_configuration
description: The ip_configuration settings subblock
type: |-
Expand Down Expand Up @@ -229,6 +232,9 @@ spec:
description: The update track of maintenance window for the master instance maintenance. Can be either `canary` or `stable`.
type: string
default: canary
- name: master_instance_name
description: (Optional) The name of the existing instance that will act as the master in the replication setup. Note, this requires the master to have `binary_log_enabled` set, as well as existing backups.
type: string
- name: module_depends_on
description: List of modules or resources this module depends on.
type: list(any)
Expand Down
14 changes: 14 additions & 0 deletions modules/mysql/variables.tf
Expand Up @@ -49,6 +49,20 @@ variable "region" {
default = "us-central1"
}

// optional
variable "master_instance_name" {
description = "The name of the existing instance that will act as the master in the replication setup."
type = string
default = null
}

// optional
variable "instance_type" {
description = "Users can upgrade a read replica instance to a stand-alone Cloud SQL instance with the help of instance_type. To promote, users have to set the instance_type property as CLOUD_SQL_INSTANCE and remove/unset master_instance_name and replica_configuration from instance configuration. This operation might cause your instance to restart."
type = string
default = null
}

// Master
variable "tier" {
description = "The tier for the master instance."
Expand Down

0 comments on commit 6cda644

Please sign in to comment.