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 ability to import google_sql_database #12

Merged
merged 3 commits into from
Jun 22, 2017
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
31 changes: 31 additions & 0 deletions google/import_sql_database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccGoogleSqlDatabase_importBasic(t *testing.T) {
resourceName := "google_sql_database.database"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(
testGoogleSqlDatabase_basic, acctest.RandString(10), acctest.RandString(10)),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
18 changes: 16 additions & 2 deletions google/resource_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"

Expand All @@ -13,6 +14,9 @@ func resourceSqlDatabase() *schema.Resource {
Create: resourceSqlDatabaseCreate,
Read: resourceSqlDatabaseRead,
Delete: resourceSqlDatabaseDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Expand Down Expand Up @@ -51,6 +55,7 @@ func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {

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

db := &sqladmin.Database{
Name: database_name,
Expand Down Expand Up @@ -86,8 +91,15 @@ func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
return err
}

database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)
s := strings.Split(d.Id(), ":")

if len(s) != 2 {
return fmt.Errorf("Error, failure importing database %s. "+
"ID format is instance:name", d.Id())
}

instance_name := s[0]
database_name := s[1]

db, err := config.clientSqlAdmin.Databases.Get(project, instance_name,
database_name).Do()
Expand All @@ -96,6 +108,8 @@ func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
return handleNotFoundError(err, d, fmt.Sprintf("SQL Database %q in instance %q", database_name, instance_name))
}

d.Set("instance", db.Instance)
d.Set("name", db.Name)
d.Set("self_link", db.SelfLink)
d.SetId(instance_name + ":" + database_name)

Expand Down
7 changes: 4 additions & 3 deletions google/resource_sql_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestAccGoogleSqlDatabase_basic(t *testing.T) {
CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testGoogleSqlDatabase_basic,
Config: fmt.Sprintf(
testGoogleSqlDatabase_basic, acctest.RandString(10), acctest.RandString(10)),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleSqlDatabaseExists(
"google_sql_database.database", &database),
Expand Down Expand Up @@ -99,7 +100,7 @@ func testAccGoogleSqlDatabaseDestroy(s *terraform.State) error {
return nil
}

var testGoogleSqlDatabase_basic = fmt.Sprintf(`
var testGoogleSqlDatabase_basic = `
resource "google_sql_database_instance" "instance" {
name = "sqldatabasetest%s"
region = "us-central"
Expand All @@ -112,4 +113,4 @@ resource "google_sql_database" "database" {
name = "sqldatabasetest%s"
instance = "${google_sql_database_instance.instance.name}"
}
`, acctest.RandString(10), acctest.RandString(10))
`
10 changes: 9 additions & 1 deletion website/docs/r/sql_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "google_sql_database_instance" "master" {
}
resource "google_sql_database" "users" {
name = "users"
name = "users-db"
instance = "${google_sql_database_instance.master.name}"
}
```
Expand All @@ -48,3 +48,11 @@ In addition to the arguments listed above, the following computed attributes are
exported:

* `self_link` - The URI of the created resource.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you mind subbing "users-db" for "image-store-bucket" above in the example? (Github doesn't like comments on unchanged lines)

## Import

SQL databases can be imported using the `instance` and `name`, e.g.

```
$ terraform import google_sql_database.database master-instance:users-db
```