From 19afa06a5ddba1ed7164308f899ed97aa95235eb Mon Sep 17 00:00:00 2001 From: clint shryock Date: Wed, 17 May 2017 12:04:36 -0500 Subject: [PATCH 1/3] provider/google: Fix server/state diff with disk_autoresize --- .../google/resource_sql_database_instance.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/builtin/providers/google/resource_sql_database_instance.go b/builtin/providers/google/resource_sql_database_instance.go index e946e6267cb8..b6c6d8352694 100644 --- a/builtin/providers/google/resource_sql_database_instance.go +++ b/builtin/providers/google/resource_sql_database_instance.go @@ -90,6 +90,7 @@ func resourceSqlDatabaseInstance() *schema.Resource { }, "disk_autoresize": &schema.Schema{ Type: schema.TypeBool, + Default: false, Optional: true, }, "disk_size": &schema.Schema{ @@ -320,7 +321,8 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) _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 +365,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 +662,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)) { @@ -920,6 +916,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) settings := &sqladmin.Settings{ Tier: _settings["tier"].(string), SettingsVersion: instance.Settings.SettingsVersion, + ForceSendFields: []string{"StorageAutoResize"}, } if v, ok := _settings["activation_policy"]; ok { @@ -962,9 +959,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 { From 0bf6ba102a02f9c8f4593ccd61c726dbfc991120 Mon Sep 17 00:00:00 2001 From: clint shryock Date: Thu, 18 May 2017 12:19:00 -0500 Subject: [PATCH 2/3] 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 --- .../google/resource_sql_database_instance.go | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/builtin/providers/google/resource_sql_database_instance.go b/builtin/providers/google/resource_sql_database_instance.go index b6c6d8352694..73ee5c4dc6be 100644 --- a/builtin/providers/google/resource_sql_database_instance.go +++ b/builtin/providers/google/resource_sql_database_instance.go @@ -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,9 +92,10 @@ func resourceSqlDatabaseInstance() *schema.Resource { }, }, "disk_autoresize": &schema.Schema{ - Type: schema.TypeBool, - Default: false, - Optional: true, + Type: schema.TypeBool, + Default: true, + Optional: true, + DiffSuppressFunc: suppressFirstGen, }, "disk_size": &schema.Schema{ Type: schema.TypeInt, @@ -303,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 data.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) @@ -315,9 +336,6 @@ 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{ @@ -908,9 +926,6 @@ 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{ From 962ada837f96d1181c8251c6bb75079bd61504da Mon Sep 17 00:00:00 2001 From: clint shryock Date: Thu, 18 May 2017 15:07:55 -0500 Subject: [PATCH 3/3] fix typos --- builtin/providers/google/resource_sql_database_instance.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builtin/providers/google/resource_sql_database_instance.go b/builtin/providers/google/resource_sql_database_instance.go index 73ee5c4dc6be..522bbbb96744 100644 --- a/builtin/providers/google/resource_sql_database_instance.go +++ b/builtin/providers/google/resource_sql_database_instance.go @@ -307,7 +307,7 @@ func resourceSqlDatabaseInstance() *schema.Resource { } } -// Suppress diff with any disk.autoresize value on 1st Generation Instances +// 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{}) @@ -315,10 +315,10 @@ func suppressFirstGen(k, old, new string, d *schema.ResourceData) bool { tier := settings["tier"].(string) matched, err := regexp.MatchString("db*", tier) if err != nil { - log.Printf("[ERR] error with regex in diff supression for data.autoresize: %s", err) + 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") + log.Printf("[DEBUG] suppressing diff on disk_autoresize due to 1st gen instance type") return true } return false