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 import support for aws_iam_user_group_membership resource #6976

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

"github.com/aws/aws-sdk-go/service/iam"

Expand All @@ -16,6 +17,9 @@ func resourceAwsIamUserGroupMembership() *schema.Resource {
Read: resourceAwsIamUserGroupMembershipRead,
Update: resourceAwsIamUserGroupMembershipUpdate,
Delete: resourceAwsIamUserGroupMembershipDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsIamUserGroupMembershipImport,
},

Schema: map[string]*schema.Schema{
"user": {
Expand Down Expand Up @@ -162,3 +166,19 @@ func addUserToGroups(conn *iam.IAM, user string, groups []*string) error {

return nil
}

func resourceAwsIamUserGroupMembershipImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) < 2 {
return nil, fmt.Errorf("unexpected format of ID (%q), expected <user-name>/<group-name1>/...", d.Id())
}

userName := idParts[0]
groupList := idParts[1:]

d.Set("user", userName)
d.Set("groups", groupList)
d.SetId(resource.UniqueId())

return []*schema.ResourceData{d}, nil
}
33 changes: 33 additions & 0 deletions aws/resource_aws_iam_user_group_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"strconv"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -35,6 +36,21 @@ func TestAccAWSUserGroupMembership_basic(t *testing.T) {
testAccAWSUserGroupMembershipCheckGroupListForUser(userName1, []string{groupName1}, []string{groupName2, groupName3}),
),
},
{
ResourceName: "aws_iam_user_group_membership.user1_test1",
ImportState: true,
ImportStateIdFunc: testAccAWSUserGroupMembershipImportStateIdFunc("aws_iam_user_group_membership.user1_test1"),
// We do not have a way to align IDs since the Create function uses resource.UniqueId()
// Failed state verification, resource with ID USER/GROUP not found
//ImportStateVerify: true,
ImportStateCheck: func(s []*terraform.InstanceState) error {
if len(s) != 1 {
return fmt.Errorf("expected 1 state: %#v", s)
}

return nil
},
},
// test adding an additional group to an existing resource
{
Config: usersAndGroupsConfig + testAccAWSUserGroupMembershipConfigAddOne,
Expand Down Expand Up @@ -161,6 +177,23 @@ func testAccAWSUserGroupMembershipCheckGroupListForUser(userName string, groups
}
}

func testAccAWSUserGroupMembershipImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

groupCount, _ := strconv.Atoi(rs.Primary.Attributes["groups.#"])
stateId := rs.Primary.Attributes["user"]
for i := 0; i < groupCount; i++ {
groupName := rs.Primary.Attributes[fmt.Sprintf("group.%d", i)]
stateId = fmt.Sprintf("%s/%s", stateId, groupName)
}
return stateId, nil
}
}

// users and groups for all other tests
func testAccAWSUserGroupMembershipConfigUsersAndGroups(userName1, userName2, groupName1, groupName2, groupName3 string) string {
return fmt.Sprintf(`
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/iam_user_group_membership.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ The following arguments are supported:
[1]: /docs/providers/aws/r/iam_group.html
[2]: /docs/providers/aws/r/iam_user.html
[3]: /docs/providers/aws/r/iam_group_membership.html

## Import

IAM user group membership can be imported using the user name and group names separated by `/`.

```
$ terraform import aws_iam_user_group_membership.example1 user1/group1/group2
```