Skip to content

Commit

Permalink
data-source/iam_instance_profile: export attributes of role_arn and r…
Browse files Browse the repository at this point in the history
…ole_name
  • Loading branch information
xiaowei.wang committed Apr 21, 2018
1 parent 20aebab commit 7992cf3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
16 changes: 13 additions & 3 deletions aws/data_source_aws_iam_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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
}
19 changes: 14 additions & 5 deletions aws/data_source_aws_iam_instance_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand All @@ -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),
),
},
},
Expand Down
6 changes: 5 additions & 1 deletion website/docs/d/iam_instance_profile.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

0 comments on commit 7992cf3

Please sign in to comment.