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

Add private_ips attribute to aws_lb resource #2901

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions aws/resource_aws_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func resourceAwsLb() *schema.Resource {
Default: "application",
},

"private_ips": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Set: schema.HashString,
},

"security_groups": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Expand Down Expand Up @@ -657,6 +664,7 @@ func lbSuffixFromARN(arn *string) string {

// flattenAwsLbResource takes a *elbv2.LoadBalancer and populates all respective resource fields.
func flattenAwsLbResource(d *schema.ResourceData, meta interface{}, lb *elbv2.LoadBalancer) error {
ec2conn := meta.(*AWSClient).ec2conn
elbconn := meta.(*AWSClient).elbv2conn

d.Set("arn", lb.LoadBalancerArn)
Expand All @@ -678,6 +686,22 @@ func flattenAwsLbResource(d *schema.ResourceData, meta interface{}, lb *elbv2.Lo
return fmt.Errorf("error setting subnet_mapping: %s", err)
}

privateIps := make([]string, 0)
networkInterfacesResp, err := ec2conn.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{
Filters: buildEC2AttributeFilterList(
map[string]string{
"description": fmt.Sprintf("ELB %s", lbSuffixFromARN(lb.LoadBalancerArn)),
},
),
})
if err != nil {
return err
}
for _, eni := range networkInterfacesResp.NetworkInterfaces {
privateIps = append(privateIps, *eni.PrivateIpAddress)
}
d.Set("private_ips", privateIps)

respTags, err := elbconn.DescribeTags(&elbv2.DescribeTagsInput{
ResourceArns: []*string{lb.LoadBalancerArn},
})
Expand Down
3 changes: 3 additions & 0 deletions aws/resource_aws_lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func TestAccAWSLB_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("aws_lb.lb_test", "zone_id"),
resource.TestCheckResourceAttrSet("aws_lb.lb_test", "dns_name"),
resource.TestCheckResourceAttrSet("aws_lb.lb_test", "arn"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "private_ips.#", "1"),
),
},
},
Expand Down Expand Up @@ -150,6 +151,7 @@ func TestAccAWSLB_networkLoadbalancerBasic(t *testing.T) {
resource.TestCheckResourceAttrSet("aws_lb.lb_test", "dns_name"),
resource.TestCheckResourceAttrSet("aws_lb.lb_test", "arn"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "load_balancer_type", "network"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "private_ips.#", "1"),
),
},
},
Expand Down Expand Up @@ -178,6 +180,7 @@ func TestAccAWSLB_networkLoadbalancerEIP(t *testing.T) {
resource.TestCheckResourceAttr("aws_lb.lb_test", "load_balancer_type", "network"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "subnet_mapping.#", "2"),
resource.TestCheckResourceAttr("aws_lb.test", "private_ips.#", "2"),
),
},
},
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/lb.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ The following attributes are exported in addition to the arguments listed above:
* `arn_suffix` - The ARN suffix for use with CloudWatch Metrics.
* `dns_name` - The DNS name of the load balancer.
* `zone_id` - The canonical hosted zone ID of the load balancer (to be used in a Route 53 Alias record).
* `private_ips` - Private IP addresses of the network interfaces attached to the load balancer.

## Timeouts

Expand Down