From 7de0f0da9226598dee3bb1234c74e8573bce31a8 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Wed, 14 Sep 2016 09:42:52 +0100 Subject: [PATCH] provider/aws: Expose CloudWatch Metrics ref on ALB When creating a CloudWatch Metric for an Application Load Balancer it is neccessary to use the suffix of the ARN as the reference to the load balancer. This commit exposes that as an attribute on the `aws_alb` resource to prevent the need to use regular expression substitution to make the reference. Fixes #8808. --- builtin/providers/aws/resource_aws_alb.go | 21 +++++++++++++ .../providers/aws/resource_aws_alb_test.go | 31 +++++++++++++++++++ .../docs/providers/aws/r/alb.html.markdown | 9 +++--- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_alb.go b/builtin/providers/aws/resource_aws_alb.go index 0668ed4af6ce..8a70772c28a6 100644 --- a/builtin/providers/aws/resource_aws_alb.go +++ b/builtin/providers/aws/resource_aws_alb.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/errwrap" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" + "regexp" ) func resourceAwsAlb() *schema.Resource { @@ -28,6 +29,11 @@ func resourceAwsAlb() *schema.Resource { Computed: true, }, + "cloudwatch_ref": { + Type: schema.TypeString, + Computed: true, + }, + "name": { Type: schema.TypeString, Optional: true, @@ -191,6 +197,7 @@ func resourceAwsAlbRead(d *schema.ResourceData, meta interface{}) error { alb := describeResp.LoadBalancers[0] d.Set("arn", alb.LoadBalancerArn) + d.Set("cloudwatch_ref", cloudwatchALBSuffixFromARN(alb.LoadBalancerArn)) d.Set("name", alb.LoadBalancerName) d.Set("internal", (alb.Scheme != nil && *alb.Scheme == "internal")) d.Set("security_groups", flattenStringList(alb.SecurityGroups)) @@ -345,3 +352,17 @@ func flattenSubnetsFromAvailabilityZones(availabilityZones []*elbv2.Availability } return result } + +func cloudwatchALBSuffixFromARN(arn *string) string { + if arn == nil { + return "" + } + + if arnComponents := regexp.MustCompile(`arn:.*:loadbalancer/(.*)`).FindAllStringSubmatch(*arn, -1); len(arnComponents) == 1 { + if len(arnComponents[0]) == 2 { + return arnComponents[0][1] + } + } + + return "" +} diff --git a/builtin/providers/aws/resource_aws_alb_test.go b/builtin/providers/aws/resource_aws_alb_test.go index d6d5a48f8e01..c0d6687cc871 100644 --- a/builtin/providers/aws/resource_aws_alb_test.go +++ b/builtin/providers/aws/resource_aws_alb_test.go @@ -14,6 +14,37 @@ import ( "github.com/hashicorp/terraform/terraform" ) +func TestALBCloudwatchSuffixFromARN(t *testing.T) { + cases := []struct { + name string + arn *string + suffix string + }{ + { + name: "valid suffix", + arn: aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:loadbalancer/app/my-alb/abc123`), + suffix: `app/my-alb/abc123`, + }, + { + name: "no suffix", + arn: aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:loadbalancer`), + suffix: ``, + }, + { + name: "nil ARN", + arn: nil, + suffix: ``, + }, + } + + for _, tc := range cases { + actual := cloudwatchALBSuffixFromARN(tc.arn) + if actual != tc.suffix { + t.Fatalf("bad suffix: %q\nExpected: %s\n Got: %s", tc.name, tc.suffix, actual) + } + } +} + func TestAccAWSALB_basic(t *testing.T) { var conf elbv2.LoadBalancer albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) diff --git a/website/source/docs/providers/aws/r/alb.html.markdown b/website/source/docs/providers/aws/r/alb.html.markdown index c4b25825377d..eba1b1873204 100644 --- a/website/source/docs/providers/aws/r/alb.html.markdown +++ b/website/source/docs/providers/aws/r/alb.html.markdown @@ -59,11 +59,12 @@ Access Logs (`access_logs`) support the following: The following attributes are exported in addition to the arguments listed above: -* `id` - The ARN of the load balancer (matches `arn`) -* `arn` - The ARN of the load balancer (matches `id`) -* `dns_name` - The DNS name of the load balancer +* `id` - The ARN of the load balancer (matches `arn`). +* `arn` - The ARN of the load balancer (matches `id`). +* `cloudwatch_ref` - The ARN suffix for use with CloudWatch Metrics. +* `dns_name` - The DNS name of the load balancer. * `canonical_hosted_zone_id` - The canonical hosted zone ID of the load balancer. -* `zone_id` - The canonical hosted zone ID of the load balancer (to be used in a Route 53 Alias record) +* `zone_id` - The canonical hosted zone ID of the load balancer (to be used in a Route 53 Alias record). ## Import