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

Add charset and collation to google_sql_database. #183

Merged
merged 8 commits into from
Jul 17, 2017
60 changes: 58 additions & 2 deletions google/resource_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func resourceSqlDatabase() *schema.Resource {
return &schema.Resource{
Create: resourceSqlDatabaseCreate,
Read: resourceSqlDatabaseRead,
Update: resourceSqlDatabaseUpdate,
Delete: resourceSqlDatabaseDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -41,6 +42,18 @@ func resourceSqlDatabase() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"charset": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "utf8",
},

"collation": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "utf8_general_ci",
},
},
}
}
Expand All @@ -58,8 +71,10 @@ func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(instance_name + ":" + database_name)

db := &sqladmin.Database{
Name: database_name,
Instance: instance_name,
Name: database_name,
Instance: instance_name,
Charset: d.Get("charset").(string),
Collation: d.Get("collation").(string),
}

mutexKV.Lock(instanceMutexKey(project, instance_name))
Expand Down Expand Up @@ -112,10 +127,51 @@ func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
d.Set("name", db.Name)
d.Set("self_link", db.SelfLink)
d.SetId(instance_name + ":" + database_name)
d.Set("charset", db.Charset)
d.Set("collation", db.Collation)

return nil
}

func resourceSqlDatabaseUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)

db := &sqladmin.Database{
Name: database_name,
Instance: instance_name,
Charset: d.Get("charset").(string),
Collation: d.Get("collation").(string),
}

mutexKV.Lock(instanceMutexKey(project, instance_name))
defer mutexKV.Unlock(instanceMutexKey(project, instance_name))
op, err := config.clientSqlAdmin.Databases.Update(project, instance_name, database_name,
db).Do()

if err != nil {
return fmt.Errorf("Error, failed to update "+
"database %s in instance %s: %s", database_name,
instance_name, err)
}

err = sqladminOperationWait(config, op, "Update Database")

if err != nil {
return fmt.Errorf("Error, failure waiting for update of %s "+
"into %s: %s", database_name, instance_name, err)
}

return resourceSqlDatabaseRead(d, meta)
}

func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand Down
61 changes: 61 additions & 0 deletions google/resource_sql_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,41 @@ func TestAccGoogleSqlDatabase_basic(t *testing.T) {
})
}

func TestAccGoogleSqlDatabase_update(t *testing.T) {
var database sqladmin.Database

instance_name := acctest.RandString(10)
database_name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(
testGoogleSqlDatabase_basic, instance_name, database_name),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleSqlDatabaseExists(
"google_sql_database.database", &database),
testAccCheckGoogleSqlDatabaseEquals(
"google_sql_database.database", &database),
),
},
resource.TestStep{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's split the charset and collation parts into their own test and leave _basic as a single-step test; that way, we can keep the testing for Update out of _basic.

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.

Config: fmt.Sprintf(
testGoogleSqlDatabase_latin1, instance_name, database_name),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleSqlDatabaseExists(
"google_sql_database.database", &database),
testAccCheckGoogleSqlDatabaseEquals(
"google_sql_database.database", &database),
),
},
},
})
}

func testAccCheckGoogleSqlDatabaseEquals(n string,
database *sqladmin.Database) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand All @@ -43,6 +78,8 @@ func testAccCheckGoogleSqlDatabaseEquals(n string,

database_name := rs.Primary.Attributes["name"]
instance_name := rs.Primary.Attributes["instance"]
charset := rs.Primary.Attributes["charset"]
collation := rs.Primary.Attributes["collation"]

if database_name != database.Name {
return fmt.Errorf("Error name mismatch, (%s, %s)", database_name, database.Name)
Expand All @@ -52,6 +89,14 @@ func testAccCheckGoogleSqlDatabaseEquals(n string,
return fmt.Errorf("Error instance_name mismatch, (%s, %s)", instance_name, database.Instance)
}

if charset != database.Charset {
return fmt.Errorf("Error charset mismatch, (%s, %s)", charset, database.Charset)
}

if collation != database.Collation {
return fmt.Errorf("Error collation mismatch, (%s, %s)", collation, database.Collation)
}

return nil
}
}
Expand Down Expand Up @@ -114,3 +159,19 @@ resource "google_sql_database" "database" {
instance = "${google_sql_database_instance.instance.name}"
}
`
var testGoogleSqlDatabase_latin1 = `
resource "google_sql_database_instance" "instance" {
name = "sqldatabasetest%s"
region = "us-central"
settings {
tier = "D0"
}
}

resource "google_sql_database" "database" {
name = "sqldatabasetest%s"
instance = "${google_sql_database_instance.instance.name}"
charset = "latin1"
collation = "latin1_swedish_ci"
}
`
11 changes: 9 additions & 2 deletions website/docs/r/sql_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ resource "google_sql_database_instance" "master" {
}

resource "google_sql_database" "users" {
name = "users-db"
instance = "${google_sql_database_instance.master.name}"
name = "users-db"
instance = "${google_sql_database_instance.master.name}"
charset = "latin1"
collation = "latin1_swedish_ci"
}
```

Expand All @@ -42,6 +44,11 @@ The following arguments are supported:
* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

* `charset` - (Optional) The MySQL charset value (default "utf8").

* `collation` - (Optional) The MySQL collation value (default
"utf8_general_ci").

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down