From b80b2a453ebaf78aefc8eaceb5129a67f49d5d43 Mon Sep 17 00:00:00 2001 From: josemanuelt Date: Wed, 30 May 2018 11:54:11 -0500 Subject: [PATCH 1/2] Added user label support in sql_database_instance (#1372) --- google/resource_sql_database_instance.go | 28 +++++++++ google/resource_sql_database_instance_test.go | 57 +++++++++++++++++++ .../r/sql_database_instance.html.markdown | 2 + 3 files changed, 87 insertions(+) diff --git a/google/resource_sql_database_instance.go b/google/resource_sql_database_instance.go index d9897e0fab9..53b4e87ad06 100644 --- a/google/resource_sql_database_instance.go +++ b/google/resource_sql_database_instance.go @@ -230,6 +230,12 @@ func resourceSqlDatabaseInstance() *schema.Resource { Optional: true, Default: "SYNCHRONOUS", }, + "user_labels": &schema.Schema{ + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, }, }, }, @@ -595,6 +601,15 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) settings.ReplicationType = v.(string) } + settings.UserLabels = map[string]string{} + + if v, ok := _settings["user_labels"]; ok { + _userLabels := v.(map[string]interface{}) + for k, v := range _userLabels { + settings.UserLabels[k] = v.(string) + } + } + instance := &sqladmin.DatabaseInstance{ Region: region, Settings: settings, @@ -1027,6 +1042,14 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) settings.ReplicationType = v.(string) } + if v, ok := _settings["user_labels"]; ok { + _userLabels := v.(map[string]interface{}) + + for k, v := range _userLabels { + settings.UserLabels[k] = v.(string) + } + } + instance.Settings = settings } @@ -1079,6 +1102,7 @@ func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} { "disk_size": settings.DataDiskSizeGb, "pricing_plan": settings.PricingPlan, "replication_type": settings.ReplicationType, + "user_labels": settings.UserLabels, } if settings.BackupConfiguration != nil { @@ -1105,6 +1129,10 @@ func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} { data["disk_autoresize"] = *settings.StorageAutoResize } + if settings.UserLabels != nil { + data["user_labels"] = settings.UserLabels + } + return []map[string]interface{}{data} } diff --git a/google/resource_sql_database_instance_test.go b/google/resource_sql_database_instance_test.go index b1e3ae06e4a..5fa5fa56532 100644 --- a/google/resource_sql_database_instance_test.go +++ b/google/resource_sql_database_instance_test.go @@ -521,6 +521,33 @@ func TestAccSqlDatabaseInstance_multipleOperations(t *testing.T) { }) } +func TestAccSqlDatabaseInstance_basic_with_user_labels(t *testing.T) { + t.Parallel() + + var instance sqladmin.DatabaseInstance + databaseID := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccSqlDatabaseInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: fmt.Sprintf( + testGoogleSqlDatabaseInstance_basic_with_user_labels, databaseID), + Check: resource.ComposeTestCheckFunc( + testAccCheckGoogleSqlDatabaseInstanceExists( + "google_sql_database_instance.instance", &instance), + testAccCheckGoogleSqlDatabaseInstanceEquals( + "google_sql_database_instance.instance", &instance), + testAccCheckGoogleSqlDatabaseRootUserDoesNotExist( + &instance), + ), + }, + }, + }) +} + func testAccCheckGoogleSqlDatabaseInstanceEquals(n string, instance *sqladmin.DatabaseInstance) resource.TestCheckFunc { return func(s *terraform.State) error { @@ -693,6 +720,22 @@ func testAccCheckGoogleSqlDatabaseInstanceEquals(n string, return fmt.Errorf("Error settings.pricing_plan mismatch, (%s, %s)", server, local) } + if instance.Settings.UserLabels != nil { + server := instance.Settings.UserLabels["location"] + local = attributes["settings.0.user_labels.location"] + + if server != local { + return fmt.Errorf("Error settings.user_labels.location mismatch, (%s, %s)", server, local) + } + + server = instance.Settings.UserLabels["track"] + local = attributes["settings.0.user_labels.track"] + + if server != local { + return fmt.Errorf("Error settings.user_labels.track mismatch, (%s, %s)", server, local) + } + } + if instance.ReplicaConfiguration != nil { server = strconv.FormatBool(instance.ReplicaConfiguration.FailoverTarget) local = attributes["replica_configuration.0.failover_target"] @@ -1064,3 +1107,17 @@ resource "google_sql_user" "user" { password = "hunter2" } ` + +var testGoogleSqlDatabaseInstance_basic_with_user_labels = ` +resource "google_sql_database_instance" "instance" { + name = "tf-lw-%d" + region = "us-central1" + settings { + tier = "db-f1-micro" + user_labels { + track = "production" + location = "western-division" + } + } +} +` diff --git a/website/docs/r/sql_database_instance.html.markdown b/website/docs/r/sql_database_instance.html.markdown index 46dfa298b5f..1067f81937e 100644 --- a/website/docs/r/sql_database_instance.html.markdown +++ b/website/docs/r/sql_database_instance.html.markdown @@ -125,6 +125,8 @@ The required `settings` block supports: * `replication_type` - (Optional) Replication type for this instance, can be one of `ASYNCHRONOUS` or `SYNCHRONOUS`. +* `user_labels` - (Optional) A set of key/value user label pairs to assign to the instance. + The optional `settings.database_flags` sublist supports: * `name` - (Optional) Name of the flag. From 6b07052385711d373a65f9bf65907d5f70f6a295 Mon Sep 17 00:00:00 2001 From: josemanuelt Date: Wed, 30 May 2018 17:11:16 -0500 Subject: [PATCH 2/2] Fixes #1372 --- google/resource_sql_database_instance.go | 13 ++--------- google/resource_sql_database_instance_test.go | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/google/resource_sql_database_instance.go b/google/resource_sql_database_instance.go index 53b4e87ad06..f722c882d0c 100644 --- a/google/resource_sql_database_instance.go +++ b/google/resource_sql_database_instance.go @@ -601,13 +601,8 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) settings.ReplicationType = v.(string) } - settings.UserLabels = map[string]string{} - if v, ok := _settings["user_labels"]; ok { - _userLabels := v.(map[string]interface{}) - for k, v := range _userLabels { - settings.UserLabels[k] = v.(string) - } + settings.UserLabels = convertStringMap(v.(map[string]interface{})) } instance := &sqladmin.DatabaseInstance{ @@ -1043,11 +1038,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) } if v, ok := _settings["user_labels"]; ok { - _userLabels := v.(map[string]interface{}) - - for k, v := range _userLabels { - settings.UserLabels[k] = v.(string) - } + settings.UserLabels = convertStringMap(v.(map[string]interface{})) } instance.Settings = settings diff --git a/google/resource_sql_database_instance_test.go b/google/resource_sql_database_instance_test.go index 5fa5fa56532..a15e2b13b75 100644 --- a/google/resource_sql_database_instance_test.go +++ b/google/resource_sql_database_instance_test.go @@ -544,6 +544,16 @@ func TestAccSqlDatabaseInstance_basic_with_user_labels(t *testing.T) { &instance), ), }, + resource.TestStep{ + Config: fmt.Sprintf( + testGoogleSqlDatabaseInstance_basic_with_user_labels_update, databaseID), + Check: resource.ComposeTestCheckFunc( + testAccCheckGoogleSqlDatabaseInstanceExists( + "google_sql_database_instance.instance", &instance), + testAccCheckGoogleSqlDatabaseInstanceEquals( + "google_sql_database_instance.instance", &instance), + ), + }, }, }) } @@ -1121,3 +1131,15 @@ resource "google_sql_database_instance" "instance" { } } ` +var testGoogleSqlDatabaseInstance_basic_with_user_labels_update = ` +resource "google_sql_database_instance" "instance" { + name = "tf-lw-%d" + region = "us-central1" + settings { + tier = "db-f1-micro" + user_labels { + track = "production" + } + } +} +`