-
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
service/iam: Import inline IAM user policies on terraform import of IAM user #2931
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0e99fac
test/aws_iam_user: Create failing test for iam user import with policy
erran 3ee9a15
r/aws_iam_user: Import inline policies with iam user
erran 0e311a5
r/aws_iam_user_policy: Add support to import state
erran 42444ca
Merge branch 'master' into import-iam-user-policy
erran a954d97
test/aws_iam_user_policy: Validate ParseId put policy
erran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func awsIamUserInlinePolicies(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
results := make([]*schema.ResourceData, 0) | ||
conn := meta.(*AWSClient).iamconn | ||
policyNames := make([]*string, 0) | ||
err := conn.ListUserPoliciesPages(&iam.ListUserPoliciesInput{ | ||
UserName: aws.String(d.Id()), | ||
}, func(page *iam.ListUserPoliciesOutput, lastPage bool) bool { | ||
for _, policyName := range page.PolicyNames { | ||
policyNames = append(policyNames, policyName) | ||
} | ||
|
||
return lastPage | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, policyName := range policyNames { | ||
policy, err := conn.GetUserPolicy(&iam.GetUserPolicyInput{ | ||
PolicyName: policyName, | ||
UserName: aws.String(d.Id()), | ||
}) | ||
if err != nil { | ||
return nil, errwrap.Wrapf("Error importing AWS IAM User Policy: {{err}}", err) | ||
} | ||
|
||
policyResource := resourceAwsIamUserPolicy() | ||
pData := policyResource.Data(nil) | ||
pData.SetId(fmt.Sprintf("%s:%s", *policy.UserName, *policy.PolicyName)) | ||
pData.SetType("aws_iam_user_policy") | ||
pData.Set("name", policy.PolicyName) | ||
pData.Set("policy", policy.PolicyDocument) | ||
pData.Set("user", policy.UserName) | ||
results = append(results, pData) | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
func resourceAwsIamUserImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
results := make([]*schema.ResourceData, 1) | ||
results[0] = d | ||
|
||
policyData, err := awsIamUserInlinePolicies(d, meta) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, data := range policyData { | ||
results = append(results, data) | ||
} | ||
|
||
return results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
pulled in post-merge, we should remove this.