From 7992cf36d628c4bfb46c34f7f87a6df482bba1cb Mon Sep 17 00:00:00 2001 From: "xiaowei.wang" Date: Sat, 21 Apr 2018 10:52:25 +0200 Subject: [PATCH] data-source/iam_instance_profile: export attributes of role_arn and role_name --- aws/data_source_aws_iam_instance_profile.go | 16 +++++++++++++--- ...ta_source_aws_iam_instance_profile_test.go | 19 ++++++++++++++----- .../docs/d/iam_instance_profile.html.markdown | 6 +++++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/aws/data_source_aws_iam_instance_profile.go b/aws/data_source_aws_iam_instance_profile.go index 305e2e4e734b..e999f7f9a9fc 100644 --- a/aws/data_source_aws_iam_instance_profile.go +++ b/aws/data_source_aws_iam_instance_profile.go @@ -31,10 +31,18 @@ func dataSourceAwsIAMInstanceProfile() *schema.Resource { Type: schema.TypeString, Required: true, }, + "role_arn": { + Type: schema.TypeString, + Computed: true, + }, "role_id": { Type: schema.TypeString, Computed: true, }, + "role_name": { + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -64,9 +72,11 @@ func dataSourceAwsIAMInstanceProfileRead(d *schema.ResourceData, meta interface{ d.Set("create_date", fmt.Sprintf("%v", instanceProfile.CreateDate)) d.Set("path", instanceProfile.Path) - for _, r := range instanceProfile.Roles { - d.Set("role_id", r.RoleId) - } + // it's guaranteed that instanceProfile.Roles exists and has one element + role := instanceProfile.Roles[0] + d.Set("role_arn", role.Arn) + d.Set("role_id", role.RoleId) + d.Set("role_name", role.RoleName) return nil } diff --git a/aws/data_source_aws_iam_instance_profile_test.go b/aws/data_source_aws_iam_instance_profile_test.go index eabbb5d450b3..411e87c0dfc4 100644 --- a/aws/data_source_aws_iam_instance_profile_test.go +++ b/aws/data_source_aws_iam_instance_profile_test.go @@ -10,8 +10,8 @@ import ( ) func TestAccAWSDataSourceIAMInstanceProfile_basic(t *testing.T) { - roleName := fmt.Sprintf("test-datasource-user-%d", acctest.RandInt()) - profileName := fmt.Sprintf("test-datasource-user-%d", acctest.RandInt()) + roleName := fmt.Sprintf("tf-acc-ds-instance-profile-role-%d", acctest.RandInt()) + profileName := fmt.Sprintf("tf-acc-ds-instance-profile-%d", acctest.RandInt()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -20,10 +20,19 @@ func TestAccAWSDataSourceIAMInstanceProfile_basic(t *testing.T) { { Config: testAccDatasourceAwsIamInstanceProfileConfig(roleName, profileName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("data.aws_iam_instance_profile.test", "role_id"), + resource.TestMatchResourceAttr( + "data.aws_iam_instance_profile.test", + "arn", + regexp.MustCompile("^arn:aws:iam::[0-9]{12}:instance-profile/testpath/"+profileName+"$"), + ), resource.TestCheckResourceAttr("data.aws_iam_instance_profile.test", "path", "/testpath/"), - resource.TestMatchResourceAttr("data.aws_iam_instance_profile.test", "arn", - regexp.MustCompile("^arn:aws:iam::[0-9]{12}:instance-profile/testpath/"+profileName+"$")), + resource.TestMatchResourceAttr( + "data.aws_iam_instance_profile.test", + "role_arn", + regexp.MustCompile("^arn:aws:iam::[0-9]{12}:role/"+roleName+"$"), + ), + resource.TestCheckResourceAttrSet("data.aws_iam_instance_profile.test", "role_id"), + resource.TestCheckResourceAttr("data.aws_iam_instance_profile.test", "role_name", roleName), ), }, }, diff --git a/website/docs/d/iam_instance_profile.html.markdown b/website/docs/d/iam_instance_profile.html.markdown index 4eb866a0799a..017069a34a33 100644 --- a/website/docs/d/iam_instance_profile.html.markdown +++ b/website/docs/d/iam_instance_profile.html.markdown @@ -9,7 +9,7 @@ description: |- # Data Source: aws_iam_instance_profile This data source can be used to fetch information about a specific -IAM instance profile. By using this data source, you can reference IAM +IAM instance profile. By using this data source, you can reference IAM instance profile properties without having to hard code ARNs as input. ## Example Usage @@ -33,4 +33,8 @@ data "aws_iam_instance_profile" "example" { * `path` - The path to the instance profile. +* `role_arn` - The role arn associated with this instance profile. + * `role_id` - The role id associated with this instance profile. + +* `role_name` - The role name associated with this instance profile.