Skip to content

Commit

Permalink
feat: Support new regional HA for MySQL (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev25 committed Apr 22, 2020
1 parent 952bc80 commit d3ed591
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 89 deletions.
52 changes: 1 addition & 51 deletions examples/mysql-ha/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module "mysql" {
// Master configurations
tier = "db-n1-standard-1"
zone = "c"
availability_type = "REGIONAL"
maintenance_window_day = 7
maintenance_window_hour = 12
maintenance_window_update_track = "stable"
Expand Down Expand Up @@ -133,57 +134,6 @@ module "mysql" {
]
}

// Failover replica configurations
failover_replica = true
failover_replica_name_suffix = "-test"
failover_replica_tier = "db-n1-standard-1"
failover_replica_zone = "a"
failover_replica_activation_policy = "ALWAYS"
failover_replica_crash_safe_replication = true
failover_replica_disk_autoresize = true
failover_replica_disk_type = "PD_SSD"
failover_replica_replication_type = "SYNCHRONOUS"
failover_replica_maintenance_window_day = 3
failover_replica_maintenance_window_hour = 20
failover_replica_maintenance_window_update_track = "canary"

failover_replica_user_labels = {
baz = "boo"
}

failover_replica_database_flags = [
{
name = "long_query_time"
value = "1"
},
]

failover_replica_configuration = {
dump_file_path = "gs://${var.project_id}.appspot.com/tmp"
connect_retry_interval = 5
ca_certificate = null
client_certificate = null
client_key = null
failover_target = null
master_heartbeat_period = null
password = null
ssl_cipher = null
username = null
verify_server_certificate = null
}

failover_replica_ip_configuration = {
ipv4_enabled = true
require_ssl = false
private_network = null
authorized_networks = [
{
name = "${var.project_id}-cidr"
value = var.mysql_ha_external_ip_range
},
]
}

db_name = var.mysql_ha_name
db_charset = "utf8mb4"
db_collation = "utf8mb4_general_ci"
Expand Down
1 change: 1 addition & 0 deletions modules/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| additional\_databases | A list of databases to be created in your cluster | object | `<list>` | no |
| additional\_users | A list of users to be created in your cluster | object | `<list>` | no |
| authorized\_gae\_applications | The list of authorized App Engine project names | list(string) | `<list>` | no |
| availability\_type | The availability type for the master instance. Can be either `REGIONAL` or `null`. | string | `"REGIONAL"` | no |
| backup\_configuration | The backup_configuration settings subblock for the database setings | object | `<map>` | no |
| create\_timeout | The optional timout that is applied to limit long database creates. | string | `"10m"` | no |
| database\_flags | List of Cloud SQL flags that are applied to the database server | object | `<list>` | no |
Expand Down
9 changes: 7 additions & 2 deletions modules/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ locals {
enabled = var.ip_configuration
disabled = {}
}

// HA method using REGIONAL availability_type requires binary logs to be enabled
binary_log_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "binary_log_enabled", null)
backups_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "enabled", null)
}

resource "google_sql_database_instance" "default" {
Expand All @@ -33,12 +37,13 @@ resource "google_sql_database_instance" "default" {
settings {
tier = var.tier
activation_policy = var.activation_policy
availability_type = var.availability_type
authorized_gae_applications = var.authorized_gae_applications
dynamic "backup_configuration" {
for_each = [var.backup_configuration]
content {
binary_log_enabled = lookup(backup_configuration.value, "binary_log_enabled", null)
enabled = lookup(backup_configuration.value, "enabled", null)
binary_log_enabled = local.binary_log_enabled
enabled = local.backups_enabled
start_time = lookup(backup_configuration.value, "start_time", null)
}
}
Expand Down
6 changes: 6 additions & 0 deletions modules/mysql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ variable "activation_policy" {
default = "ALWAYS"
}

variable "availability_type" {
description = "The availability type for the master instance. Can be either `REGIONAL` or `null`."
type = string
default = "REGIONAL"
}

variable "authorized_gae_applications" {
description = "The list of authorized App Engine project names"
type = list(string)
Expand Down
38 changes: 2 additions & 36 deletions test/integration/mysql-ha/controls/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
authorized_network = attribute('authorized_network')

describe google_sql_database_instances(project: project_id).where(instance_name: /#{basename}/) do
its(:count) { should eq 5 }
its(:count) { should eq 4 }
end

describe google_sql_database_instance(project: project_id, database: basename) do
let(:expected_settings) {
{
activation_policy: "ALWAYS",
availability_type: "REGIONAL",
data_disk_size_gb: 10,
data_disk_type: "PD_SSD",
kind: "sql#settings",
Expand Down Expand Up @@ -57,41 +58,6 @@
it { expect(user_labels).to include(foo: "bar") }
end

describe google_sql_database_instance(project: project_id, database: "#{basename}-failover-test") do
let(:expected_settings) {
{
activation_policy: "ALWAYS",
data_disk_size_gb: 10,
data_disk_type: "PD_SSD",
kind: "sql#settings",
pricing_plan: "PER_USE",
replication_type: "SYNCHRONOUS",
storage_auto_resize: true,
storage_auto_resize_limit: 0,
tier: "db-n1-standard-1",
}
}
let(:settings) { subject.settings.item }
let(:ip_configuration) { settings[:ip_configuration] }
let(:database_flags) { settings[:database_flags] }
let(:location_preference) { settings[:location_preference] }
let(:maintenance_window) { settings[:maintenance_window] }
let(:user_labels) { settings[:user_labels] }

its(:backend_type) { should eq 'SECOND_GEN' }
its(:database_version) { should eq 'MYSQL_5_7' }
its(:state) { should eq 'RUNNABLE' }
its(:region) { should eq 'us-central1' }
its(:gce_zone) { should eq 'us-central1-a' }

it { expect(settings).to include(expected_settings) }
it { expect(ip_configuration).to include(authorized_networks: [{kind: 'sql#aclEntry', name: "#{project_id}-cidr", value: authorized_network}], ipv4_enabled: true, require_ssl: false) }
it { expect(database_flags).to include(name: "long_query_time", value: "1") }
it { expect(location_preference).to include(kind: "sql#locationPreference", zone: "us-central1-a") }
it { expect(maintenance_window).to include(kind: "sql#maintenanceWindow", day: 3, hour: 20, update_track: "canary") }
it { expect(user_labels).to include(baz: "boo") }
end

%i[a b c].each_with_index do |zone, index|
name = "#{basename}-replica-test#{index}"
describe google_sql_database_instance(project: project_id, database: name) do
Expand Down

0 comments on commit d3ed591

Please sign in to comment.