From 9f9abac4fe1fa231f9ae3d77c3ad3b03b3813b26 Mon Sep 17 00:00:00 2001 From: Daniel Dersch Date: Fri, 6 Sep 2019 18:39:00 -0400 Subject: [PATCH 1/6] Allow aws_s3_bucket_object to be imported --- aws/resource_aws_s3_bucket_object.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/aws/resource_aws_s3_bucket_object.go b/aws/resource_aws_s3_bucket_object.go index cc95b3a5060..a0d84aa8fc4 100644 --- a/aws/resource_aws_s3_bucket_object.go +++ b/aws/resource_aws_s3_bucket_object.go @@ -33,6 +33,10 @@ func resourceAwsS3BucketObject() *schema.Resource { Update: resourceAwsS3BucketObjectUpdate, Delete: resourceAwsS3BucketObjectDelete, + Importer: &schema.ResourceImporter{ + State: importState, + }, + CustomizeDiff: customdiff.Sequence( resourceAwsS3BucketObjectCustomizeDiff, SetTagsDiff, @@ -323,6 +327,23 @@ func resourceAwsS3BucketObjectCreate(d *schema.ResourceData, meta interface{}) e return resourceAwsS3BucketObjectPut(d, meta) } +func importState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + id := d.Id() + if len(id) < 1 || !strings.Contains(id, "/") { + return nil, fmt.Errorf("id %s should be in format /", id) + } + bucket := strings.Split(id, "/")[0] + key := strings.Join(strings.Split(id, "/")[1:], "/") + d.Set("bucket", bucket) + d.Set("key", key) + + if err := resourceAwsS3BucketObjectRead(d, meta); err != nil { + return nil, err + } + + return []*schema.ResourceData{d}, nil +} + func resourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) error { s3conn := meta.(*AWSClient).s3conn defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig From ca21f74ddae3c8e6b399972995922538a1cf91b4 Mon Sep 17 00:00:00 2001 From: Daniel Dersch Date: Fri, 6 Sep 2019 18:48:57 -0400 Subject: [PATCH 2/6] Add documentation for importing an aws_s3_bucket_object --- website/docs/r/s3_bucket_object.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/s3_bucket_object.html.markdown b/website/docs/r/s3_bucket_object.html.markdown index 9d53f250998..2f02b0f83be 100644 --- a/website/docs/r/s3_bucket_object.html.markdown +++ b/website/docs/r/s3_bucket_object.html.markdown @@ -154,4 +154,4 @@ In addition to all arguments above, the following attributes are exported: * `etag` - ETag generated for the object (an MD5 sum of the object content). For plaintext objects or objects encrypted with an AWS-managed key, the hash is an MD5 digest of the object data. For objects encrypted with a KMS key or objects created by either the Multipart Upload or Part Copy operation, the hash is not an MD5 digest, regardless of the method of encryption. More information on possible values can be found on [Common Response Headers](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTCommonResponseHeaders.html). * `id` - `key` of the resource supplied above * `tags_all` - Map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block). -* `version_id` - Unique version ID value for the object, if bucket versioning is enabled. \ No newline at end of file +* `version_id` - Unique version ID value for the object, if bucket versioning is enabled. From a55189ea2178d6ef0c0c9de70189b020e5a6a27a Mon Sep 17 00:00:00 2001 From: Daniel Dersch Date: Fri, 6 Sep 2019 19:52:37 -0400 Subject: [PATCH 3/6] SetId to the object key for consistency --- aws/resource_aws_s3_bucket_object.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/resource_aws_s3_bucket_object.go b/aws/resource_aws_s3_bucket_object.go index a0d84aa8fc4..540e5600510 100644 --- a/aws/resource_aws_s3_bucket_object.go +++ b/aws/resource_aws_s3_bucket_object.go @@ -334,6 +334,7 @@ func importState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceDa } bucket := strings.Split(id, "/")[0] key := strings.Join(strings.Split(id, "/")[1:], "/") + d.SetId(key) d.Set("bucket", bucket) d.Set("key", key) From 4f702916f144de8e502dc074f2f4dcab17ef6244 Mon Sep 17 00:00:00 2001 From: Daniel Dersch Date: Mon, 9 Sep 2019 17:49:48 -0400 Subject: [PATCH 4/6] Acceptance tests, update docs --- aws/resource_aws_s3_bucket_object.go | 29 ++++- aws/resource_aws_s3_bucket_object_test.go | 109 +++++++++++++++++- website/docs/r/s3_bucket_object.html.markdown | 14 +++ 3 files changed, 140 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_s3_bucket_object.go b/aws/resource_aws_s3_bucket_object.go index 540e5600510..b2ff6bd9628 100644 --- a/aws/resource_aws_s3_bucket_object.go +++ b/aws/resource_aws_s3_bucket_object.go @@ -329,17 +329,34 @@ func resourceAwsS3BucketObjectCreate(d *schema.ResourceData, meta interface{}) e func importState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { id := d.Id() + + if strings.HasPrefix(id, "s3://") { + id = strings.TrimPrefix(id, "s3://") + } + if len(id) < 1 || !strings.Contains(id, "/") { - return nil, fmt.Errorf("id %s should be in format /", id) + return []*schema.ResourceData{d}, fmt.Errorf("id %s should be in format / or s3:///", id) + } + parts := strings.Split(id, "/") + + if len(parts) < 2 { + return []*schema.ResourceData{d}, fmt.Errorf("id %s should be in format / or s3:///", id) } - bucket := strings.Split(id, "/")[0] - key := strings.Join(strings.Split(id, "/")[1:], "/") + + bucket := parts[0] + key := strings.Join(parts[1:], "/") + d.SetId(key) - d.Set("bucket", bucket) - d.Set("key", key) + if err := d.Set("bucket", bucket); err != nil { + return []*schema.ResourceData{d}, err + } + + if err := d.Set("key", key); err != nil { + return []*schema.ResourceData{d}, err + } if err := resourceAwsS3BucketObjectRead(d, meta); err != nil { - return nil, err + return []*schema.ResourceData{d}, err } return []*schema.ResourceData{d}, nil diff --git a/aws/resource_aws_s3_bucket_object_test.go b/aws/resource_aws_s3_bucket_object_test.go index e7d2c0d6dae..4247293f4b6 100644 --- a/aws/resource_aws_s3_bucket_object_test.go +++ b/aws/resource_aws_s3_bucket_object_test.go @@ -146,6 +146,13 @@ func TestAccAWSS3BucketObject_empty(t *testing.T) { testAccCheckAWSS3BucketObjectBody(&obj, ""), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -171,6 +178,13 @@ func TestAccAWSS3BucketObject_source(t *testing.T) { testAccCheckAWSS3BucketObjectBody(&obj, "{anything will do }"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "source"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -194,6 +208,13 @@ func TestAccAWSS3BucketObject_content(t *testing.T) { testAccCheckAWSS3BucketObjectBody(&obj, "some_bucket_content"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "content", "content_base64"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -220,6 +241,13 @@ func TestAccAWSS3BucketObject_etagEncryption(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "etag", "7b006ff4d70f68cc65061acf2f802e6f"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "source"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -292,6 +320,13 @@ func TestAccAWSS3BucketObject_sourceHashTrigger(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "source_hash", "cffc5e20de2d21764145b1124c9b337b"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "content", "content_base64"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -337,12 +372,11 @@ func TestAccAWSS3BucketObject_nonVersioned(t *testing.T) { CheckDestroy: testAccCheckAWSS3BucketObjectDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSS3BucketObjectConfig_nonVersioned(rName, sourceInitial), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSS3BucketObjectExists(resourceName, &originalObj), - testAccCheckAWSS3BucketObjectBody(&originalObj, "initial object state"), - resource.TestCheckResourceAttr(resourceName, "version_id", ""), - ), + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "source"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), }, }, }) @@ -386,6 +420,13 @@ func TestAccAWSS3BucketObject_updates(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "object_lock_retain_until_date", ""), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "source"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/updateable-key", rInt), + }, }, }) } @@ -470,6 +511,13 @@ func TestAccAWSS3BucketObject_updatesWithVersioning(t *testing.T) { testAccCheckAWSS3BucketObjectVersionIdDiffers(&modifiedObj, &originalObj), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "source"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/updateable-key", rInt), + }, }, }) } @@ -536,6 +584,13 @@ func TestAccAWSS3BucketObject_kms(t *testing.T) { testAccCheckAWSS3BucketObjectBody(&obj, "{anything will do }"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "source"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -563,6 +618,13 @@ func TestAccAWSS3BucketObject_sse(t *testing.T) { testAccCheckAWSS3BucketObjectBody(&obj, "{anything will do }"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "source"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -607,6 +669,13 @@ func TestAccAWSS3BucketObject_acl(t *testing.T) { testAccCheckAWSS3BucketObjectAcl(resourceName, []string{"FULL_CONTROL"}), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl", "content"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -647,6 +716,13 @@ func TestAccAWSS3BucketObject_metadata(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "metadata.%", "0"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"acl"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -703,6 +779,13 @@ func TestAccAWSS3BucketObject_storageClass(t *testing.T) { testAccCheckAWSS3BucketObjectStorageClass(resourceName, "DEEP_ARCHIVE"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"content", "acl"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + }, }, }) } @@ -768,6 +851,13 @@ func TestAccAWSS3BucketObject_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"content", "acl"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/%s", rInt, key), + }, }, }) } @@ -833,6 +923,13 @@ func TestAccAWSS3BucketObject_tagsLeadingSingleSlash(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"content", "acl"}, + ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/%s", rInt, key), + }, }, }) } diff --git a/website/docs/r/s3_bucket_object.html.markdown b/website/docs/r/s3_bucket_object.html.markdown index 2f02b0f83be..abc4286868f 100644 --- a/website/docs/r/s3_bucket_object.html.markdown +++ b/website/docs/r/s3_bucket_object.html.markdown @@ -155,3 +155,17 @@ In addition to all arguments above, the following attributes are exported: * `id` - `key` of the resource supplied above * `tags_all` - Map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block). * `version_id` - Unique version ID value for the object, if bucket versioning is enabled. + +## Import + +Objects can be imported using the `id`. The `id` is the bucket name and the key together e.g. + +``` +$ terraform import aws_s3_bucket_object.object some-bucket-name/some/key.txt +``` + +Additionally, s3 url syntax can be used, e.g. + +``` +$ terraform import aws_s3_bucket_object.object s3://some-bucket-name/some/key.txt +``` From 2d0ce0b15225b974e66f26f893417fc40fc83364 Mon Sep 17 00:00:00 2001 From: bill-rich Date: Tue, 27 Jul 2021 15:59:22 -0700 Subject: [PATCH 5/6] Use rName --- aws/resource_aws_s3_bucket_object_test.go | 66 +++++++++++++---------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/aws/resource_aws_s3_bucket_object_test.go b/aws/resource_aws_s3_bucket_object_test.go index 4247293f4b6..87bbea6fa72 100644 --- a/aws/resource_aws_s3_bucket_object_test.go +++ b/aws/resource_aws_s3_bucket_object_test.go @@ -150,8 +150,8 @@ func TestAccAWSS3BucketObject_empty(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -182,8 +182,8 @@ func TestAccAWSS3BucketObject_source(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "source"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "source", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -212,8 +212,8 @@ func TestAccAWSS3BucketObject_content(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "content", "content_base64"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "content", "content_base64", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -245,8 +245,8 @@ func TestAccAWSS3BucketObject_etagEncryption(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "source"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "source", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -324,8 +324,8 @@ func TestAccAWSS3BucketObject_sourceHashTrigger(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "content", "content_base64"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "content", "content_base64", "force_destroy", "source", "source_hash"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -371,12 +371,20 @@ func TestAccAWSS3BucketObject_nonVersioned(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckAWSS3BucketObjectDestroy, Steps: []resource.TestStep{ + { + Config: testAccAWSS3BucketObjectConfig_nonVersioned(rName, sourceInitial), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSS3BucketObjectExists(resourceName, &originalObj), + testAccCheckAWSS3BucketObjectBody(&originalObj, "initial object state"), + resource.TestCheckResourceAttr(resourceName, "version_id", ""), + ), + }, { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, ImportStateVerifyIgnore: []string{"acl", "source"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -424,8 +432,8 @@ func TestAccAWSS3BucketObject_updates(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "source"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/updateable-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "source", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/updateable-key", rName), }, }, }) @@ -515,8 +523,8 @@ func TestAccAWSS3BucketObject_updatesWithVersioning(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "source"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/updateable-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "source", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/updateable-key", rName), }, }, }) @@ -588,8 +596,8 @@ func TestAccAWSS3BucketObject_kms(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "source"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "source", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -622,8 +630,8 @@ func TestAccAWSS3BucketObject_sse(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "source"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "source", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -673,8 +681,8 @@ func TestAccAWSS3BucketObject_acl(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl", "content"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "content", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -720,8 +728,8 @@ func TestAccAWSS3BucketObject_metadata(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"acl"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"acl", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -783,8 +791,8 @@ func TestAccAWSS3BucketObject_storageClass(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"content", "acl"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/test-key", rInt), + ImportStateVerifyIgnore: []string{"content", "acl", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/test-key", rName), }, }, }) @@ -855,8 +863,8 @@ func TestAccAWSS3BucketObject_tags(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"content", "acl"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/%s", rInt, key), + ImportStateVerifyIgnore: []string{"content", "acl", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/%s", rName, key), }, }, }) @@ -927,8 +935,8 @@ func TestAccAWSS3BucketObject_tagsLeadingSingleSlash(t *testing.T) { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"content", "acl"}, - ImportStateId: fmt.Sprintf("s3://tf-object-test-bucket-%d/%s", rInt, key), + ImportStateVerifyIgnore: []string{"content", "acl", "force_destroy"}, + ImportStateId: fmt.Sprintf("s3://%s/%s", rName, key), }, }, }) From 7c36be791e651599aedb4807782ff7ca76f090e2 Mon Sep 17 00:00:00 2001 From: bill-rich Date: Tue, 27 Jul 2021 16:02:34 -0700 Subject: [PATCH 6/6] Import fixes --- aws/resource_aws_s3_bucket_object.go | 60 +++++++++++----------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/aws/resource_aws_s3_bucket_object.go b/aws/resource_aws_s3_bucket_object.go index b2ff6bd9628..65f0681e875 100644 --- a/aws/resource_aws_s3_bucket_object.go +++ b/aws/resource_aws_s3_bucket_object.go @@ -34,7 +34,7 @@ func resourceAwsS3BucketObject() *schema.Resource { Delete: resourceAwsS3BucketObjectDelete, Importer: &schema.ResourceImporter{ - State: importState, + State: resourceAwsS3BucketObjectImport, }, CustomizeDiff: customdiff.Sequence( @@ -327,41 +327,6 @@ func resourceAwsS3BucketObjectCreate(d *schema.ResourceData, meta interface{}) e return resourceAwsS3BucketObjectPut(d, meta) } -func importState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - id := d.Id() - - if strings.HasPrefix(id, "s3://") { - id = strings.TrimPrefix(id, "s3://") - } - - if len(id) < 1 || !strings.Contains(id, "/") { - return []*schema.ResourceData{d}, fmt.Errorf("id %s should be in format / or s3:///", id) - } - parts := strings.Split(id, "/") - - if len(parts) < 2 { - return []*schema.ResourceData{d}, fmt.Errorf("id %s should be in format / or s3:///", id) - } - - bucket := parts[0] - key := strings.Join(parts[1:], "/") - - d.SetId(key) - if err := d.Set("bucket", bucket); err != nil { - return []*schema.ResourceData{d}, err - } - - if err := d.Set("key", key); err != nil { - return []*schema.ResourceData{d}, err - } - - if err := resourceAwsS3BucketObjectRead(d, meta); err != nil { - return []*schema.ResourceData{d}, err - } - - return []*schema.ResourceData{d}, nil -} - func resourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) error { s3conn := meta.(*AWSClient).s3conn defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig @@ -571,6 +536,29 @@ func resourceAwsS3BucketObjectDelete(d *schema.ResourceData, meta interface{}) e return nil } +func resourceAwsS3BucketObjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + id := d.Id() + + if strings.HasPrefix(id, "s3://") { + id = strings.TrimPrefix(id, "s3://") + } + + parts := strings.Split(id, "/") + + if len(parts) < 2 { + return []*schema.ResourceData{d}, fmt.Errorf("id %s should be in format / or s3:///", id) + } + + bucket := parts[0] + key := strings.Join(parts[1:], "/") + + d.SetId(key) + d.Set("bucket", bucket) + d.Set("key", key) + + return []*schema.ResourceData{d}, nil +} + func resourceAwsS3BucketObjectSetKMS(d *schema.ResourceData, meta interface{}, sseKMSKeyId *string) error { // Only set non-default KMS key ID (one that doesn't match default) if sseKMSKeyId != nil {