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

provider/aws: Expose ARN suffix on ALB #8833

Merged
merged 1 commit into from
Sep 14, 2016
Merged
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
21 changes: 21 additions & 0 deletions builtin/providers/aws/resource_aws_alb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"strconv"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -28,6 +29,11 @@ func resourceAwsAlb() *schema.Resource {
Computed: true,
},

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

"name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -191,6 +197,7 @@ func resourceAwsAlbRead(d *schema.ResourceData, meta interface{}) error {
alb := describeResp.LoadBalancers[0]

d.Set("arn", alb.LoadBalancerArn)
d.Set("arn_suffix", albSuffixFromARN(alb.LoadBalancerArn))
d.Set("name", alb.LoadBalancerName)
d.Set("internal", (alb.Scheme != nil && *alb.Scheme == "internal"))
d.Set("security_groups", flattenStringList(alb.SecurityGroups))
Expand Down Expand Up @@ -345,3 +352,17 @@ func flattenSubnetsFromAvailabilityZones(availabilityZones []*elbv2.Availability
}
return result
}

func albSuffixFromARN(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 ""
}
31 changes: 31 additions & 0 deletions builtin/providers/aws/resource_aws_alb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := albSuffixFromARN(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))
Expand Down
9 changes: 5 additions & 4 deletions website/source/docs/providers/aws/r/alb.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
* `arn_suffix` - 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

Expand Down