From 24ac9969637062ccd4cb6e49f98ed01c9b6e1d38 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 23 Feb 2016 12:16:36 +0000 Subject: [PATCH] provider/aws: Add support for versioned objects --- .../aws/resource_aws_s3_bucket_object.go | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/builtin/providers/aws/resource_aws_s3_bucket_object.go b/builtin/providers/aws/resource_aws_s3_bucket_object.go index ae1214ab6..f1a4d24aa 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket_object.go +++ b/builtin/providers/aws/resource_aws_s3_bucket_object.go @@ -82,6 +82,11 @@ func resourceAwsS3BucketObject() *schema.Resource { Optional: true, Computed: true, }, + + "version_id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -146,6 +151,7 @@ func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) erro // See https://forums.aws.amazon.com/thread.jspa?threadID=44003 d.Set("etag", strings.Trim(*resp.ETag, `"`)) + d.Set("version_id", resp.VersionId) d.SetId(key) return resourceAwsS3BucketObjectRead(d, meta) } @@ -179,6 +185,7 @@ func resourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) err d.Set("content_encoding", resp.ContentEncoding) d.Set("content_language", resp.ContentLanguage) d.Set("content_type", resp.ContentType) + d.Set("version_id", resp.VersionId) log.Printf("[DEBUG] Reading S3 Bucket Object meta: %s", resp) return nil @@ -190,13 +197,40 @@ func resourceAwsS3BucketObjectDelete(d *schema.ResourceData, meta interface{}) e bucket := d.Get("bucket").(string) key := d.Get("key").(string) - _, err := s3conn.DeleteObject( - &s3.DeleteObjectInput{ + if _, ok := d.GetOk("version_id"); ok { + // Bucket is versioned, we need to delete all versions + vInput := s3.ListObjectVersionsInput{ + Bucket: aws.String(bucket), + Prefix: aws.String(key), + } + out, err := s3conn.ListObjectVersions(&vInput) + if err != nil { + return fmt.Errorf("Failed listing S3 object versions: %s", err) + } + + for _, v := range out.Versions { + input := s3.DeleteObjectInput{ + Bucket: aws.String(bucket), + Key: aws.String(key), + VersionId: v.VersionId, + } + _, err := s3conn.DeleteObject(&input) + if err != nil { + return fmt.Errorf("Error deleting S3 object version of %s:\n %s:\n %s", + key, v, err) + } + } + } else { + // Just delete the object + input := s3.DeleteObjectInput{ Bucket: aws.String(bucket), Key: aws.String(key), - }) - if err != nil { - return fmt.Errorf("Error deleting S3 bucket object: %s", err) + } + _, err := s3conn.DeleteObject(&input) + if err != nil { + return fmt.Errorf("Error deleting S3 bucket object: %s", err) + } } + return nil }