-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added user label support in sql_database_instance #1567
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, user convertStringMap |
||
_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} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a new step in this test to test the update path for labels? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
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" | ||
} | ||
} | ||
} | ||
` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use convertStringMap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done