provider/aws: ability to generate unique RDS identifier

Needed to truncate the identifier for SQL Server engines to keep it at
max 15 chars per the docs. Not a full UUID going into it, but should be
"unique enough" to not matter in practice.

Modified the basic test to use the generated value. Other tests are
still working w/ explicitly specified identifiers.
This commit is contained in:
Paul Hinze 2016-03-18 15:15:30 -05:00
parent 6bba27c509
commit 4e5429a140
3 changed files with 28 additions and 17 deletions

View File

@ -85,7 +85,8 @@ func resourceAwsDbInstance() *schema.Resource {
"identifier": &schema.Schema{ "identifier": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
Computed: true,
ForceNew: true, ForceNew: true,
ValidateFunc: validateRdsId, ValidateFunc: validateRdsId,
}, },
@ -291,12 +292,25 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
conn := meta.(*AWSClient).rdsconn conn := meta.(*AWSClient).rdsconn
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{})) 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 { if v, ok := d.GetOk("replicate_source_db"); ok {
opts := rds.CreateDBInstanceReadReplicaInput{ opts := rds.CreateDBInstanceReadReplicaInput{
SourceDBInstanceIdentifier: aws.String(v.(string)), SourceDBInstanceIdentifier: aws.String(v.(string)),
CopyTagsToSnapshot: aws.Bool(d.Get("copy_tags_to_snapshot").(bool)), CopyTagsToSnapshot: aws.Bool(d.Get("copy_tags_to_snapshot").(bool)),
DBInstanceClass: aws.String(d.Get("instance_class").(string)), DBInstanceClass: aws.String(d.Get("instance_class").(string)),
DBInstanceIdentifier: aws.String(d.Get("identifier").(string)), DBInstanceIdentifier: aws.String(identifier),
Tags: tags, Tags: tags,
} }
if attr, ok := d.GetOk("iops"); ok { if attr, ok := d.GetOk("iops"); ok {

View File

@ -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 // 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 // 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. // delete and not be blocked on kicking off another one.
var testAccAWSDBInstanceConfig = fmt.Sprintf(` var testAccAWSDBInstanceConfig = `
resource "aws_db_instance" "bar" { resource "aws_db_instance" "bar" {
identifier = "foobarbaz-test-terraform-%d"
allocated_storage = 10 allocated_storage = 10
engine = "MySQL" engine = "MySQL"
engine_version = "5.6.21" engine_version = "5.6.21"
@ -383,7 +381,7 @@ resource "aws_db_instance" "bar" {
backup_retention_period = 0 backup_retention_period = 0
parameter_group_name = "default.mysql5.6" parameter_group_name = "default.mysql5.6"
}`, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) }`
func testAccReplicaInstanceConfig(val int) string { func testAccReplicaInstanceConfig(val int) string {
return fmt.Sprintf(` return fmt.Sprintf(`

View File

@ -28,16 +28,15 @@ for more information.
``` ```
resource "aws_db_instance" "default" { resource "aws_db_instance" "default" {
identifier = "mydb-rds" allocated_storage = 10
allocated_storage = 10 engine = "mysql"
engine = "mysql" engine_version = "5.6.17"
engine_version = "5.6.17" instance_class = "db.t1.micro"
instance_class = "db.t1.micro" name = "mydb"
name = "mydb" username = "foo"
username = "foo" password = "bar"
password = "bar" db_subnet_group_name = "my_database_subnet_group"
db_subnet_group_name = "my_database_subnet_group" parameter_group_name = "default.mysql5.6"
parameter_group_name = "default.mysql5.6"
} }
``` ```
@ -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. * `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` - (Required unless a `snapshot_identifier` or `replicate_source_db` is provided) The database engine to use.
* `engine_version` - (Optional) The engine version 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. * `instance_class` - (Required) The instance type of the RDS instance.
* `storage_type` - (Optional) One of "standard" (magnetic), "gp2" (general * `storage_type` - (Optional) One of "standard" (magnetic), "gp2" (general
purpose SSD), or "io1" (provisioned IOPS SSD). The default is "io1" if purpose SSD), or "io1" (provisioned IOPS SSD). The default is "io1" if