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

Fix issue with GCP Cloud SQL Instance disk_autoresize #14582

Merged
merged 3 commits into from
May 18, 2017
Merged
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
50 changes: 30 additions & 20 deletions builtin/providers/google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package google

import (
"fmt"
"log"
"regexp"
"strings"

"github.com/hashicorp/terraform/helper/resource"
Expand All @@ -28,6 +30,7 @@ func resourceSqlDatabaseInstance() *schema.Resource {
"settings": &schema.Schema{
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"version": &schema.Schema{
Expand Down Expand Up @@ -89,8 +92,10 @@ func resourceSqlDatabaseInstance() *schema.Resource {
},
},
"disk_autoresize": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Type: schema.TypeBool,
Default: true,
Optional: true,
DiffSuppressFunc: suppressFirstGen,
},
"disk_size": &schema.Schema{
Type: schema.TypeInt,
Expand Down Expand Up @@ -302,6 +307,23 @@ func resourceSqlDatabaseInstance() *schema.Resource {
}
}

// Suppress diff with any disk_autoresize value on 1st Generation Instances
func suppressFirstGen(k, old, new string, d *schema.ResourceData) bool {
settingsList := d.Get("settings").([]interface{})

settings := settingsList[0].(map[string]interface{})
tier := settings["tier"].(string)
matched, err := regexp.MatchString("db*", tier)
if err != nil {
log.Printf("[ERR] error with regex in diff supression for disk_autoresize: %s", err)
}
if !matched {
log.Printf("[DEBUG] suppressing diff on disk_autoresize due to 1st gen instance type")
return true
}
return false
}

func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand All @@ -314,13 +336,11 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
databaseVersion := d.Get("database_version").(string)

_settingsList := d.Get("settings").([]interface{})
if len(_settingsList) > 1 {
return fmt.Errorf("At most one settings block is allowed")
}

_settings := _settingsList[0].(map[string]interface{})
settings := &sqladmin.Settings{
Tier: _settings["tier"].(string),
Tier: _settings["tier"].(string),
ForceSendFields: []string{"StorageAutoResize"},
}

if v, ok := _settings["activation_policy"]; ok {
Expand Down Expand Up @@ -363,9 +383,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
settings.CrashSafeReplicationEnabled = v.(bool)
}

if v, ok := _settings["disk_autoresize"]; ok && v.(bool) {
settings.StorageAutoResize = v.(bool)
}
settings.StorageAutoResize = _settings["disk_autoresize"].(bool)

if v, ok := _settings["disk_size"]; ok && v.(int) > 0 {
settings.DataDiskSizeGb = int64(v.(int))
Expand Down Expand Up @@ -662,11 +680,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
_settings["crash_safe_replication"] = settings.CrashSafeReplicationEnabled
}

if v, ok := _settings["disk_autoresize"]; ok && v != nil {
if v.(bool) {
_settings["disk_autoresize"] = settings.StorageAutoResize
}
}
_settings["disk_autoresize"] = settings.StorageAutoResize

if v, ok := _settings["disk_size"]; ok && v != nil {
if v.(int) > 0 && settings.DataDiskSizeGb < int64(v.(int)) {
Expand Down Expand Up @@ -912,14 +926,12 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
_oList := _oListCast.([]interface{})
_o := _oList[0].(map[string]interface{})
_settingsList := _settingsListCast.([]interface{})
if len(_settingsList) > 1 {
return fmt.Errorf("At most one settings block is allowed")
}

_settings := _settingsList[0].(map[string]interface{})
settings := &sqladmin.Settings{
Tier: _settings["tier"].(string),
SettingsVersion: instance.Settings.SettingsVersion,
ForceSendFields: []string{"StorageAutoResize"},
}

if v, ok := _settings["activation_policy"]; ok {
Expand Down Expand Up @@ -962,9 +974,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
settings.CrashSafeReplicationEnabled = v.(bool)
}

if v, ok := _settings["disk_autoresize"]; ok && v.(bool) {
settings.StorageAutoResize = v.(bool)
}
settings.StorageAutoResize = _settings["disk_autoresize"].(bool)

if v, ok := _settings["disk_size"]; ok {
if v.(int) > 0 && int64(v.(int)) > instance.Settings.DataDiskSizeGb {
Expand Down