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/aws_iam_role: Add permissions_boundary attribute #5186

Merged
merged 1 commit into from
Jul 24, 2018
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
48 changes: 44 additions & 4 deletions aws/data_source_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package aws

import (
"fmt"
"net/url"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -28,6 +32,10 @@ func dataSourceAwsIAMRole() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"permissions_boundary": {
Type: schema.TypeString,
Computed: true,
},
"role_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -63,6 +71,8 @@ func dataSourceAwsIAMRole() *schema.Resource {
}

func dataSourceAwsIAMRoleRead(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn

name, hasName := d.GetOk("name")
roleName, hasRoleName := d.GetOk("role_name")

Expand All @@ -78,10 +88,40 @@ func dataSourceAwsIAMRoleRead(d *schema.ResourceData, meta interface{}) error {
}
d.SetId(id)

data := resourceAwsIamRoleRead(d, meta)
input := &iam.GetRoleInput{
RoleName: aws.String(d.Id()),
}

output, err := iamconn.GetRole(input)
if err != nil {
return fmt.Errorf("Error reading IAM Role %s: %s", d.Id(), err)
}

d.Set("arn", output.Role.Arn)
if err := d.Set("create_date", output.Role.CreateDate.Format(time.RFC3339)); err != nil {
return err
}
d.Set("description", output.Role.Description)
d.Set("max_session_duration", output.Role.MaxSessionDuration)
d.Set("name", output.Role.RoleName)
d.Set("path", output.Role.Path)
d.Set("permissions_boundary", "")
Copy link
Member

Choose a reason for hiding this comment

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

Do we have to set this to an empty string? I would think that Terraform would automatically do this but maybe I'm wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the d.Set() is wrapped in a conditional, this is to properly handle the missing attribute if its not sent back by the API (output.Role.PermissionsBoundary is nil). Otherwise Terraform will throw an error about the missing attribute in this scenario:

output "permissions_boundary" {
  value = "${data.aws_iam_role.example.permissions_boundary}"
}

It can potentially go away after Terraform 0.12 allows returning full resources.

if output.Role.PermissionsBoundary != nil {
d.Set("permissions_boundary", output.Role.PermissionsBoundary.PermissionsBoundaryArn)
}
d.Set("unique_id", output.Role.RoleId)

assumRolePolicy, err := url.QueryUnescape(aws.StringValue(output.Role.AssumeRolePolicyDocument))
if err != nil {
return err
}
if err := d.Set("assume_role_policy", assumRolePolicy); err != nil {
return err
}

// Keep backward compatibility with previous attributes
d.Set("role_id", d.Get("unique_id").(string))
d.Set("assume_role_policy_document", d.Get("assume_role_policy").(string))
d.Set("role_id", output.Role.RoleId)
d.Set("assume_role_policy_document", assumRolePolicy)

return data
return nil
}
1 change: 1 addition & 0 deletions aws/data_source_aws_iam_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAccAWSDataSourceIAMRole_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.aws_iam_role.test", "unique_id"),
resource.TestCheckResourceAttrSet("data.aws_iam_role.test", "assume_role_policy"),
resource.TestCheckResourceAttr("data.aws_iam_role.test", "path", "/testpath/"),
resource.TestCheckResourceAttr("data.aws_iam_role.test", "permissions_boundary", ""),
resource.TestCheckResourceAttr("data.aws_iam_role.test", "name", roleName),
resource.TestCheckResourceAttrSet("data.aws_iam_role.test", "create_date"),
resource.TestMatchResourceAttr("data.aws_iam_role.test", "arn",
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/iam_role.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ data "aws_iam_role" "example" {
* `arn` - The Amazon Resource Name (ARN) specifying the role.
* `assume_role_policy` - The policy document associated with the role.
* `path` - The path to the role.
* `permissions_boundary` - The ARN of the policy that is used to set the permissions boundary for the role.
* `unique_id` - The stable and unique string identifying the role.