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

Feature/add import aws iam group policy attachment #6625

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
17 changes: 17 additions & 0 deletions aws/resource_aws_iam_group_policy_attachment.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/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -16,6 +17,9 @@ func resourceAwsIamGroupPolicyAttachment() *schema.Resource {
Create: resourceAwsIamGroupPolicyAttachmentCreate,
Read: resourceAwsIamGroupPolicyAttachmentRead,
Delete: resourceAwsIamGroupPolicyAttachmentDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsIamGroupPolicyAttachmentImport,
},

Schema: map[string]*schema.Schema{
"group": {
Expand Down Expand Up @@ -101,6 +105,19 @@ func resourceAwsIamGroupPolicyAttachmentDelete(d *schema.ResourceData, meta inte
return nil
}

func resourceAwsIamGroupPolicyAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.SplitN(d.Id(), "/", 2)
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("unexpected format of ID (%q), expected <group-name>/<policy_arn>", d.Id())
}
groupName := idParts[0]
policyARN := idParts[1]
d.Set("group", groupName)
d.Set("policy_arn", policyARN)
d.SetId(fmt.Sprintf("%s-%s", groupName, policyARN))
return []*schema.ResourceData{d}, nil
}

func attachPolicyToGroup(conn *iam.IAM, group string, arn string) error {
_, err := conn.AttachGroupPolicy(&iam.AttachGroupPolicyInput{
GroupName: aws.String(group),
Expand Down
28 changes: 28 additions & 0 deletions aws/resource_aws_iam_group_policy_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ func TestAccAWSIAMGroupPolicyAttachment_basic(t *testing.T) {
testAccCheckAWSGroupPolicyAttachmentAttributes([]string{policyName}, &out),
),
},
{
ResourceName: "aws_iam_group_policy_attachment.test-attach",
ImportState: true,
ImportStateIdFunc: testAccAWSIAMGroupPolicyAttachmentImportStateIdFunc("aws_iam_group_policy_attachment.test-attach"),
// We do not have a way to align IDs since the Create function uses resource.PrefixedUniqueId()
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks so much for this comment. 😄

// Failed state verification, resource with ID GROUP-POLICYARN not found
// ImportStateVerify: true,
ImportStateCheck: func(s []*terraform.InstanceState) error {
if len(s) != 1 {
return fmt.Errorf("expected 1 state: %#v", s)
}
rs := s[0]
if !strings.HasPrefix(rs.Attributes["policy_arn"], "arn:") {
return fmt.Errorf("expected policy_arn attribute to be set and begin with arn:, received: %s", rs.Attributes["policy_arn"])
}
return nil
},
},
{
Config: testAccAWSGroupPolicyAttachConfigUpdate(groupName, policyName, policyName2, policyName3),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -201,3 +219,13 @@ resource "aws_iam_group_policy_attachment" "test-attach2" {
}
`, groupName, policyName, policyName2, policyName3)
}

func testAccAWSIAMGroupPolicyAttachmentImportStateIdFunc(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)
}
return fmt.Sprintf("%s/%s", rs.Primary.Attributes["group"], rs.Primary.Attributes["policy_arn"]), nil
}
}
6 changes: 6 additions & 0 deletions website/docs/r/iam_group_policy_attachment.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ The following arguments are supported:

* `group` (Required) - The group the policy should be applied to
* `policy_arn` (Required) - The ARN of the policy you want to apply

## Import
IAM group policy attachments can be imported using the group name and policy arn separated by `/`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like there's an additional space in front of this line and the next. Will fix on merge. 👍

```
$ terraform import aws_iam_group_policy_attachment.test-attach test-group/arn:aws:iam::xxxxxxxxxxxx:policy/test-policy
```