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

Add "name_with_path" to iam_role data source and resource #6287

Closed
Closed
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
7 changes: 7 additions & 0 deletions aws/data_source_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"net/url"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -58,6 +59,10 @@ func dataSourceAwsIAMRole() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"name_with_path": {
Type: schema.TypeString,
Computed: true,
},
"create_date": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -105,6 +110,8 @@ func dataSourceAwsIAMRoleRead(d *schema.ResourceData, meta interface{}) error {
d.Set("max_session_duration", output.Role.MaxSessionDuration)
d.Set("name", output.Role.RoleName)
d.Set("path", output.Role.Path)
d.Set("name_with_path", strings.Replace(aws.StringValue(output.Role.Path), "/", "", 1)+
aws.StringValue(output.Role.RoleName))
d.Set("permissions_boundary", "")
if output.Role.PermissionsBoundary != nil {
d.Set("permissions_boundary", output.Role.PermissionsBoundary.PermissionsBoundaryArn)
Expand Down
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 @@ -24,6 +24,7 @@ func TestAccAWSDataSourceIAMRole_basic(t *testing.T) {
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.TestCheckResourceAttr("data.aws_iam_role.test", "name_with_path", "testpath/"+roleName),
resource.TestCheckResourceAttrSet("data.aws_iam_role.test", "create_date"),
resource.TestMatchResourceAttr("data.aws_iam_role.test", "arn",
regexp.MustCompile(`^arn:[\w-]+:([a-zA-Z0-9\-])+:([a-z]{2}-(gov-)?[a-z]+-\d{1})?:(\d{12})?:(.*)$`)),
Expand Down
8 changes: 8 additions & 0 deletions aws/resource_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/url"
"regexp"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -57,6 +58,11 @@ func resourceAwsIamRole() *schema.Resource {
},
},

"name_with_path": {
Type: schema.TypeString,
Computed: true,
},

"name_prefix": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -210,6 +216,8 @@ func resourceAwsIamRoleRead(d *schema.ResourceData, meta interface{}) error {
d.Set("description", role.Description)
d.Set("max_session_duration", role.MaxSessionDuration)
d.Set("name", role.RoleName)
d.Set("name_with_path", strings.Replace(aws.StringValue(role.Path), "/", "", 1)+
aws.StringValue(role.RoleName))
d.Set("path", role.Path)
if role.PermissionsBoundary != nil {
d.Set("permissions_boundary", role.PermissionsBoundary.PermissionsBoundaryArn)
Expand Down
33 changes: 33 additions & 0 deletions aws/resource_aws_iam_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ func TestAccAWSIAMRole_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists("aws_iam_role.role", &conf),
resource.TestCheckResourceAttr("aws_iam_role.role", "path", "/"),
resource.TestCheckResourceAttr("aws_iam_role.role", "name_with_path", "test-role-"+rName),
resource.TestCheckResourceAttrSet("aws_iam_role.role", "create_date"),
),
},
},
})
}

func TestAccAWSIAMRole_basicWithPath(t *testing.T) {
var conf iam.GetRoleOutput
rName := acctest.RandString(10)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRoleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSIAMRoleConfigWithPath(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists("aws_iam_role.role", &conf),
resource.TestCheckResourceAttr("aws_iam_role.role", "path", "/foo/bar/"),
resource.TestCheckResourceAttr("aws_iam_role.role", "name_with_path", "foo/bar/test-role-"+rName),
resource.TestCheckResourceAttrSet("aws_iam_role.role", "create_date"),
),
},
Expand Down Expand Up @@ -482,6 +505,16 @@ resource "aws_iam_role" "role" {
`, rName)
}

func testAccAWSIAMRoleConfigWithPath(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "role" {
name = "test-role-%s"
path = "/foo/bar/"
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
}
`, rName)
}

func testAccAWSIAMRoleConfigWithDescription(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "role" {
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 @@ -28,6 +28,7 @@ data "aws_iam_role" "example" {

* `id` - The friendly IAM role name to match.
* `arn` - The Amazon Resource Name (ARN) specifying the role.
* `name_with_path` - The [friendly name](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html#identifiers-friendly-names) of the role including the path without the leading slash.
* `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.
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/iam_role.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ In addition to all arguments above, the following attributes are exported:
* `arn` - The Amazon Resource Name (ARN) specifying the role.
* `create_date` - The creation date of the IAM role.
* `unique_id` - The stable and unique string identifying the role.
* `name` - The name of the role.
* `name` - The name of the role, without the path.
* `name_with_path` - The [friendly name](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html#identifiers-friendly-names) of the role including the path without the leading slash.
* `description` - The description of the role.

## Example of Using Data Source for Assume Role Policy
Expand Down