provider/aws: Defensively code around db_security_group ingress rules (#7893)

Fixes #7812

All of the options of `aws_db_security_group` ingress rules are
optional. Therefore, when one of them isn't set (and AWS doesn't
calculate the value), Terraform threw a panic

This commit just defensively codes around this fact. It checks to make
sure there is a value returned from the API before adding it to the map

```
% make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSDBSecurityGroup_'
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
TF_ACC=1 go test ./builtin/providers/aws -v
-run=TestAccAWSDBSecurityGroup_ -timeout 120m
=== RUN   TestAccAWSDBSecurityGroup_basic
--- PASS: TestAccAWSDBSecurityGroup_basic (38.66s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/aws    38.682s
```
This commit is contained in:
Paul Stack 2016-08-15 21:45:23 +01:00 committed by GitHub
parent 0c8b243ce0
commit 2c68808309

View File

@ -160,10 +160,15 @@ func resourceAwsDbSecurityGroupRead(d *schema.ResourceData, meta interface{}) er
}
for _, g := range sg.EC2SecurityGroups {
rule := map[string]interface{}{
"security_group_name": *g.EC2SecurityGroupName,
"security_group_id": *g.EC2SecurityGroupId,
"security_group_owner_id": *g.EC2SecurityGroupOwnerId,
rule := map[string]interface{}{}
if g.EC2SecurityGroupId != nil {
rule["security_group_id"] = *g.EC2SecurityGroupId
}
if g.EC2SecurityGroupName != nil {
rule["security_group_name"] = *g.EC2SecurityGroupName
}
if g.EC2SecurityGroupOwnerId != nil {
rule["security_group_owner_id"] = *g.EC2SecurityGroupOwnerId
}
rules.Add(rule)
}