From 8c958a318f24a6259f0325d2d39507431e0d31a1 Mon Sep 17 00:00:00 2001 From: Jakub Kania Date: Thu, 9 Aug 2018 00:54:42 +0200 Subject: [PATCH] Add arn for aws_subnet and data.aws_subnet --- aws/data_source_aws_subnet.go | 15 +++++++++++++++ aws/data_source_aws_subnet_test.go | 8 ++++++++ aws/resource_aws_subnet.go | 16 ++++++++++++++++ aws/resource_aws_subnet_test.go | 5 +++++ website/docs/d/subnet.html.markdown | 4 ++++ website/docs/r/subnet.html.markdown | 1 + 6 files changed, 49 insertions(+) diff --git a/aws/data_source_aws_subnet.go b/aws/data_source_aws_subnet.go index bdea59c4688..f8fffa26478 100644 --- a/aws/data_source_aws_subnet.go +++ b/aws/data_source_aws_subnet.go @@ -5,6 +5,7 @@ import ( "log" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/schema" ) @@ -74,6 +75,11 @@ func dataSourceAwsSubnet() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "arn": { + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -155,5 +161,14 @@ func dataSourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error { } } + arn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Region: meta.(*AWSClient).region, + Service: "ec2", + AccountID: meta.(*AWSClient).accountid, + Resource: fmt.Sprintf("subnet/%s", d.Id()), + } + d.Set("arn", arn.String()) + return nil } diff --git a/aws/data_source_aws_subnet_test.go b/aws/data_source_aws_subnet_test.go index 39eb35e4163..347a38453ee 100644 --- a/aws/data_source_aws_subnet_test.go +++ b/aws/data_source_aws_subnet_test.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + "regexp" ) func TestAccDataSourceAwsSubnet_basic(t *testing.T) { @@ -117,6 +118,13 @@ func testAccDataSourceAwsSubnetCheck(name string, rInt int) resource.TestCheckFu return fmt.Errorf("bad Name tag %s", attr["tags.Name"]) } + arnformat := `^arn:[^:]+:ec2:[^:]+:\d{12}:subnet/subnet-.+` + arnregex := regexp.MustCompile(arnformat) + + if !arnregex.MatchString(attr["arn"]) { + return fmt.Errorf("arn doesn't match format %s", attr["arn"]) + } + return nil } } diff --git a/aws/resource_aws_subnet.go b/aws/resource_aws_subnet.go index 88d23e829fb..b356d31b69b 100644 --- a/aws/resource_aws_subnet.go +++ b/aws/resource_aws_subnet.go @@ -6,6 +6,7 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/resource" @@ -68,6 +69,11 @@ func resourceAwsSubnet() *schema.Resource { Computed: true, }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchema(), }, } @@ -154,6 +160,16 @@ func resourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error { d.Set("ipv6_cidr_block", "") } } + + arn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Region: meta.(*AWSClient).region, + Service: "ec2", + AccountID: meta.(*AWSClient).accountid, + Resource: fmt.Sprintf("subnet/%s", d.Id()), + } + d.Set("arn", arn.String()) + d.Set("tags", tagsToMap(subnet.Tags)) return nil diff --git a/aws/resource_aws_subnet_test.go b/aws/resource_aws_subnet_test.go index ea57f64551d..27044f9992a 100644 --- a/aws/resource_aws_subnet_test.go +++ b/aws/resource_aws_subnet_test.go @@ -10,6 +10,7 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + "regexp" ) // add sweeper to delete known test subnets @@ -111,6 +112,10 @@ func TestAccAWSSubnet_basic(t *testing.T) { testAccCheckSubnetExists( "aws_subnet.foo", &v), testCheck, + resource.TestMatchResourceAttr( + "aws_subnet.foo", + "arn", + regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:subnet/subnet-.+`)), ), }, }, diff --git a/website/docs/d/subnet.html.markdown b/website/docs/d/subnet.html.markdown index 440f5b20fb6..0e1ba3bd266 100644 --- a/website/docs/d/subnet.html.markdown +++ b/website/docs/d/subnet.html.markdown @@ -89,3 +89,7 @@ All of the argument attributes except `filter` blocks are also exported as result attributes. This data source will complete the data by populating any fields that are not included in the configuration with the data for the selected subnet. + +In addition the following attributes are exported: + +* `arn` - The ARN of the subnet. \ No newline at end of file diff --git a/website/docs/r/subnet.html.markdown b/website/docs/r/subnet.html.markdown index 32a6a0e41cb..6e9ce2c16dc 100644 --- a/website/docs/r/subnet.html.markdown +++ b/website/docs/r/subnet.html.markdown @@ -64,6 +64,7 @@ The following arguments are supported: In addition to all arguments above, the following attributes are exported: * `id` - The ID of the subnet +* `arn` - The ARN of the subnet. * `availability_zone`- The AZ for the subnet. * `cidr_block` - The CIDR block for the subnet. * `vpc_id` - The VPC ID.