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

New Data Source: aws_elb #2004

Merged
merged 9 commits into from
Dec 8, 2017
212 changes: 212 additions & 0 deletions aws/data_source_aws_elb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsElb() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsElbRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"access_logs": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"interval": {
Type: schema.TypeInt,
Computed: true,
},
"bucket": {
Type: schema.TypeString,
Computed: true,
},
"bucket_prefix": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},

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

"connection_draining": {
Type: schema.TypeBool,
Computed: true,
},

"connection_draining_timeout": {
Type: schema.TypeInt,
Computed: true,
},

"cross_zone_load_balancing": {
Type: schema.TypeBool,
Computed: true,
},

"dns_name": {
Type: schema.TypeString,
Computed: true,
},

"health_check": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"healthy_threshold": {
Type: schema.TypeInt,
Computed: true,
},

"unhealthy_threshold": {
Type: schema.TypeInt,
Computed: true,
},

"target": {
Type: schema.TypeString,
Computed: true,
},

"interval": {
Type: schema.TypeInt,
Computed: true,
},

"timeout": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},

"idle_timeout": {
Type: schema.TypeInt,
Computed: true,
},

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

"internal": {
Type: schema.TypeBool,
Computed: true,
},

"listener": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_port": {
Type: schema.TypeInt,
Computed: true,
},

"instance_protocol": {
Type: schema.TypeString,
Computed: true,
},

"lb_port": {
Type: schema.TypeInt,
Computed: true,
},

"lb_protocol": {
Type: schema.TypeString,
Computed: true,
},

"ssl_certificate_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
Set: resourceAwsElbListenerHash,
},

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

"source_security_group": {
Type: schema.TypeString,
Computed: true,
},

"source_security_group_id": {
Type: schema.TypeString,
Computed: true,
},

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

"tags": tagsSchemaComputed(),

"zone_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
lbName := d.Get("name").(string)

input := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{aws.String(lbName)},
}

log.Printf("[DEBUG] Reading ELBs: %#v", input)
resp, err := elbconn.DescribeLoadBalancers(input)
if err != nil {
return fmt.Errorf("Error retrieving LB: %s", err)
}
if len(resp.LoadBalancerDescriptions) != 1 {
return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(resp.LoadBalancerDescriptions))
}
d.SetId(*resp.LoadBalancerDescriptions[0].LoadBalancerName)

return flattenAwsELbResource(d, meta.(*AWSClient).ec2conn, elbconn, resp.LoadBalancerDescriptions[0])
}
114 changes: 114 additions & 0 deletions aws/data_source_aws_elb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAWSELB_basic(t *testing.T) {
// Must be less than 32 characters for ELB name
rName := fmt.Sprintf("TestAccDataSourceAWSELB-%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAWSELBConfigBasic(rName, t.Name()),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elb.elb_test", "name", rName),
resource.TestCheckResourceAttr("data.aws_elb.elb_test", "cross_zone_load_balancing", "true"),
resource.TestCheckResourceAttr("data.aws_elb.elb_test", "idle_timeout", "30"),
resource.TestCheckResourceAttr("data.aws_elb.elb_test", "internal", "true"),
resource.TestCheckResourceAttr("data.aws_elb.elb_test", "subnets.#", "2"),
resource.TestCheckResourceAttr("data.aws_elb.elb_test", "security_groups.#", "1"),
resource.TestCheckResourceAttr("data.aws_elb.elb_test", "tags.%", "1"),
resource.TestCheckResourceAttr("data.aws_elb.elb_test", "tags.TestName", t.Name()),
resource.TestCheckResourceAttrSet("data.aws_elb.elb_test", "dns_name"),
resource.TestCheckResourceAttrSet("data.aws_elb.elb_test", "zone_id"),
),
},
},
})
}

func testAccDataSourceAWSELBConfigBasic(rName, testName string) string {
return fmt.Sprintf(`
resource "aws_elb" "elb_test" {
name = "%[1]s"
internal = true
security_groups = ["${aws_security_group.elb_test.id}"]
subnets = ["${aws_subnet.elb_test.*.id}"]

idle_timeout = 30

listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}

tags {
TestName = "%[2]s"
}
}

variable "subnets" {
default = ["10.0.1.0/24", "10.0.2.0/24"]
type = "list"
}

data "aws_availability_zones" "available" {}

resource "aws_vpc" "elb_test" {
cidr_block = "10.0.0.0/16"

tags {
TestName = "%[2]s"
}
}

resource "aws_subnet" "elb_test" {
count = 2
vpc_id = "${aws_vpc.elb_test.id}"
cidr_block = "${element(var.subnets, count.index)}"
map_public_ip_on_launch = true
availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}"

tags {
TestName = "%[2]s"
}
}

resource "aws_security_group" "elb_test" {
name = "%[1]s"
description = "%[2]s"
vpc_id = "${aws_vpc.elb_test.id}"

ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags {
TestName = "%[2]s"
}
}

data "aws_elb" "elb_test" {
name = "${aws_elb.elb_test.name}"
}`, rName, testName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func Provider() terraform.ResourceProvider {
"aws_eip": dataSourceAwsEip(),
"aws_elastic_beanstalk_solution_stack": dataSourceAwsElasticBeanstalkSolutionStack(),
"aws_elasticache_cluster": dataSourceAwsElastiCacheCluster(),
"aws_elb": dataSourceAwsElb(),
"aws_elasticache_replication_group": dataSourceAwsElasticacheReplicationGroup(),
"aws_elb_hosted_zone_id": dataSourceAwsElbHostedZoneId(),
"aws_elb_service_account": dataSourceAwsElbServiceAccount(),
Expand Down
20 changes: 8 additions & 12 deletions aws/resource_aws_elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,21 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions)
}

return flattenAwsELbResource(d, meta.(*AWSClient).ec2conn, elbconn, describeResp.LoadBalancerDescriptions[0])
}

// flattenAwsELbResource takes a *elbv2.LoadBalancer and populates all respective resource fields.
func flattenAwsELbResource(d *schema.ResourceData, ec2conn *ec2.EC2, elbconn *elb.ELB, lb *elb.LoadBalancerDescription) error {
describeAttrsOpts := &elb.DescribeLoadBalancerAttributesInput{
LoadBalancerName: aws.String(elbName),
LoadBalancerName: aws.String(d.Id()),
}
describeAttrsResp, err := elbconn.DescribeLoadBalancerAttributes(describeAttrsOpts)
if err != nil {
if isLoadBalancerNotFound(err) {
// The ELB is gone now, so just remove it from the state
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving ELB: %s", err)
}

lbAttrs := describeAttrsResp.LoadBalancerAttributes

lb := describeResp.LoadBalancerDescriptions[0]

d.Set("name", lb.LoadBalancerName)
d.Set("dns_name", lb.DNSName)
d.Set("zone_id", lb.CanonicalHostedZoneNameID)
Expand All @@ -416,7 +413,7 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
var elbVpc string
if lb.VPCId != nil {
elbVpc = *lb.VPCId
sgId, err := sourceSGIdByName(meta, *lb.SourceSecurityGroup.GroupName, elbVpc)
sgId, err := sourceSGIdByName(ec2conn, *lb.SourceSecurityGroup.GroupName, elbVpc)
if err != nil {
return fmt.Errorf("[WARN] Error looking up ELB Security Group ID: %s", err)
} else {
Expand Down Expand Up @@ -850,8 +847,7 @@ func isLoadBalancerNotFound(err error) bool {
return ok && elberr.Code() == "LoadBalancerNotFound"
}

func sourceSGIdByName(meta interface{}, sg, vpcId string) (string, error) {
conn := meta.(*AWSClient).ec2conn
func sourceSGIdByName(conn *ec2.EC2, sg, vpcId string) (string, error) {
var filters []*ec2.Filter
var sgFilterName, sgFilterVPCID *ec2.Filter
sgFilterName = &ec2.Filter{
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<li<%= sidebar_current("docs-aws-datasource-elasticache-replication-group") %>>
<a href="/docs/providers/aws/d/elasticache_replication_group.html">aws_elasticache_replication_group</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-elb") %>>
<a href="/docs/providers/aws/d/elb.html">aws_elb</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-elb-hosted-zone-id") %>>
<a href="/docs/providers/aws/d/elb_hosted_zone_id.html">aws_elb_hosted_zone_id</a>
</li>
Expand Down
Loading