Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-source/iam_instance_profile: export attributes role_arn and role_name #4300

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should perform a len() test to prevent a panic if for some reason instanceProfile.Roles has no elements.

if len(instanceProfile.Roles) > 0 {
  role := instanceProfile.Roles[0]
  d.Set("role_arn", role.Arn)
  d.Set("role_id", role.RoleId)
  d.Set("role_name", role.RoleName)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can't always trust aws as we've seen quite a few times 😆

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+"$"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adjusting this to be more concise! A maintainability note: we should allow other AWS partitions for acceptance testing in aws-cn/aws-us-gov. Easiest way is: ^arn:[^:]+:...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to know. thanks 😄

),
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.