Skip to content

Commit

Permalink
Add ability to import a database instance
Browse files Browse the repository at this point in the history
  • Loading branch information
gh-mlfowler committed May 15, 2017
1 parent ecd0145 commit 2867bf3
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -71,18 +74,7 @@ func resourceSqlDatabaseInstance() *schema.Resource {
"crash_safe_replication": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"disk_size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"disk_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"disk_autoresize": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"database_flags": &schema.Schema{
Type: schema.TypeList,
Expand All @@ -100,6 +92,18 @@ func resourceSqlDatabaseInstance() *schema.Resource {
},
},
},
"disk_autoresize": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"disk_size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"disk_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"ip_configuration": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -222,6 +226,7 @@ func resourceSqlDatabaseInstance() *schema.Resource {
"master_instance_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

Expand Down Expand Up @@ -579,6 +584,11 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
return err
}

// In the import case this will be set
if _, ok := d.GetOk("name"); !ok {
d.Set("name", d.Id())
}

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

Expand All @@ -596,7 +606,16 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
}

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

if len(_settingsList) > 0 {
_settings = _settingsList[0].(map[string]interface{})
} else {
_settingsList = make([]interface{}, 1)
_settings = make(map[string]interface{})
d.Set("region", instance.Region)
d.Set("database_version", instance.DatabaseVersion)
}

settings := instance.Settings
_settings["version"] = settings.SettingsVersion
Expand Down Expand Up @@ -884,10 +903,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

0 comments on commit 2867bf3

Please sign in to comment.