Skip to content

Commit

Permalink
Merge pull request #5723 from hashicorp/phinze/rds-instance-generate-…
Browse files Browse the repository at this point in the history
…identifier

provider/aws: ability to generate unique RDS identifier
  • Loading branch information
phinze committed Mar 18, 2016
2 parents b9620e9 + 4e5429a commit d60bf93
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
18 changes: 16 additions & 2 deletions builtin/providers/aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func resourceAwsDbInstance() *schema.Resource {

"identifier": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validateRdsId,
},
Expand Down Expand Up @@ -291,12 +292,25 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
conn := meta.(*AWSClient).rdsconn
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))

identifier := d.Get("identifier").(string)
// Generate a unique ID for the user
if identifier == "" {
identifier = resource.PrefixedUniqueId("tf-")
// SQL Server identifier size is max 15 chars, so truncate
if engine := d.Get("engine").(string); engine != "" {
if strings.Contains(strings.ToLower(engine), "sqlserver") {
identifier = identifier[:15]
}
}
d.Set("identifier", identifier)
}

if v, ok := d.GetOk("replicate_source_db"); ok {
opts := rds.CreateDBInstanceReadReplicaInput{
SourceDBInstanceIdentifier: aws.String(v.(string)),
CopyTagsToSnapshot: aws.Bool(d.Get("copy_tags_to_snapshot").(bool)),
DBInstanceClass: aws.String(d.Get("instance_class").(string)),
DBInstanceIdentifier: aws.String(d.Get("identifier").(string)),
DBInstanceIdentifier: aws.String(identifier),
Tags: tags,
}
if attr, ok := d.GetOk("iops"); ok {
Expand Down
6 changes: 2 additions & 4 deletions builtin/providers/aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,8 @@ func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestC
// Database names cannot collide, and deletion takes so long, that making the
// name a bit random helps so able we can kill a test that's just waiting for a
// delete and not be blocked on kicking off another one.
var testAccAWSDBInstanceConfig = fmt.Sprintf(`
var testAccAWSDBInstanceConfig = `
resource "aws_db_instance" "bar" {
identifier = "foobarbaz-test-terraform-%d"
allocated_storage = 10
engine = "MySQL"
engine_version = "5.6.21"
Expand All @@ -383,7 +381,7 @@ resource "aws_db_instance" "bar" {
backup_retention_period = 0
parameter_group_name = "default.mysql5.6"
}`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
}`

func testAccReplicaInstanceConfig(val int) string {
return fmt.Sprintf(`
Expand Down
21 changes: 10 additions & 11 deletions website/source/docs/providers/aws/r/db_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ for more information.

```
resource "aws_db_instance" "default" {
identifier = "mydb-rds"
allocated_storage = 10
engine = "mysql"
engine_version = "5.6.17"
instance_class = "db.t1.micro"
name = "mydb"
username = "foo"
password = "bar"
db_subnet_group_name = "my_database_subnet_group"
parameter_group_name = "default.mysql5.6"
allocated_storage = 10
engine = "mysql"
engine_version = "5.6.17"
instance_class = "db.t1.micro"
name = "mydb"
username = "foo"
password = "bar"
db_subnet_group_name = "my_database_subnet_group"
parameter_group_name = "default.mysql5.6"
}
```

Expand All @@ -51,7 +50,7 @@ The following arguments are supported:
* `allocated_storage` - (Required unless a `snapshot_identifier` or `replicate_source_db` is provided) The allocated storage in gigabytes.
* `engine` - (Required unless a `snapshot_identifier` or `replicate_source_db` is provided) The database engine to use.
* `engine_version` - (Optional) The engine version to use.
* `identifier` - (Required) The name of the RDS instance
* `identifier` - (Optional) The name of the RDS instance, if omitted, Terraform will assign a random, unique name
* `instance_class` - (Required) The instance type of the RDS instance.
* `storage_type` - (Optional) One of "standard" (magnetic), "gp2" (general
purpose SSD), or "io1" (provisioned IOPS SSD). The default is "io1" if
Expand Down

0 comments on commit d60bf93

Please sign in to comment.