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 members attribute to iam_group data source #7132

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
40 changes: 40 additions & 0 deletions aws/data_source_aws_iam_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ func dataSourceAwsIAMGroup() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"users": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"user_id": {
Type: schema.TypeString,
Computed: true,
},
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -58,6 +82,22 @@ func dataSourceAwsIAMGroupRead(d *schema.ResourceData, meta interface{}) error {
d.Set("arn", group.Arn)
d.Set("path", group.Path)
d.Set("group_id", group.GroupId)
if err := d.Set("users", dataSourceUsersRead(resp.Users)); err != nil {
return fmt.Errorf("error setting users: %s", err)
}

return nil
}

func dataSourceUsersRead(iamUsers []*iam.User) []map[string]interface{} {
users := make([]map[string]interface{}, 0, len(iamUsers))
for _, i := range iamUsers {
u := make(map[string]interface{})
u["arn"] = aws.StringValue(i.Arn)
u["user_id"] = aws.StringValue(i.UserId)
u["user_name"] = aws.StringValue(i.UserName)
u["path"] = aws.StringValue(i.Path)
users = append(users, u)
}
return users
}
53 changes: 51 additions & 2 deletions aws/data_source_aws_iam_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand All @@ -22,7 +21,34 @@ func TestAccAWSDataSourceIAMGroup_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.aws_iam_group.test", "group_id"),
resource.TestCheckResourceAttr("data.aws_iam_group.test", "path", "/"),
resource.TestCheckResourceAttr("data.aws_iam_group.test", "group_name", groupName),
resource.TestMatchResourceAttr("data.aws_iam_group.test", "arn", regexp.MustCompile("^arn:aws:iam::[0-9]{12}:group/"+groupName)),
testAccCheckResourceAttrGlobalARN("data.aws_iam_group.test", "arn", "iam", fmt.Sprintf("group/%s", groupName)),
),
},
},
})
}

func TestAccAWSDataSourceIAMGroup_users(t *testing.T) {
groupName := fmt.Sprintf("test-datasource-group-%d", acctest.RandInt())
userName := fmt.Sprintf("test-datasource-user-%d", acctest.RandInt())
groupMemberShipName := fmt.Sprintf("test-datasource-group-membership-%d", acctest.RandInt())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAwsIAMGroupConfigWithUser(groupName, userName, groupMemberShipName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.aws_iam_group.test", "group_id"),
resource.TestCheckResourceAttr("data.aws_iam_group.test", "path", "/"),
resource.TestCheckResourceAttr("data.aws_iam_group.test", "group_name", groupName),
testAccCheckResourceAttrGlobalARN("data.aws_iam_group.test", "arn", "iam", fmt.Sprintf("group/%s", groupName)),
resource.TestCheckResourceAttr("data.aws_iam_group.test", "users.#", "1"),
resource.TestCheckResourceAttrPair("data.aws_iam_group.test", "users.0.arn", "aws_iam_user.user", "arn"),
resource.TestCheckResourceAttrPair("data.aws_iam_group.test", "users.0.user_id", "aws_iam_user.user", "unique_id"),
resource.TestCheckResourceAttrPair("data.aws_iam_group.test", "users.0.user_name", "aws_iam_user.user", "name"),
resource.TestCheckResourceAttrPair("data.aws_iam_group.test", "users.0.path", "aws_iam_user.user", "path"),
),
},
},
Expand All @@ -41,3 +67,26 @@ data "aws_iam_group" "test" {
}
`, name)
}

func testAccAwsIAMGroupConfigWithUser(groupName, userName, membershipName string) string {
return fmt.Sprintf(`
resource "aws_iam_group" "group" {
name = "%s"
path = "/"
}

resource "aws_iam_user" "user" {
name = "%s"
}

resource "aws_iam_group_membership" "team" {
name = "%s"
users = ["${aws_iam_user.user.name}"]
group = "${aws_iam_group.group.name}"
}

data "aws_iam_group" "test" {
group_name = "${aws_iam_group_membership.team.group}"
}
`, groupName, userName, membershipName)
}
12 changes: 12 additions & 0 deletions website/docs/d/iam_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ data "aws_iam_group" "example" {
* `path` - The path to the group.

* `group_id` - The stable and unique string identifying the group.

* `users` - List of objects containing group member information. See supported fields below.

### `users`

* `arn` - The Amazon Resource Name (ARN) specifying the iam user.

* `user_id` - The stable and unique string identifying the iam user.

* `user_name` - The name of the iam user.

* `path` - The path to the iam user.