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
This commit is contained in:
Paul Hinze 2015-04-01 09:13:41 -05:00
parent 85c89c3ec6
commit 78963fc3d9
2 changed files with 35 additions and 28 deletions

View File

@ -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{

View File

@ -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())