Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added instance_type field to google_sql_database_instance resource #7044

Merged
merged 9 commits into from
Jan 6, 2023
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 @@ -2672,3 +2675,18 @@ 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
}
}
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