Skip to content
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

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
Expand Down Expand Up @@ -595,6 +601,10 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
settings.ReplicationType = v.(string)
}

if v, ok := _settings["user_labels"]; ok {
settings.UserLabels = convertStringMap(v.(map[string]interface{}))
}

instance := &sqladmin.DatabaseInstance{
Region: region,
Settings: settings,
Expand Down Expand Up @@ -1027,6 +1037,10 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
settings.ReplicationType = v.(string)
}

if v, ok := _settings["user_labels"]; ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, user convertStringMap

settings.UserLabels = convertStringMap(v.(map[string]interface{}))
}

instance.Settings = settings
}

Expand Down Expand Up @@ -1079,6 +1093,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 {
Expand All @@ -1105,6 +1120,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}
}

Expand Down
79 changes: 79 additions & 0 deletions google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,43 @@ 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(
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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),
),
},
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),
),
},
},
})
}

func testAccCheckGoogleSqlDatabaseInstanceEquals(n string,
instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -693,6 +730,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"]
Expand Down Expand Up @@ -1064,3 +1117,29 @@ 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"
}
}
}
`
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"
}
}
}
`
2 changes: 2 additions & 0 deletions website/docs/r/sql_database_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down