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 import for google_sql_database_instance #11

Merged
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
57 changes: 57 additions & 0 deletions google/import_sql_database_instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package google

import (
"fmt"
"testing"

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

// Test importing a first generation database
func TestAccGoogleSqlDatabaseInstance_importBasic(t *testing.T) {
resourceName := "google_sql_database_instance.instance"
databaseID := acctest.RandInt()

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

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

// Test importing a second generation database
func TestAccGoogleSqlDatabaseInstance_importBasic3(t *testing.T) {
resourceName := "google_sql_database_instance.instance"
databaseID := acctest.RandInt()

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

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
27 changes: 21 additions & 6 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func resourceSqlDatabaseInstance() *schema.Resource {
Read: resourceSqlDatabaseInstanceRead,
Update: resourceSqlDatabaseInstanceUpdate,
Delete: resourceSqlDatabaseInstanceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Expand Down Expand Up @@ -231,6 +234,7 @@ func resourceSqlDatabaseInstance() *schema.Resource {
"master_instance_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

Expand Down Expand Up @@ -521,6 +525,8 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
d.Set("name", instance.Name)
}

d.SetId(instance.Name)

if v, ok := d.GetOk("replica_configuration"); ok {
_replicaConfigurationList := v.([]interface{})

Expand Down Expand Up @@ -631,14 +637,25 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
}

instance, err := config.clientSqlAdmin.Instances.Get(project,
d.Get("name").(string)).Do()
d.Id()).Do()

if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("SQL Database Instance %q", d.Get("name").(string)))
}

_settingsList := d.Get("settings").([]interface{})
_settings := _settingsList[0].(map[string]interface{})
var _settings map[string]interface{}

// If we're importing the settings will be nil
if len(_settingsList) > 0 {
_settings = _settingsList[0].(map[string]interface{})
} else {
_settings = make(map[string]interface{})
}

d.Set("name", instance.Name)
d.Set("region", instance.Region)
d.Set("database_version", instance.DatabaseVersion)

settings := instance.Settings
_settings["version"] = settings.SettingsVersion
Expand Down Expand Up @@ -829,6 +846,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
_settings["replication_type"] = settings.ReplicationType
}

_settingsList = make([]interface{}, 1)
_settingsList[0] = _settings
d.Set("settings", _settingsList)

Expand Down Expand Up @@ -862,10 +880,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
}

d.Set("ip_address", _ipAddresses)

if v, ok := d.GetOk("master_instance_name"); ok && v != nil {
d.Set("master_instance_name", strings.TrimPrefix(instance.MasterInstanceName, project+":"))
}
d.Set("master_instance_name", strings.TrimPrefix(instance.MasterInstanceName, project+":"))

d.Set("self_link", instance.SelfLink)
d.SetId(instance.Name)
Expand Down
2 changes: 1 addition & 1 deletion google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ resource "google_sql_database_instance" "instance" {
var testGoogleSqlDatabaseInstance_basic3 = `
resource "google_sql_database_instance" "instance" {
name = "tf-lw-%d"
region = "us-central"
region = "us-central1"
settings {
tier = "db-f1-micro"
}
Expand Down
10 changes: 9 additions & 1 deletion website/docs/r/sql_database_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ when an Instance can automatically restart to apply updates. It supports:

* `hour` - (Optional) Hour of day (`0-23`), ignored if `day` not set

* `update_track` - (Optional) Receive updates earlier (`canary`) or later
* `update_track` - (Optional) Receive updates earlier (`canary`) or later
(`stable`)

The optional `replica_configuration` block must have `master_instance_name` set
Expand Down Expand Up @@ -194,3 +194,11 @@ exported:

* `settings.version` - Used to make sure changes to the `settings` block are
atomic.

## Import

Database instances can be imported using the `name`, e.g.

```
$ terraform import google_sql_database_instance.master master-instance
```