diff --git a/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb b/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb index 30b9f5847df9..40bb3eab7f95 100644 --- a/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb +++ b/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb @@ -677,6 +677,12 @@ is set to true. Defaults to ZONAL.`, Description: `The ID of the project in which the resource belongs. If it is not provided, the provider project is used.`, }, + "instance_type": { + Type: schema.TypeString, + Computed: true, + Description: `The type of the instance. The valid values are:- 'SQL_INSTANCE_TYPE_UNSPECIFIED', 'CLOUD_SQL_INSTANCE', 'ON_PREMISES_INSTANCE' and 'READ_REPLICA_INSTANCE'.`, + }, + "replica_configuration": { Type: schema.TypeList, Optional: true, @@ -952,6 +958,10 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) instance.MaintenanceVersion = d.Get("maintenance_version").(string) } + if _, ok := d.GetOk("instance_type"); ok { + instance.InstanceType = d.Get("instance_type").(string) + } + instance.RootPassword = d.Get("root_password").(string) // Modifying a replica during Create can cause problems if the master is @@ -1397,7 +1407,9 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e if err := d.Set("service_account_email_address", instance.ServiceAccountEmailAddress); err != nil { return fmt.Errorf("Error setting service_account_email_address: %s", err) } - + if err := d.Set("instance_type", instance.InstanceType); err != nil { + return fmt.Errorf("Error setting instance_type: %s", err) + } if err := d.Set("settings", flattenSettings(instance.Settings)); err != nil { log.Printf("[WARN] Failed to set SQL Database Instance Settings") } @@ -1540,6 +1552,10 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) defer mutexKV.Unlock(instanceMutexKey(project, v.(string))) } + if _, ok := d.GetOk("instance_type"); ok { + instance.InstanceType = d.Get("instance_type").(string) + } + err = retryTimeDuration(func() (rerr error) { op, rerr = config.NewSqlAdminClient(userAgent).Instances.Update(project, d.Get("name").(string), instance).Do() return rerr diff --git a/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb b/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb index c4a7576acb9d..3366e5cb2d52 100644 --- a/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb @@ -73,6 +73,9 @@ func TestAccSqlDatabaseInstance_basicInferredName(t *testing.T) { Steps: []resource.TestStep{ resource.TestStep{ Config: testGoogleSqlDatabaseInstance_basic2, + Check: resource.ComposeTestCheckFunc( + checkInstanceTypeIsPresent("google_sql_database_instance.instance"), + ), }, resource.TestStep{ ResourceName: "google_sql_database_instance.instance", @@ -1401,6 +1404,38 @@ func TestAccSqlDatabaseInstance_sqlMysqlInstancePvpExample(t *testing.T) { }) } +func TestAccSqlDatabaseInstance_updateReadReplicaWithBinaryLogEnabled(t *testing.T) { + t.Parallel() + + instance := "tf-test-" + randString(t, 10) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testGoogleSqlDatabaseInstance_readReplica(instance), + }, + { + ResourceName: "google_sql_database_instance.replica", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + { + Config: testGoogleSqlDatabaseInstance_updateReadReplica(instance), + }, + { + ResourceName: "google_sql_database_instance.replica", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + }, + }) +} + func testAccSqlDatabaseInstance_sqlMysqlInstancePvpExample(context map[string]interface{}) string { return Nprintf(` resource "google_sql_database_instance" "mysql_pvp_instance_name" { @@ -2672,3 +2707,160 @@ data "google_sql_backup_run" "backup" { } `, context) } + +func checkInstanceTypeIsPresent(resourceName string) func(*terraform.State) error { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("can't find %s in state", resourceName) + } + rsAttr := rs.Primary.Attributes + _, ok = rsAttr["instance_type"]; + if !ok { + return fmt.Errorf("Instance type is not computed for %s", resourceName) + } + return nil + } +} + +func testGoogleSqlDatabaseInstance_readReplica(instance string) string { + return fmt.Sprintf(` +resource "google_sql_database_instance" "master" { + region = "asia-northeast1" + name = "%s-master" + database_version = "MYSQL_5_7" + deletion_protection = false + settings { + availability_type = "ZONAL" + disk_autoresize = true + disk_size = 10 + disk_type = "PD_SSD" + tier = "db-f1-micro" + + activation_policy = "ALWAYS" + pricing_plan = "PER_USE" + + backup_configuration { + binary_log_enabled = true + enabled = true + location = "asia" + start_time = "18:00" + } + + database_flags { + name = "character_set_server" + value = "utf8mb4" + } + } +} + + +resource "google_sql_database_instance" "replica" { + depends_on = [google_sql_database_instance.master] + name = "%s-replica" + master_instance_name = google_sql_database_instance.master.name + region = "asia-northeast1" + database_version = "MYSQL_5_7" + deletion_protection = false + replica_configuration { + failover_target = false + } + settings { + tier = "db-f1-micro" + availability_type = "ZONAL" + pricing_plan = "PER_USE" + disk_autoresize = true + disk_size = 10 + + backup_configuration { + binary_log_enabled = true + } + + database_flags { + name = "slave_parallel_workers" + value = "3" + } + + database_flags { + name = "slave_parallel_type" + value = "LOGICAL_CLOCK" + } + + database_flags { + name = "slave_pending_jobs_size_max" + value = "536870912" # 512MB + } + } +}`, instance, instance) +} + +func testGoogleSqlDatabaseInstance_updateReadReplica(instance string) string { + return fmt.Sprintf(` +resource "google_sql_database_instance" "master" { + region = "asia-northeast1" + name = "%s-master" + database_version = "MYSQL_5_7" + deletion_protection = false + settings { + availability_type = "ZONAL" + disk_autoresize = true + disk_size = 10 + disk_type = "PD_SSD" + tier = "db-f1-micro" + + activation_policy = "ALWAYS" + pricing_plan = "PER_USE" + + backup_configuration { + binary_log_enabled = true + enabled = true + location = "asia" + start_time = "18:00" + } + + database_flags { + name = "character_set_server" + value = "utf8mb4" + } + } +} + + +resource "google_sql_database_instance" "replica" { + depends_on = [google_sql_database_instance.master] + name = "%s-replica" + master_instance_name = google_sql_database_instance.master.name + region = "asia-northeast1" + database_version = "MYSQL_5_7" + deletion_protection = false + replica_configuration { + failover_target = false + } + settings { + tier = "db-f1-micro" + availability_type = "ZONAL" + pricing_plan = "PER_USE" + disk_autoresize = true + disk_size = 10 + + backup_configuration { + binary_log_enabled = true + } + + database_flags { + name = "slave_parallel_workers" + value = "2" + } + + database_flags { + name = "slave_parallel_type" + value = "LOGICAL_CLOCK" + } + + database_flags { + name = "slave_pending_jobs_size_max" + value = "536870912" # 512MB + } + } +}`, instance, instance) +} \ No newline at end of file diff --git a/mmv1/third_party/terraform/website/docs/r/sql_database_instance.html.markdown b/mmv1/third_party/terraform/website/docs/r/sql_database_instance.html.markdown index 73971ee1bd4f..4fca97141285 100644 --- a/mmv1/third_party/terraform/website/docs/r/sql_database_instance.html.markdown +++ b/mmv1/third_party/terraform/website/docs/r/sql_database_instance.html.markdown @@ -481,6 +481,8 @@ a workaround for an [issue fixed in Terraform 0.12](https://github.com/hashicorp but also provides a convenient way to access an IP of a specific type without performing filtering in a Terraform config. +* `instance_type` - The type of the instance. The supported values are `SQL_INSTANCE_TYPE_UNSPECIFIED`, `CLOUD_SQL_INSTANCE`, `ON_PREMISES_INSTANCE` and `READ_REPLICA_INSTANCE`. + * `settings.version` - Used to make sure changes to the `settings` block are atomic.