From 78963fc3d97642bf0c5a200639bdb29fd1e8ae8a Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 1 Apr 2015 09:13:41 -0500 Subject: [PATCH] providers/aws: fix/improve RDS pointers handling * d.Set has a pointer nil check we can lean on * need to be a bit more conservative about nil checks on nested structs; (this fixes the RDS acceptance tests) /cc @fanhaf --- .../providers/aws/resource_aws_db_instance.go | 49 ++++++++++--------- .../aws/resource_aws_db_instance_test.go | 14 +++--- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/builtin/providers/aws/resource_aws_db_instance.go b/builtin/providers/aws/resource_aws_db_instance.go index 11b05c81b..9bcf21898 100644 --- a/builtin/providers/aws/resource_aws_db_instance.go +++ b/builtin/providers/aws/resource_aws_db_instance.go @@ -304,33 +304,38 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { return nil } - if v.DBName != nil { - d.Set("name", *v.DBName) - } else { - d.Set("name", "") + d.Set("name", v.DBName) + d.Set("username", v.MasterUsername) + d.Set("engine", v.Engine) + d.Set("engine_version", v.EngineVersion) + d.Set("allocated_storage", v.AllocatedStorage) + d.Set("storage_type", v.StorageType) + d.Set("instance_class", v.DBInstanceClass) + d.Set("availability_zone", v.AvailabilityZone) + d.Set("backup_retention_period", v.BackupRetentionPeriod) + d.Set("backup_window", v.PreferredBackupWindow) + d.Set("maintenance_window", v.PreferredMaintenanceWindow) + d.Set("multi_az", v.MultiAZ) + if v.DBSubnetGroup != nil { + d.Set("db_subnet_group_name", v.DBSubnetGroup.DBSubnetGroupName) } - d.Set("username", *v.MasterUsername) - d.Set("engine", *v.Engine) - d.Set("engine_version", *v.EngineVersion) - d.Set("allocated_storage", *v.AllocatedStorage) - d.Set("storage_type", *v.StorageType) - d.Set("instance_class", *v.DBInstanceClass) - d.Set("availability_zone", *v.AvailabilityZone) - d.Set("backup_retention_period", *v.BackupRetentionPeriod) - d.Set("backup_window", *v.PreferredBackupWindow) - d.Set("maintenance_window", *v.PreferredMaintenanceWindow) - d.Set("multi_az", *v.MultiAZ) - d.Set("port", *v.Endpoint.Port) - d.Set("db_subnet_group_name", *v.DBSubnetGroup.DBSubnetGroupName) if len(v.DBParameterGroups) > 0 { - d.Set("parameter_group_name", *v.DBParameterGroups[0].DBParameterGroupName) + d.Set("parameter_group_name", v.DBParameterGroups[0].DBParameterGroupName) } - d.Set("address", *v.Endpoint.Address) - d.Set("endpoint", fmt.Sprintf("%s:%d", *v.Endpoint.Address, *v.Endpoint.Port)) - d.Set("status", *v.DBInstanceStatus) - d.Set("storage_encrypted", *v.StorageEncrypted) + if v.Endpoint != nil { + d.Set("port", v.Endpoint.Port) + d.Set("address", v.Endpoint.Address) + + if v.Endpoint.Address != nil && v.Endpoint.Port != nil { + d.Set("endpoint", + fmt.Sprintf("%s:%d", *v.Endpoint.Address, *v.Endpoint.Port)) + } + } + + d.Set("status", v.DBInstanceStatus) + d.Set("storage_encrypted", v.StorageEncrypted) // Create an empty schema.Set to hold all vpc security group ids ids := &schema.Set{ diff --git a/builtin/providers/aws/resource_aws_db_instance_test.go b/builtin/providers/aws/resource_aws_db_instance_test.go index 3141990e6..ba86d005a 100644 --- a/builtin/providers/aws/resource_aws_db_instance_test.go +++ b/builtin/providers/aws/resource_aws_db_instance_test.go @@ -2,7 +2,9 @@ package aws import ( "fmt" + "math/rand" "testing" + "time" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" @@ -24,8 +26,6 @@ func TestAccAWSDBInstance(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), testAccCheckAWSDBInstanceAttributes(&v), - resource.TestCheckResourceAttr( - "aws_db_instance.bar", "identifier", "foobarbaz-test-terraform"), resource.TestCheckResourceAttr( "aws_db_instance.bar", "allocated_storage", "10"), resource.TestCheckResourceAttr( @@ -133,9 +133,12 @@ func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestC } } -const testAccAWSDBInstanceConfig = ` +// 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(` resource "aws_db_instance" "bar" { - identifier = "foobarbaz-test-terraform" + identifier = "foobarbaz-test-terraform-%d" allocated_storage = 10 engine = "mysql" @@ -148,5 +151,4 @@ resource "aws_db_instance" "bar" { backup_retention_period = 0 parameter_group_name = "default.mysql5.6" -} -` +}`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())