Skip to content

Commit

Permalink
Merge pull request #3822 from terraform-providers/b-aws_lb-schema-panic
Browse files Browse the repository at this point in the history
resource/aws_lb: Correctly set subnet_mappings in state
  • Loading branch information
bflad authored Mar 28, 2018
2 parents 6356002 + 28bc253 commit 51b7b2b
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions aws/resource_aws_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func resourceAwsLb() *schema.Resource {
"subnet_mapping": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -617,6 +618,23 @@ func flattenSubnetsFromAvailabilityZones(availabilityZones []*elbv2.Availability
return result
}

func flattenSubnetMappingsFromAvailabilityZones(availabilityZones []*elbv2.AvailabilityZone) []map[string]interface{} {
l := make([]map[string]interface{}, 0)
for _, availabilityZone := range availabilityZones {
for _, loadBalancerAddress := range availabilityZone.LoadBalancerAddresses {
m := make(map[string]interface{}, 0)
m["subnet_id"] = *availabilityZone.SubnetId

if loadBalancerAddress.AllocationId != nil {
m["allocation_id"] = *loadBalancerAddress.AllocationId
}

l = append(l, m)
}
}
return l
}

func lbSuffixFromARN(arn *string) string {
if arn == nil {
return ""
Expand All @@ -640,29 +658,19 @@ func flattenAwsLbResource(d *schema.ResourceData, meta interface{}, lb *elbv2.Lo
d.Set("name", lb.LoadBalancerName)
d.Set("internal", (lb.Scheme != nil && *lb.Scheme == "internal"))
d.Set("security_groups", flattenStringList(lb.SecurityGroups))
d.Set("subnets", flattenSubnetsFromAvailabilityZones(lb.AvailabilityZones))
d.Set("vpc_id", lb.VpcId)
d.Set("zone_id", lb.CanonicalHostedZoneId)
d.Set("dns_name", lb.DNSName)
d.Set("ip_address_type", lb.IpAddressType)
d.Set("load_balancer_type", lb.Type)

subnetMappings := make([]interface{}, 0)
for _, az := range lb.AvailabilityZones {
subnetMappingRaw := make([]map[string]interface{}, len(az.LoadBalancerAddresses))
for _, subnet := range az.LoadBalancerAddresses {
subnetMap := make(map[string]interface{}, 0)
subnetMap["subnet_id"] = *az.SubnetId

if subnet.AllocationId != nil {
subnetMap["allocation_id"] = *subnet.AllocationId
}
if err := d.Set("subnets", flattenSubnetsFromAvailabilityZones(lb.AvailabilityZones)); err != nil {
return fmt.Errorf("error setting subnets: %s", err)
}

subnetMappingRaw = append(subnetMappingRaw, subnetMap)
}
subnetMappings = append(subnetMappings, subnetMappingRaw)
if err := d.Set("subnet_mapping", flattenSubnetMappingsFromAvailabilityZones(lb.AvailabilityZones)); err != nil {
return fmt.Errorf("error setting subnet_mapping: %s", err)
}
d.Set("subnet_mapping", subnetMappings)

respTags, err := elbconn.DescribeTags(&elbv2.DescribeTagsInput{
ResourceArns: []*string{lb.LoadBalancerArn},
Expand Down

0 comments on commit 51b7b2b

Please sign in to comment.