provider/aws: Add S3 Bucket Object (supercedes #2079)

This commit is contained in:
Clint Shryock 2015-07-30 15:17:37 -05:00
parent f6bad310ee
commit 44f470514c
2 changed files with 26 additions and 20 deletions

View File

@ -7,9 +7,8 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/aws/awsutil"
"github.com/awslabs/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
func resourceAwsS3BucketObject() *schema.Resource {
@ -37,6 +36,11 @@ func resourceAwsS3BucketObject() *schema.Resource {
Required: true,
ForceNew: true,
},
"etag": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
@ -51,8 +55,7 @@ func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) erro
file, err := os.Open(source)
if err != nil {
d.SetId("")
return fmt.Errorf("Error opening S3 bucket object source(%s): %s", source, err)
return fmt.Errorf("Error opening S3 bucket object source (%s): %s", source, err)
}
resp, err := s3conn.PutObject(
@ -63,11 +66,11 @@ func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) erro
})
if err != nil {
d.SetId("")
return fmt.Errorf("Error putting object in S3 bucket (%s): %s", bucket, err)
}
d.SetId(*resp.ETag)
d.Set("etag", resp.ETag)
d.SetId(key)
return nil
}
@ -76,21 +79,23 @@ func resourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) err
bucket := d.Get("bucket").(string)
key := d.Get("key").(string)
etag := d.Get("etag").(string)
resp, err := s3conn.HeadObject(
&s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
IfMatch: aws.String(d.Id()),
Bucket: aws.String(bucket),
Key: aws.String(key),
IfMatch: aws.String(etag),
})
if err != nil {
// if there is an error reading the object we assume it's not there.
d.SetId("")
log.Printf("Error Reading Object (%s): %s", key, err)
return nil
}
log.Printf(awsutil.StringValue(resp))
log.Printf("[DEBUG] Reading S3 Bucket Object meta: %s", resp)
return nil
}

View File

@ -2,14 +2,15 @@ package aws
import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"io/ioutil"
"os"
"testing"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
var tf, err = ioutil.TempFile("", "tf")
@ -47,7 +48,7 @@ func testAccCheckAWSS3BucketObjectDestroy(s *terraform.State) error {
&s3.HeadObjectInput{
Bucket: aws.String(rs.Primary.Attributes["bucket"]),
Key: aws.String(rs.Primary.Attributes["key"]),
IfMatch: aws.String(rs.Primary.ID),
IfMatch: aws.String(rs.Primary.Attributes["etag"]),
})
if err == nil {
return fmt.Errorf("AWS S3 Object still exists: %s", rs.Primary.ID)
@ -75,7 +76,7 @@ func testAccCheckAWSS3BucketObjectExists(n string) resource.TestCheckFunc {
&s3.GetObjectInput{
Bucket: aws.String(rs.Primary.Attributes["bucket"]),
Key: aws.String(rs.Primary.Attributes["key"]),
IfMatch: aws.String(rs.Primary.ID),
IfMatch: aws.String(rs.Primary.Attributes["etag"]),
})
if err != nil {
return fmt.Errorf("S3Bucket Object error: %s", err)
@ -89,10 +90,10 @@ var testAccAWSS3BucketObjectConfig = fmt.Sprintf(`
resource "aws_s3_bucket" "object_bucket" {
bucket = "tf-object-test-bucket-%d"
}
resource "aws_s3_bucket_object" "object" {
depends_on = "aws_s3_bucket.object_bucket"
bucket = "tf-object-test-bucket-%d"
bucket = "${aws_s3_bucket.object_bucket.bucket}"
key = "test-key"
source = "%s"
}
`, randomBucket, randomBucket, tf.Name())
`, randomBucket, tf.Name())