Skip to content

Commit

Permalink
Added instance_type field to google_sql_database_instance resource (
Browse files Browse the repository at this point in the history
  • Loading branch information
ravisiddhu authored and i-laird committed Jan 10, 2023
1 parent 796f26d commit 57871dd
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 57871dd

Please sign in to comment.