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_s3_bucket_policy: add import ability #6543

Merged
merged 1 commit into from
Nov 25, 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
6 changes: 6 additions & 0 deletions aws/resource_aws_s3_bucket_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func resourceAwsS3BucketPolicy() *schema.Resource {
Read: resourceAwsS3BucketPolicyRead,
Update: resourceAwsS3BucketPolicyPut,
Delete: resourceAwsS3BucketPolicyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"bucket": {
Expand Down Expand Up @@ -86,6 +89,9 @@ func resourceAwsS3BucketPolicyRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("policy", v); err != nil {
return err
}
if err := d.Set("bucket", d.Id()); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bflad not sure if this will break anything - had to do it in order for import to work correctly though.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a valid (non-breaking) change, thanks!

return err
}

return nil
}
Expand Down
13 changes: 12 additions & 1 deletion aws/resource_aws_s3_bucket_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/jen20/awspolicyequivalence"
awspolicy "github.com/jen20/awspolicyequivalence"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

goimports added this automatically, let me know if I need to revert the change.

)

func TestAccAWSS3BucketPolicy_basic(t *testing.T) {
Expand Down Expand Up @@ -39,6 +39,11 @@ func TestAccAWSS3BucketPolicy_basic(t *testing.T) {
testAccCheckAWSS3BucketHasPolicy("aws_s3_bucket.bucket", expectedPolicyText),
),
},
{
ResourceName: "aws_s3_bucket_policy.bucket",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -89,6 +94,12 @@ func TestAccAWSS3BucketPolicy_policyUpdate(t *testing.T) {
testAccCheckAWSS3BucketHasPolicy("aws_s3_bucket.bucket", expectedPolicyText2),
),
},

{
ResourceName: "aws_s3_bucket_policy.bucket",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
10 changes: 8 additions & 2 deletions website/docs/r/s3_bucket_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ resource "aws_s3_bucket_policy" "b" {
"Resource": "arn:aws:s3:::my_tf_test_bucket/*",
"Condition": {
"IpAddress": {"aws:SourceIp": "8.8.8.8/32"}
}
}
}
}
]
}
POLICY
Expand All @@ -49,3 +49,9 @@ The following arguments are supported:

* `bucket` - (Required) The name of the bucket to which to apply the policy.
* `policy` - (Required) The text of the policy. For more information about building AWS IAM policy documents with Terraform, see the [AWS IAM Policy Document Guide](/docs/providers/aws/guides/iam-policy-documents.html).

## Import
S3 bucket policies can be imported using the bucket name, e.g.
```
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 an extra space snuck in here, which would likely break the page formatting. Maybe someday we'll get markdownlint running against pull requests to catch this. 😅 I'll fix this on merge.

$ terraform import aws_s3_bucket_policy.example my-bucket-name
```