From d8b5bea598af6fa075a5758a58093778b065b000 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Wed, 16 Nov 2016 16:28:37 -0800 Subject: [PATCH 1/2] provider/aws: Add aws_alb_listener data source This adds the aws_alb_listener data source to get information on an AWS Application Load Balancer listener. The schema is slightly modified (only option-wise, attributes are the same) and we use the aws_alb_listener resource read function to get the data. Note that the HTTPS test here may fail due until hashicorp/terraform#10180 is merged. --- .../aws/data_source_aws_alb_listener.go | 62 ++++ .../aws/data_source_aws_alb_listener_test.go | 318 ++++++++++++++++++ builtin/providers/aws/provider.go | 1 + 3 files changed, 381 insertions(+) create mode 100644 builtin/providers/aws/data_source_aws_alb_listener.go create mode 100644 builtin/providers/aws/data_source_aws_alb_listener_test.go diff --git a/builtin/providers/aws/data_source_aws_alb_listener.go b/builtin/providers/aws/data_source_aws_alb_listener.go new file mode 100644 index 000000000000..63ec4ed1a1f1 --- /dev/null +++ b/builtin/providers/aws/data_source_aws_alb_listener.go @@ -0,0 +1,62 @@ +package aws + +import "github.com/hashicorp/terraform/helper/schema" + +func dataSourceAwsAlbListener() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsAlbListenerRead, + + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Required: true, + }, + + "load_balancer_arn": { + Type: schema.TypeString, + Computed: true, + }, + "port": { + Type: schema.TypeInt, + Computed: true, + }, + + "protocol": { + Type: schema.TypeString, + Computed: true, + }, + + "ssl_policy": { + Type: schema.TypeString, + Computed: true, + }, + + "certificate_arn": { + Type: schema.TypeString, + Computed: true, + }, + + "default_action": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "target_group_arn": { + Type: schema.TypeString, + Computed: true, + }, + "type": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceAwsAlbListenerRead(d *schema.ResourceData, meta interface{}) error { + d.SetId(d.Get("arn").(string)) + return resourceAwsAlbListenerRead(d, meta) +} diff --git a/builtin/providers/aws/data_source_aws_alb_listener_test.go b/builtin/providers/aws/data_source_aws_alb_listener_test.go new file mode 100644 index 000000000000..da41e3d4c9bb --- /dev/null +++ b/builtin/providers/aws/data_source_aws_alb_listener_test.go @@ -0,0 +1,318 @@ +package aws + +import ( + "fmt" + "math/rand" + "testing" + "time" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAWSALBListener_basic(t *testing.T) { + albName := fmt.Sprintf("testlistener-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAWSALBListenerConfigBasic(albName, targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "load_balancer_arn"), + resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "arn"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "protocol", "HTTP"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "port", "80"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "default_action.#", "1"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "default_action.0.type", "forward"), + resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "default_action.0.target_group_arn"), + ), + }, + }, + }) +} + +func TestAccDataSourceAWSALBListener_https(t *testing.T) { + albName := fmt.Sprintf("testlistener-https-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAWSALBListenerConfigHTTPS(albName, targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "load_balancer_arn"), + resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "arn"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "protocol", "HTTPS"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "port", "443"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "default_action.#", "1"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "default_action.0.type", "forward"), + resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "default_action.0.target_group_arn"), + resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "certificate_arn"), + resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "ssl_policy", "ELBSecurityPolicy-2015-05"), + ), + }, + }, + }) +} + +func testAccDataSourceAWSALBListenerConfigBasic(albName, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_alb_listener" "front_end" { + load_balancer_arn = "${aws_alb.alb_test.id}" + protocol = "HTTP" + port = "80" + + default_action { + target_group_arn = "${aws_alb_target_group.test.id}" + type = "forward" + } +} + +resource "aws_alb" "alb_test" { + name = "%s" + internal = false + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_alb_target_group" "test" { + name = "%s" + port = 8080 + protocol = "HTTP" + vpc_id = "${aws_vpc.alb_test.id}" + + health_check { + path = "/health" + interval = 60 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_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 = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_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 = "TestAccAWSALB_basic" + } +} + +data "aws_alb_listener" "front_end" { + arn = "${aws_alb_listener.front_end.arn}" +}`, albName, targetGroupName) +} + +func testAccDataSourceAWSALBListenerConfigHTTPS(albName, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_alb_listener" "front_end" { + load_balancer_arn = "${aws_alb.alb_test.id}" + protocol = "HTTPS" + port = "443" + ssl_policy = "ELBSecurityPolicy-2015-05" + certificate_arn = "${aws_iam_server_certificate.test_cert.arn}" + + default_action { + target_group_arn = "${aws_alb_target_group.test.id}" + type = "forward" + } +} + +resource "aws_alb" "alb_test" { + name = "%s" + internal = false + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_alb_target_group" "test" { + name = "%s" + port = 8080 + protocol = "HTTP" + vpc_id = "${aws_vpc.alb_test.id}" + + health_check { + path = "/health" + interval = 60 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_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 = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_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 = "TestAccAWSALB_basic" + } +} + +resource "aws_iam_server_certificate" "test_cert" { + name = "terraform-test-cert-%d" + certificate_body = < Date: Thu, 17 Nov 2016 09:28:25 -0800 Subject: [PATCH 2/2] provider/aws: Add aws_alb_listener data source docs Now documented. --- .../aws/d/alb_listener.html.markdown | 38 +++++++++++++++++++ website/source/layouts/aws.erb | 3 ++ 2 files changed, 41 insertions(+) create mode 100644 website/source/docs/providers/aws/d/alb_listener.html.markdown diff --git a/website/source/docs/providers/aws/d/alb_listener.html.markdown b/website/source/docs/providers/aws/d/alb_listener.html.markdown new file mode 100644 index 000000000000..379a495a6a90 --- /dev/null +++ b/website/source/docs/providers/aws/d/alb_listener.html.markdown @@ -0,0 +1,38 @@ +--- +layout: "aws" +page_title: "AWS: aws_alb_listener" +sidebar_current: "docs-aws-datasource-alb-listener" +description: |- + Provides an Application Load Balancer Listener data source. +--- + +# aws\_alb\_listener + +Provides information about an Application Load Balancer Listener. + +This data source can prove useful when a module accepts an ALB Listener as an +input variable and needs to know the ALB it is attached to, or other +information specific to the listener in question. + +## Example Usage + +``` +variable "listener_arn" { + type = "string" +} + +data "aws_alb_listener" "listener" { + arn = "${var.listener_arn}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `arn` - (Required) The ARN of the listener. + +## Attributes Reference + +See the [ALB Listener Resource](/docs/providers/aws/r/alb_listener.html) for details +on the returned attributes - they are identical. diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index a95b954bed50..b29c946adb33 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -20,6 +20,9 @@ > aws_ami + > + aws_alb_listener + > aws_availability_zone