Skip to content

Commit

Permalink
Merge pull request #3200 from zpatrick/master
Browse files Browse the repository at this point in the history
Optional "content" argument for "s3_bucket_object"
  • Loading branch information
apparentlymart committed Oct 10, 2015
2 parents b9a6215 + d261117 commit a888cd6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
36 changes: 28 additions & 8 deletions builtin/providers/aws/resource_aws_s3_bucket_object.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package aws

import (
"bytes"
"fmt"
"io"
"log"
"os"

Expand Down Expand Up @@ -32,9 +34,17 @@ func resourceAwsS3BucketObject() *schema.Resource {
},

"source": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"content"},
},

"content": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"source"},
},

"etag": &schema.Schema{
Expand All @@ -50,19 +60,28 @@ func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) erro

bucket := d.Get("bucket").(string)
key := d.Get("key").(string)
source := d.Get("source").(string)
var body io.ReadSeeker

file, err := os.Open(source)
if v, ok := d.GetOk("source"); ok {
source := v.(string)
file, err := os.Open(source)
if err != nil {
return fmt.Errorf("Error opening S3 bucket object source (%s): %s", source, err)
}

if err != nil {
return fmt.Errorf("Error opening S3 bucket object source (%s): %s", source, err)
body = file
} else if v, ok := d.GetOk("content"); ok {
content := v.(string)
body = bytes.NewReader([]byte(content))
} else {
return fmt.Errorf("Must specify \"source\" or \"content\" field")
}

resp, err := s3conn.PutObject(
&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: file,
Body: body,
})

if err != nil {
Expand Down Expand Up @@ -118,3 +137,4 @@ func resourceAwsS3BucketObjectDelete(d *schema.ResourceData, meta interface{}) e
}
return nil
}

46 changes: 38 additions & 8 deletions builtin/providers/aws/resource_aws_s3_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var tf, err = ioutil.TempFile("", "tf")

func TestAccAWSS3BucketObject_basic(t *testing.T) {
func TestAccAWSS3BucketObject_source(t *testing.T) {
// first write some data to the tempfile just so it's not 0 bytes.
ioutil.WriteFile(tf.Name(), []byte("{anything will do }"), 0644)
resource.Test(t, resource.TestCase{
Expand All @@ -29,7 +29,26 @@ func TestAccAWSS3BucketObject_basic(t *testing.T) {
CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSS3BucketObjectConfig,
Config: testAccAWSS3BucketObjectConfigSource,
Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"),
},
},
})
}

func TestAccAWSS3BucketObject_content(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
if err != nil {
panic(err)
}
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSS3BucketObjectConfigContent,
Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"),
},
},
Expand Down Expand Up @@ -86,14 +105,25 @@ func testAccCheckAWSS3BucketObjectExists(n string) resource.TestCheckFunc {
}

var randomBucket = randInt
var testAccAWSS3BucketObjectConfig = fmt.Sprintf(`
var testAccAWSS3BucketObjectConfigSource = fmt.Sprintf(`
resource "aws_s3_bucket" "object_bucket" {
bucket = "tf-object-test-bucket-%d"
bucket = "tf-object-test-bucket-%d"
}
resource "aws_s3_bucket_object" "object" {
bucket = "${aws_s3_bucket.object_bucket.bucket}"
key = "test-key"
source = "%s"
bucket = "${aws_s3_bucket.object_bucket.bucket}"
key = "test-key"
source = "%s"
}
`, randomBucket, tf.Name())

var testAccAWSS3BucketObjectConfigContent = fmt.Sprintf(`
resource "aws_s3_bucket" "object_bucket" {
bucket = "tf-object-test-bucket-%d"
}
resource "aws_s3_bucket_object" "object" {
bucket = "${aws_s3_bucket.object_bucket.bucket}"
key = "test-key"
content = "some_bucket_content"
}
`, randomBucket)

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ The following arguments are supported:

* `bucket` - (Required) The name of the bucket to put the file in.
* `key` - (Required) The name of the object once it is in the bucket.
* `source` - (Required) The path to the source file being uploaded to the bucket.
* `source` - (Required unless `content` given) The path to the source file being uploaded to the bucket.
* `content` - (Required unless `source` given) The literal content being uploaded to the bucket.

Either `source` or `content` must be provided to specify the bucket content.
These two arguments are mutually-exclusive.

## Attributes Reference

Expand Down

0 comments on commit a888cd6

Please sign in to comment.