-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckIAMUserPolicyDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAwsIamUserPolicyConfig(suffix), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,10 @@ func resourceAwsIamUserPolicy() *schema.Resource { | |
Create: resourceAwsIamUserPolicyPut, | ||
Update: resourceAwsIamUserPolicyPut, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Read: resourceAwsIamUserPolicyRead, | ||
Delete: resourceAwsIamUserPolicyDelete, | ||
|
||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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), | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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) 👍