Fix issue with GCP Cloud SQL Instance `disk_autoresize` (#14582)

* provider/google: Fix server/state diff with disk_autoresize

* provider/google: Default true for disk.auto_resize

For sql_database_instance , to match the new API default.

Also adds diff suppression func for autoresize on 1st gen instances

* fix typos
This commit is contained in:
Clint 2017-05-18 15:09:01 -05:00 committed by GitHub
parent e7dcf95fa1
commit c3d2f6bbe4
1 changed files with 30 additions and 20 deletions

View File

@ -2,6 +2,8 @@ package google
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/terraform/helper/resource"
@ -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{
@ -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,
@ -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)
@ -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 {
@ -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))
@ -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)) {
@ -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 {
@ -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 {