From 2b4f06f98384c0a31c23b9f1f54087a0d76a821b Mon Sep 17 00:00:00 2001 From: Austin Burdine Date: Wed, 21 Nov 2018 09:22:19 -0500 Subject: [PATCH] resource/aws_s3_bucket_policy: add import ability closes #6519 --- aws/resource_aws_s3_bucket_policy.go | 6 ++++++ aws/resource_aws_s3_bucket_policy_test.go | 13 ++++++++++++- website/docs/r/s3_bucket_policy.html.markdown | 10 ++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_s3_bucket_policy.go b/aws/resource_aws_s3_bucket_policy.go index d0f74cef0ce..ea41b6b7bce 100644 --- a/aws/resource_aws_s3_bucket_policy.go +++ b/aws/resource_aws_s3_bucket_policy.go @@ -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": { @@ -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 { + return err + } return nil } diff --git a/aws/resource_aws_s3_bucket_policy_test.go b/aws/resource_aws_s3_bucket_policy_test.go index b1ed29119df..2a5d4cce656 100644 --- a/aws/resource_aws_s3_bucket_policy_test.go +++ b/aws/resource_aws_s3_bucket_policy_test.go @@ -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" ) func TestAccAWSS3BucketPolicy_basic(t *testing.T) { @@ -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, + }, }, }) } @@ -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, + }, }, }) } diff --git a/website/docs/r/s3_bucket_policy.html.markdown b/website/docs/r/s3_bucket_policy.html.markdown index 128a32545f8..6880034ace3 100644 --- a/website/docs/r/s3_bucket_policy.html.markdown +++ b/website/docs/r/s3_bucket_policy.html.markdown @@ -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 @@ -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. + ``` +$ terraform import aws_s3_bucket_policy.example my-bucket-name +```