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

resource/aws_iam_user_policy: Add support to import state #3198

Merged
merged 1 commit into from
Feb 27, 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
54 changes: 54 additions & 0 deletions aws/import_aws_iam_user_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func testAccAwsIamUserPolicyConfig(suffix string) string {
return fmt.Sprintf(`
resource "aws_iam_user" "user_%[1]s" {
name = "tf_test_user_test_%[1]s"
path = "/"
}

resource "aws_iam_user_policy" "foo_%[1]s" {
name = "tf_test_policy_test_%[1]s"
user = "${aws_iam_user.user_%[1]s.name}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}
}
EOF
}
`, suffix)
}

func TestAccAWSIAMUserPolicy_importBasic(t *testing.T) {
suffix := randomString(10)
resourceName := fmt.Sprintf("aws_iam_user_policy.foo_%s", suffix)
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick for the future: each of the acceptance tests are self-contained configurations, so we do not need to randomize the Terraform resource names (only the names in AWS) 👍


resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckIAMUserPolicyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAwsIamUserPolicyConfig(suffix),
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick for the future: you can use an existing configuration for import testing

},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
36 changes: 30 additions & 6 deletions aws/resource_aws_iam_user_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func resourceAwsIamUserPolicy() *schema.Resource {
Create: resourceAwsIamUserPolicyPut,
Update: resourceAwsIamUserPolicyPut,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Read: resourceAwsIamUserPolicyRead,
Delete: resourceAwsIamUserPolicyDelete,

Expand Down Expand Up @@ -59,8 +63,12 @@ func resourceAwsIamUserPolicyPut(d *schema.ResourceData, meta interface{}) error
}

var policyName string
var err error
if !d.IsNewResource() {
_, policyName = resourceAwsIamUserPolicyParseId(d.Id())
_, policyName, err = resourceAwsIamUserPolicyParseId(d.Id())
if err != nil {
return err
}
} else if v, ok := d.GetOk("name"); ok {
policyName = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
Expand All @@ -81,14 +89,16 @@ func resourceAwsIamUserPolicyPut(d *schema.ResourceData, meta interface{}) error
func resourceAwsIamUserPolicyRead(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn

user, name := resourceAwsIamUserPolicyParseId(d.Id())
user, name, err := resourceAwsIamUserPolicyParseId(d.Id())
if err != nil {
return err
}

request := &iam.GetUserPolicyInput{
PolicyName: aws.String(name),
UserName: aws.String(user),
}

var err error
getResp, err := iamconn.GetUserPolicy(request)
if err != nil {
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { // XXX test me
Expand All @@ -106,13 +116,22 @@ func resourceAwsIamUserPolicyRead(d *schema.ResourceData, meta interface{}) erro
if err != nil {
return err
}
return d.Set("policy", policy)
if err := d.Set("policy", policy); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick for the future: we do not need to perform error handling of d.Set() for simple string attributes 👍

return err
}
if err := d.Set("name", name); err != nil {
return err
}
return d.Set("user", user)
}

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

user, name := resourceAwsIamUserPolicyParseId(d.Id())
user, name, err := resourceAwsIamUserPolicyParseId(d.Id())
if err != nil {
return err
}

request := &iam.DeleteUserPolicyInput{
PolicyName: aws.String(name),
Expand All @@ -125,8 +144,13 @@ func resourceAwsIamUserPolicyDelete(d *schema.ResourceData, meta interface{}) er
return nil
}

func resourceAwsIamUserPolicyParseId(id string) (userName, policyName string) {
func resourceAwsIamUserPolicyParseId(id string) (userName, policyName string, err error) {
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
err = fmt.Errorf("user_policy id must be of the form <user name>:<policy name>")
return
}

userName = parts[0]
policyName = parts[1]
return
Expand Down
14 changes: 10 additions & 4 deletions aws/resource_aws_iam_user_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,16 @@ func testAccCheckIAMUserPolicyDestroy(s *terraform.State) error {
continue
}

user, name := resourceAwsIamUserPolicyParseId(rs.Primary.ID)
user, name, err := resourceAwsIamUserPolicyParseId(rs.Primary.ID)
if err != nil {
return err
}

request := &iam.GetUserPolicyInput{
PolicyName: aws.String(name),
UserName: aws.String(user),
}

var err error
getResp, err := iamconn.GetUserPolicy(request)
if err != nil {
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
Expand Down Expand Up @@ -234,8 +236,12 @@ func testAccCheckIAMUserPolicy(
}

iamconn := testAccProvider.Meta().(*AWSClient).iamconn
username, name := resourceAwsIamUserPolicyParseId(policy.Primary.ID)
_, err := iamconn.GetUserPolicy(&iam.GetUserPolicyInput{
username, name, err := resourceAwsIamUserPolicyParseId(policy.Primary.ID)
if err != nil {
return err
}

_, err = iamconn.GetUserPolicy(&iam.GetUserPolicyInput{
UserName: aws.String(username),
PolicyName: aws.String(name),
})
Expand Down