Skip to content

Commit 570a996

Browse files
committed
Test setting AvailabilityType for PostgreSQL
Add tests that cover the creation of a Postgres database with AvailabilityType set to REGIONAL, and correct some small issues that were preventing compilation.
1 parent 784334f commit 570a996

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

google/resource_sql_database_instance.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ func resourceSqlDatabaseInstance() *schema.Resource {
7777
Type: schema.TypeString,
7878
Optional: true,
7979
DiffSuppressFunc: suppressFirstGen,
80-
// Set computed instead of default because this property is for second-gen only.
81-
Computed: true,
80+
// Set computed instead of default because this property is for second-gen
81+
// only. The default when not provided is ZONAL, which means no explicit HA
82+
// configuration.
83+
Computed: true,
84+
ValidateFunc: validation.StringInSlice([]string{"REGIONAL", "ZONAL"}, false),
8285
},
8386
"backup_configuration": &schema.Schema{
8487
Type: schema.TypeList,
@@ -350,18 +353,19 @@ func resourceSqlDatabaseInstance() *schema.Resource {
350353
}
351354
}
352355

353-
// Suppress diff with any disk_autoresize value on 1st Generation Instances
356+
// Suppress diff with any attribute value that is not supported on 1st Generation
357+
// Instances
354358
func suppressFirstGen(k, old, new string, d *schema.ResourceData) bool {
355359
settingsList := d.Get("settings").([]interface{})
356360

357361
settings := settingsList[0].(map[string]interface{})
358362
tier := settings["tier"].(string)
359363
matched, err := regexp.MatchString("db*", tier)
360364
if err != nil {
361-
log.Printf("[ERR] error with regex in diff supression for disk_autoresize: %s", err)
365+
log.Printf("[ERR] error with regex in diff supression for %s: %s", k, err)
362366
}
363367
if !matched {
364-
log.Printf("[DEBUG] suppressing diff on disk_autoresize due to 1st gen instance type")
368+
log.Printf("[DEBUG] suppressing diff on %s due to 1st gen instance type", k)
365369
return true
366370
}
367371
return false
@@ -427,7 +431,8 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
427431
settings.CrashSafeReplicationEnabled = v.(bool)
428432
}
429433

430-
settings.StorageAutoResize = _settings["disk_autoresize"].(*bool)
434+
autoResize := _settings["disk_autoresize"].(bool)
435+
settings.StorageAutoResize = &autoResize
431436

432437
if v, ok := _settings["disk_size"]; ok && v.(int) > 0 {
433438
settings.DataDiskSizeGb = int64(v.(int))
@@ -729,10 +734,12 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
729734
_settingsList := _settingsListCast.([]interface{})
730735

731736
_settings := _settingsList[0].(map[string]interface{})
737+
_autoResize := _settings["disk_autoresize"].(bool)
738+
732739
settings := &sqladmin.Settings{
733740
Tier: _settings["tier"].(string),
734741
SettingsVersion: instance.Settings.SettingsVersion,
735-
StorageAutoResize: _settings["disk_autoresize"].(*bool),
742+
StorageAutoResize: &_autoResize,
736743
ForceSendFields: []string{"StorageAutoResize"},
737744
}
738745

google/resource_sql_database_instance_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,40 @@ func TestAccGoogleSqlDatabaseInstance_slave(t *testing.T) {
318318
})
319319
}
320320

321+
func TestAccGoogleSqlDatabaseInstance_highAvailability(t *testing.T) {
322+
t.Parallel()
323+
324+
var instance sqladmin.DatabaseInstance
325+
instanceID := acctest.RandInt()
326+
327+
resource.Test(t, resource.TestCase{
328+
PreCheck: func() { testAccPreCheck(t) },
329+
Providers: testAccProviders,
330+
CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
331+
Steps: []resource.TestStep{
332+
resource.TestStep{
333+
Config: fmt.Sprintf(
334+
testGoogleSqlDatabaseInstance_highAvailability, instanceID),
335+
Check: resource.ComposeTestCheckFunc(
336+
testAccCheckGoogleSqlDatabaseInstanceExists(
337+
"google_sql_database_instance.instance", &instance),
338+
testAccCheckGoogleSqlDatabaseInstanceEquals(
339+
"google_sql_database_instance.instance", &instance),
340+
// Check that we've set our high availability type correctly, and it's been
341+
// accepted by the API
342+
func(s *terraform.State) error {
343+
if instance.Settings.AvailabilityType != "REGIONAL" {
344+
return fmt.Errorf("Database %s was not configured with Regional HA", instance.Name)
345+
}
346+
347+
return nil
348+
},
349+
),
350+
},
351+
},
352+
})
353+
}
354+
321355
func TestAccGoogleSqlDatabaseInstance_diskspecs(t *testing.T) {
322356
t.Parallel()
323357

@@ -910,6 +944,25 @@ resource "google_sql_database_instance" "instance_slave" {
910944
}
911945
`
912946

947+
var testGoogleSqlDatabaseInstance_highAvailability = `
948+
resource "google_sql_database_instance" "instance" {
949+
name = "tf-lw-%d"
950+
region = "us-central1"
951+
database_version = "POSTGRES_9_6"
952+
953+
settings {
954+
tier = "db-f1-micro"
955+
956+
availability_type = "REGIONAL"
957+
958+
backup_configuration {
959+
enabled = true
960+
binary_log_enabled = true
961+
}
962+
}
963+
}
964+
`
965+
913966
var testGoogleSqlDatabaseInstance_diskspecs = `
914967
resource "google_sql_database_instance" "instance" {
915968
name = "tf-lw-%d"

0 commit comments

Comments
 (0)