provider/aws: Add support for versioned objects

This commit is contained in:
Radek Simko 2016-02-23 12:16:36 +00:00
parent 2f8b9edaf8
commit 24ac996963
1 changed files with 39 additions and 5 deletions

View File

@ -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),
}
_, err := s3conn.DeleteObject(&input)
if err != nil {
return fmt.Errorf("Error deleting S3 bucket object: %s", err)
}
}
return nil
}