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

Introducing CSQL replica promotion support in terraform #13682

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/7190.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
sql: added replica promotion support to `google_sql_database_instance`. This change will allow users to promote read replica as stand alone primary instance.
```
96 changes: 95 additions & 1 deletion google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -110,6 +111,8 @@ func resourceSqlDatabaseInstance() *schema.Resource {

CustomizeDiff: customdiff.All(
customdiff.ForceNewIfChange("settings.0.disk_size", isDiskShrinkage),
customdiff.ForceNewIfChange("master_instance_name", isMasterInstanceNameSet),
customdiff.IfValueChange("instance_type", isReplicaPromoteRequested, checkPromoteConfigurationsAndUpdateDiff),
privateNetworkCustomizeDiff,
pitrSupportDbCustomizeDiff,
),
Expand Down Expand Up @@ -669,7 +672,6 @@ is set to true. Defaults to ZONAL.`,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The name of the 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.`,
},

Expand All @@ -684,6 +686,7 @@ is set to true. Defaults to ZONAL.`,
"instance_type": {
Type: schema.TypeString,
Computed: true,
Optional: 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'.`,
},

Expand Down Expand Up @@ -1504,6 +1507,20 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
maintenance_version = v.(string)
}

promoteReadReplicaRequired := false
if d.HasChange("instance_type") {
oldInstanceType, newInstanceType := d.GetChange("instance_type")

if isReplicaPromoteRequested(nil, oldInstanceType, newInstanceType, nil) {
err = checkPromoteConfigurations(d)
if err != nil {
return err
}

promoteReadReplicaRequired = true
}
}

desiredSetting := d.Get("settings")
var op *sqladmin.Operation
var instance *sqladmin.DatabaseInstance
Expand Down Expand Up @@ -1611,6 +1628,24 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
}
}

if promoteReadReplicaRequired {
err = retryTimeDuration(func() (rerr error) {
op, rerr = config.NewSqlAdminClient(userAgent).Instances.PromoteReplica(project, d.Get("name").(string)).Do()
return rerr
}, d.Timeout(schema.TimeoutUpdate), isSqlOperationInProgressError)
if err != nil {
return fmt.Errorf("Error, failed to promote read replica instance as primary stand-alone %s: %s", instance.Name, err)
}
err = sqlAdminOperationWaitTime(config, op, project, "Promote Instance", userAgent, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
err = resourceSqlDatabaseInstanceRead(d, meta)
if err != nil {
return err
}
}

s := d.Get("settings")
instance = &sqladmin.DatabaseInstance{
Settings: expandSqlDatabaseInstanceSettings(desiredSetting.([]interface{})),
Expand Down Expand Up @@ -2106,3 +2141,62 @@ func caseDiffDashSuppress(_, old, new string, _ *schema.ResourceData) bool {
postReplaceNew := strings.Replace(new, "-", "_", -1)
return strings.ToUpper(postReplaceNew) == strings.ToUpper(old)
}

func isMasterInstanceNameSet(_ context.Context, oldMasterInstanceName interface{}, newMasterInstanceName interface{}, _ interface{}) bool {
new := newMasterInstanceName.(string)
if new == "" {
return false
}

return true
}

func isReplicaPromoteRequested(_ context.Context, oldInstanceType interface{}, newInstanceType interface{}, _ interface{}) bool {
oldInstanceType = oldInstanceType.(string)
newInstanceType = newInstanceType.(string)

if newInstanceType == "CLOUD_SQL_INSTANCE" && oldInstanceType == "READ_REPLICA_INSTANCE" {
return true
}

return false
}

func checkPromoteConfigurations(d *schema.ResourceData) error {
masterInstanceName := d.GetRawConfig().GetAttr("master_instance_name")
replicaConfiguration := d.GetRawConfig().GetAttr("replica_configuration").AsValueSlice()

return validatePromoteConfigurations(masterInstanceName, replicaConfiguration)
}

func checkPromoteConfigurationsAndUpdateDiff(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error {
masterInstanceName := diff.GetRawConfig().GetAttr("master_instance_name")
replicaConfiguration := diff.GetRawConfig().GetAttr("replica_configuration").AsValueSlice()

err := validatePromoteConfigurations(masterInstanceName, replicaConfiguration)
if err != nil {
return err
}

err = diff.SetNew("master_instance_name", nil)
if err != nil {
return err
}

err = diff.SetNew("replica_configuration", nil)
if err != nil {
return err
}
return nil
}

func validatePromoteConfigurations(masterInstanceName cty.Value, replicaConfigurations []cty.Value) error {
if !masterInstanceName.IsNull() {
return fmt.Errorf("Replica promote configuration check failed. Please remove master_instance_name and try again.")
}

if len(replicaConfigurations) != 0 {
return fmt.Errorf("Replica promote configuration check failed. Please remove replica_configuration and try again.")
}
return nil
}
Loading