Skip to content

Commit

Permalink
Merge pull request #5486 from blckct/subnet_add_arn
Browse files Browse the repository at this point in the history
Add arn for aws_subnet and data.aws_subnet
  • Loading branch information
bflad authored Aug 9, 2018
2 parents cffc341 + 8c958a3 commit f56d3f5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions aws/data_source_aws_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -74,6 +75,11 @@ func dataSourceAwsSubnet() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions aws/data_source_aws_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}
Expand Down
16 changes: 16 additions & 0 deletions aws/resource_aws_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -68,6 +69,11 @@ func resourceAwsSubnet() *schema.Resource {
Computed: true,
},

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

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions aws/resource_aws_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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-.+`)),
),
},
},
Expand Down
4 changes: 4 additions & 0 deletions website/docs/d/subnet.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions website/docs/r/subnet.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f56d3f5

Please sign in to comment.