-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add aws_alb_target_group data source (#764) * Fixed formatting issues * Fixing type in docs
- Loading branch information
Showing
6 changed files
with
454 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/elbv2" | ||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsAlbTargetGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsAlbTargetGroupRead, | ||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"arn_suffix": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"port": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"protocol": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"vpc_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"deregistration_delay": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"stickiness": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"enabled": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cookie_duration": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"health_check": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"interval": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"path": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"port": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"protocol": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"timeout": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"healthy_threshold": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"matcher": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"unhealthy_threshold": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"tags": tagsSchemaComputed(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsAlbTargetGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
elbconn := meta.(*AWSClient).elbv2conn | ||
tgArn := d.Get("arn").(string) | ||
tgName := d.Get("name").(string) | ||
|
||
describeTgOpts := &elbv2.DescribeTargetGroupsInput{} | ||
switch { | ||
case tgArn != "": | ||
describeTgOpts.TargetGroupArns = []*string{aws.String(tgArn)} | ||
case tgName != "": | ||
describeTgOpts.Names = []*string{aws.String(tgName)} | ||
} | ||
|
||
describeResp, err := elbconn.DescribeTargetGroups(describeTgOpts) | ||
if err != nil { | ||
return errwrap.Wrapf("Error retrieving ALB Target Group: {{err}}", err) | ||
} | ||
if len(describeResp.TargetGroups) != 1 { | ||
return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(describeResp.TargetGroups)) | ||
} | ||
|
||
targetGroup := describeResp.TargetGroups[0] | ||
|
||
d.SetId(*targetGroup.TargetGroupArn) | ||
return flattenAwsAlbTargetGroupResource(d, meta, targetGroup) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAWSALBTargetGroup_basic(t *testing.T) { | ||
albName := fmt.Sprintf("testalb-%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: testAccDataSourceAWSALBTargetGroupConfigBasic(albName, targetGroupName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "name", targetGroupName), | ||
resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "arn"), | ||
resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "arn_suffix"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "port", "8080"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"), | ||
resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "vpc_id"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "deregistration_delay", "300"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "tags.%", "1"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "stickiness.#", "1"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.#", "1"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.path", "/health"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.port", "8081"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.protocol", "HTTP"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.timeout", "3"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.healthy_threshold", "3"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.unhealthy_threshold", "3"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.matcher", "200-299"), | ||
|
||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "name", targetGroupName), | ||
resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "arn"), | ||
resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "arn_suffix"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "port", "8080"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "protocol", "HTTP"), | ||
resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "vpc_id"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "deregistration_delay", "300"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "tags.%", "1"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "stickiness.#", "1"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.#", "1"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.path", "/health"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.port", "8081"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.protocol", "HTTP"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.timeout", "3"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.healthy_threshold", "3"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.unhealthy_threshold", "3"), | ||
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.matcher", "200-299"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAWSALBTargetGroupConfigBasic(albName string, 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 = true | ||
security_groups = ["${aws_security_group.alb_test.id}"] | ||
subnets = ["${aws_subnet.alb_test.*.id}"] | ||
idle_timeout = 30 | ||
enable_deletion_protection = false | ||
tags { | ||
TestName = "TestAccDataSourceAWSALBTargetGroup_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" | ||
} | ||
tags { | ||
TestName = "TestAccDataSourceAWSALBTargetGroup_basic" | ||
} | ||
} | ||
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 = "TestAccDataSourceAWSALBTargetGroup_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 = "TestAccDataSourceAWSALBTargetGroup_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 = "TestAccDataSourceAWSALBTargetGroup_basic" | ||
} | ||
} | ||
data "aws_alb_target_group" "alb_tg_test_with_arn" { | ||
arn = "${aws_alb_target_group.test.arn}" | ||
} | ||
data "aws_alb_target_group" "alb_tg_test_with_name" { | ||
name = "${aws_alb_target_group.test.name}" | ||
}`, albName, targetGroupName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.