From ecc2a66584e5aaf07c074f79280750a0f2edae28 Mon Sep 17 00:00:00 2001 From: Jakub Kania Date: Thu, 2 Aug 2018 20:16:55 +0200 Subject: [PATCH 1/2] Add ARN attribute for aws_instance and data.aws_instance (#5425) --- aws/data_source_aws_instance.go | 16 ++++++++++++++++ aws/data_source_aws_instance_test.go | 2 ++ aws/resource_aws_instance.go | 17 +++++++++++++++++ aws/resource_aws_instance_test.go | 4 ++++ website/docs/d/instance.html.markdown | 1 + website/docs/r/instance.html.markdown | 1 + 6 files changed, 41 insertions(+) diff --git a/aws/data_source_aws_instance.go b/aws/data_source_aws_instance.go index d2ff51ab772..9fc63317b54 100644 --- a/aws/data_source_aws_instance.go +++ b/aws/data_source_aws_instance.go @@ -3,8 +3,10 @@ package aws import ( "fmt" "log" + "strings" "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" ) @@ -26,6 +28,10 @@ func dataSourceAwsInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, "instance_type": { Type: schema.TypeString, Computed: true, @@ -317,6 +323,16 @@ func dataSourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error { d.Set("password_data", passwordData) } + // ARN + arn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Region: meta.(*AWSClient).region, + Service: "ec2", + AccountID: meta.(*AWSClient).accountid, + Resource: fmt.Sprintf("instance/%s", strings.TrimPrefix(d.Id(), "/")), + } + d.Set("arn", arn.String()) + return nil } diff --git a/aws/data_source_aws_instance_test.go b/aws/data_source_aws_instance_test.go index cd15e1aca2a..d68cb0796d3 100644 --- a/aws/data_source_aws_instance_test.go +++ b/aws/data_source_aws_instance_test.go @@ -4,6 +4,7 @@ import ( "testing" "fmt" + "regexp" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" @@ -20,6 +21,7 @@ func TestAccAWSInstanceDataSource_basic(t *testing.T) { resource.TestCheckResourceAttr("data.aws_instance.web-instance", "ami", "ami-4fccb37f"), resource.TestCheckResourceAttr("data.aws_instance.web-instance", "tags.%", "1"), resource.TestCheckResourceAttr("data.aws_instance.web-instance", "instance_type", "m1.small"), + resource.TestMatchResourceAttr("data.aws_instance.web-instance", "arn", regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:instance/i-.+`)), ), }, }, diff --git a/aws/resource_aws_instance.go b/aws/resource_aws_instance.go index 18781205893..aa8e3a237ef 100644 --- a/aws/resource_aws_instance.go +++ b/aws/resource_aws_instance.go @@ -12,6 +12,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/hashcode" @@ -45,6 +46,11 @@ func resourceAwsInstance() *schema.Resource { ForceNew: true, }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "associate_public_ip_address": { Type: schema.TypeBool, ForceNew: true, @@ -802,6 +808,17 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error { d.Set("ephemeral_block_device", []interface{}{}) } + // ARN + + arn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Region: meta.(*AWSClient).region, + Service: "ec2", + AccountID: meta.(*AWSClient).accountid, + Resource: fmt.Sprintf("instance/%s", strings.TrimPrefix(d.Id(), "/")), + } + d.Set("arn", arn.String()) + // Instance attributes { attr, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{ diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index ebfb1729fb9..9d4e6a1a5ad 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -130,6 +130,10 @@ func TestAccAWSInstance_basic(t *testing.T) { "3dc39dda39be1205215e776bad998da361a5955d"), resource.TestCheckResourceAttr( "aws_instance.foo", "ebs_block_device.#", "0"), + resource.TestMatchResourceAttr( + "aws_instance.foo", + "arn", + regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:instance/i-.+`)), ), }, diff --git a/website/docs/d/instance.html.markdown b/website/docs/d/instance.html.markdown index ed3cbd10fc7..84b3726874e 100644 --- a/website/docs/d/instance.html.markdown +++ b/website/docs/d/instance.html.markdown @@ -57,6 +57,7 @@ are exported: interpolation. * `ami` - The ID of the AMI used to launch the instance. +* `arn` - The ARN of the instance. * `associate_public_ip_address` - Whether or not the Instance is associated with a public IP address or not (Boolean). * `availability_zone` - The availability zone of the Instance. * `ebs_block_device` - The EBS block device mappings of the Instance. diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index 8f8298a9288..ce172aec791 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -240,6 +240,7 @@ resource "aws_instance" "foo" { In addition to all arguments above, the following attributes are exported: * `id` - The instance ID. +* `arn` - The ARN of the instance. * `availability_zone` - The availability zone of the instance. * `placement_group` - The placement group of the instance. * `key_name` - The key name of the instance From 995803a0769fab7ceb055e7f726f28dbcef27ebe Mon Sep 17 00:00:00 2001 From: Jakub Kania Date: Sat, 4 Aug 2018 15:46:36 +0200 Subject: [PATCH 2/2] mend --- aws/data_source_aws_instance.go | 3 +-- aws/resource_aws_instance.go | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/aws/data_source_aws_instance.go b/aws/data_source_aws_instance.go index 9fc63317b54..0c49381cb5f 100644 --- a/aws/data_source_aws_instance.go +++ b/aws/data_source_aws_instance.go @@ -3,7 +3,6 @@ package aws import ( "fmt" "log" - "strings" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/arn" @@ -329,7 +328,7 @@ func dataSourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error { Region: meta.(*AWSClient).region, Service: "ec2", AccountID: meta.(*AWSClient).accountid, - Resource: fmt.Sprintf("instance/%s", strings.TrimPrefix(d.Id(), "/")), + Resource: fmt.Sprintf("instance/%s", d.Id()), } d.Set("arn", arn.String()) diff --git a/aws/resource_aws_instance.go b/aws/resource_aws_instance.go index aa8e3a237ef..22aa91cd1c0 100644 --- a/aws/resource_aws_instance.go +++ b/aws/resource_aws_instance.go @@ -815,7 +815,7 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error { Region: meta.(*AWSClient).region, Service: "ec2", AccountID: meta.(*AWSClient).accountid, - Resource: fmt.Sprintf("instance/%s", strings.TrimPrefix(d.Id(), "/")), + Resource: fmt.Sprintf("instance/%s", d.Id()), } d.Set("arn", arn.String())