Skip to content

Commit 24e27cc

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 24e27cc

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-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

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

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

@@ -910,6 +942,25 @@ resource "google_sql_database_instance" "instance_slave" {
910942
}
911943
`
912944

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

0 commit comments

Comments
 (0)