Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_lb: Correctly set subnet_mappings in state #3822

Merged
merged 2 commits into from
Mar 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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