Skip to content

Commit

Permalink
Import fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bill-rich committed Jul 27, 2021
1 parent 2d0ce0b commit 7c36be7
Showing 1 changed file with 24 additions and 36 deletions.
60 changes: 24 additions & 36 deletions aws/resource_aws_s3_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func resourceAwsS3BucketObject() *schema.Resource {
Delete: resourceAwsS3BucketObjectDelete,

Importer: &schema.ResourceImporter{
State: importState,
State: resourceAwsS3BucketObjectImport,
},

CustomizeDiff: customdiff.Sequence(
Expand Down Expand Up @@ -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 <bucket>/<key> or s3://<bucket>/<key>", id)
}
parts := strings.Split(id, "/")

if len(parts) < 2 {
return []*schema.ResourceData{d}, fmt.Errorf("id %s should be in format <bucket>/<key> or s3://<bucket>/<key>", 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
Expand Down Expand Up @@ -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 <bucket>/<key> or s3://<bucket>/<key>", 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 {
Expand Down

0 comments on commit 7c36be7

Please sign in to comment.