From ed403882e2aaf46bd4f5f060fc79ec6972c50451 Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Fri, 5 May 2017 10:25:52 -0400 Subject: [PATCH 1/2] provider/aws: Add ARN to security group data source Adds computed `arn` to security group data source ``` $ make testacc TEST=./builtin/providers/aws TESTARGS="-run=TestAccDataSourceAwsSecurityGroup" ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/05/05 10:17:35 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccDataSourceAwsSecurityGroup -timeout 120m === RUN TestAccDataSourceAwsSecurityGroup --- PASS: TestAccDataSourceAwsSecurityGroup (56.72s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 56.725s ``` --- .../aws/data_source_aws_security_group.go | 14 +++++++++++--- .../aws/data_source_aws_security_group_test.go | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_security_group.go b/builtin/providers/aws/data_source_aws_security_group.go index 1ff1f17a4..c0757d9a8 100644 --- a/builtin/providers/aws/data_source_aws_security_group.go +++ b/builtin/providers/aws/data_source_aws_security_group.go @@ -14,23 +14,29 @@ func dataSourceAwsSecurityGroup() *schema.Resource { Read: dataSourceAwsSecurityGroupRead, Schema: map[string]*schema.Schema{ - "vpc_id": &schema.Schema{ + "vpc_id": { Type: schema.TypeString, Optional: true, Computed: true, }, - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Optional: true, Computed: true, }, "filter": ec2CustomFiltersSchema(), - "id": &schema.Schema{ + "id": { Type: schema.TypeString, Optional: true, Computed: true, }, + + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchemaComputed(), }, } @@ -81,6 +87,8 @@ func dataSourceAwsSecurityGroupRead(d *schema.ResourceData, meta interface{}) er d.Set("description", sg.Description) d.Set("vpc_id", sg.VpcId) d.Set("tags", tagsToMap(sg.Tags)) + d.Set("arn", fmt.Sprintf("arn:%s:ec2:%s:%s/security-group/%s", + meta.(*AWSClient).partition, meta.(*AWSClient).region, *sg.OwnerId, *sg.GroupId)) return nil } diff --git a/builtin/providers/aws/data_source_aws_security_group_test.go b/builtin/providers/aws/data_source_aws_security_group_test.go index d697c1e3e..6e1f1664a 100644 --- a/builtin/providers/aws/data_source_aws_security_group_test.go +++ b/builtin/providers/aws/data_source_aws_security_group_test.go @@ -4,6 +4,8 @@ import ( "fmt" "testing" + "strings" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" @@ -66,6 +68,10 @@ func testAccDataSourceAwsSecurityGroupCheck(name string) resource.TestCheckFunc return fmt.Errorf("bad Name tag %s", attr["tags.Name"]) } + if !strings.Contains(attr["arn"], attr["id"]) { + return fmt.Errorf("bad ARN %s", attr["arn"]) + } + return nil } } From 06b5fbf9efdc3b2c3a900a5ba7ba19db4a3e9c2b Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Fri, 5 May 2017 13:58:15 -0400 Subject: [PATCH 2/2] add docs --- .../docs/providers/aws/d/security_group.html.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/website/source/docs/providers/aws/d/security_group.html.markdown b/website/source/docs/providers/aws/d/security_group.html.markdown index 988558e6d..175a0bd4b 100644 --- a/website/source/docs/providers/aws/d/security_group.html.markdown +++ b/website/source/docs/providers/aws/d/security_group.html.markdown @@ -65,6 +65,10 @@ 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 Security Group. -Additionally, the `description` attribute is exported. + +The following fields are also exported: + +* `description` - The description of the security group. +* `arn` - The computed ARN of the security group. ~> **Note:** The [default security group for a VPC](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html#DefaultSecurityGroup) has the name `default`.