Skip to content

Commit 6a7da01

Browse files
author
Lars Wander
committed
provider/google: Clarify SQL database name cannot be reused
1 parent aef1353 commit 6a7da01

3 files changed

Lines changed: 50 additions & 5 deletions

File tree

builtin/providers/google/resource_sql_database_instance.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66

7+
"github.com/hashicorp/terraform/helper/resource"
78
"github.com/hashicorp/terraform/helper/schema"
89

910
"google.golang.org/api/googleapi"
@@ -20,7 +21,8 @@ func resourceSqlDatabaseInstance() *schema.Resource {
2021
Schema: map[string]*schema.Schema{
2122
"name": &schema.Schema{
2223
Type: schema.TypeString,
23-
Required: true,
24+
Optional: true,
25+
Computed: true,
2426
ForceNew: true,
2527
},
2628
"master_instance_name": &schema.Schema{
@@ -233,7 +235,6 @@ func resourceSqlDatabaseInstance() *schema.Resource {
233235
func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) error {
234236
config := meta.(*Config)
235237

236-
name := d.Get("name").(string)
237238
region := d.Get("region").(string)
238239
databaseVersion := d.Get("database_version").(string)
239240

@@ -378,12 +379,18 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
378379
}
379380

380381
instance := &sqladmin.DatabaseInstance{
381-
Name: name,
382382
Region: region,
383383
Settings: settings,
384384
DatabaseVersion: databaseVersion,
385385
}
386386

387+
if v, ok := d.GetOk("name"); ok {
388+
instance.Name = v.(string)
389+
} else {
390+
instance.Name = resource.UniqueId()
391+
d.Set("name", instance.Name)
392+
}
393+
387394
if v, ok := d.GetOk("replica_configuration"); ok {
388395
_replicaConfigurationList := v.([]interface{})
389396
if len(_replicaConfigurationList) > 1 {
@@ -446,7 +453,11 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
446453

447454
op, err := config.clientSqlAdmin.Instances.Insert(config.Project, instance).Do()
448455
if err != nil {
449-
return fmt.Errorf("Error, failed to create instance %s: %s", name, err)
456+
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 409 {
457+
return fmt.Errorf("Error, the name %s is unavailable because it was used recently", instance.Name)
458+
} else {
459+
return fmt.Errorf("Error, failed to create instance %s: %s", instance.Name, err)
460+
}
450461
}
451462

452463
err = sqladminOperationWait(config, op, "Create Instance")

builtin/providers/google/resource_sql_database_instance_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ func TestAccGoogleSqlDatabaseInstance_basic(t *testing.T) {
4141
})
4242
}
4343

44+
func TestAccGoogleSqlDatabaseInstance_basic2(t *testing.T) {
45+
var instance sqladmin.DatabaseInstance
46+
47+
resource.Test(t, resource.TestCase{
48+
PreCheck: func() { testAccPreCheck(t) },
49+
Providers: testAccProviders,
50+
CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
51+
Steps: []resource.TestStep{
52+
resource.TestStep{
53+
Config: testGoogleSqlDatabaseInstance_basic2,
54+
Check: resource.ComposeTestCheckFunc(
55+
testAccCheckGoogleSqlDatabaseInstanceExists(
56+
"google_sql_database_instance.instance", &instance),
57+
testAccCheckGoogleSqlDatabaseInstanceEquals(
58+
"google_sql_database_instance.instance", &instance),
59+
),
60+
},
61+
},
62+
})
63+
}
64+
4465
func TestAccGoogleSqlDatabaseInstance_settings_basic(t *testing.T) {
4566
var instance sqladmin.DatabaseInstance
4667
databaseID := genRandInt()
@@ -340,6 +361,16 @@ resource "google_sql_database_instance" "instance" {
340361
}
341362
`
342363

364+
var testGoogleSqlDatabaseInstance_basic2 = `
365+
resource "google_sql_database_instance" "instance" {
366+
region = "us-central"
367+
settings {
368+
tier = "D0"
369+
crash_safe_replication = false
370+
}
371+
}
372+
`
373+
343374
var testGoogleSqlDatabaseInstance_settings = `
344375
resource "google_sql_database_instance" "instance" {
345376
name = "tf-lw-%d"

website/source/docs/providers/google/r/sql_database_instance.html.markdown

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ resource "google_sql_database_instance" "master" {
2828

2929
The following arguments are supported:
3030

31-
* `name` - (Required) The name of the instance.
31+
* `name` - (Optional, Computed) The name of the instance. If the name is left
32+
blank, Terraform will randomly generate one when the instance is first
33+
created. This is done because after a name is used, it cannot be reused
34+
for up to [two months](https://cloud.google.com/sql/docs/delete-instance).
3235

3336
* `region` - (Required) The region the instance will sit in. Note, this does
3437
not line up with the Google Compute Engine (GCE) regions - your options are

0 commit comments

Comments
 (0)