provider/aws: Add S3 Bucket Object (supercedes #2079)
This commit is contained in:
parent
f6bad310ee
commit
44f470514c
|
@ -7,9 +7,8 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
|
||||||
"github.com/awslabs/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/awslabs/aws-sdk-go/aws/awsutil"
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
"github.com/awslabs/aws-sdk-go/service/s3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func resourceAwsS3BucketObject() *schema.Resource {
|
func resourceAwsS3BucketObject() *schema.Resource {
|
||||||
|
@ -37,6 +36,11 @@ func resourceAwsS3BucketObject() *schema.Resource {
|
||||||
Required: true,
|
Required: true,
|
||||||
ForceNew: 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)
|
file, err := os.Open(source)
|
||||||
|
|
||||||
if err != nil {
|
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(
|
resp, err := s3conn.PutObject(
|
||||||
|
@ -63,11 +66,11 @@ func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) erro
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.SetId("")
|
|
||||||
return fmt.Errorf("Error putting object in S3 bucket (%s): %s", bucket, err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,21 +79,23 @@ func resourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) err
|
||||||
|
|
||||||
bucket := d.Get("bucket").(string)
|
bucket := d.Get("bucket").(string)
|
||||||
key := d.Get("key").(string)
|
key := d.Get("key").(string)
|
||||||
|
etag := d.Get("etag").(string)
|
||||||
|
|
||||||
resp, err := s3conn.HeadObject(
|
resp, err := s3conn.HeadObject(
|
||||||
&s3.HeadObjectInput{
|
&s3.HeadObjectInput{
|
||||||
Bucket: aws.String(bucket),
|
Bucket: aws.String(bucket),
|
||||||
Key: aws.String(key),
|
Key: aws.String(key),
|
||||||
IfMatch: aws.String(d.Id()),
|
IfMatch: aws.String(etag),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// if there is an error reading the object we assume it's not there.
|
// if there is an error reading the object we assume it's not there.
|
||||||
d.SetId("")
|
d.SetId("")
|
||||||
log.Printf("Error Reading Object (%s): %s", key, err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,14 +2,15 @@ package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
|
||||||
"github.com/hashicorp/terraform/terraform"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/awslabs/aws-sdk-go/aws"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/awslabs/aws-sdk-go/service/s3"
|
"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")
|
var tf, err = ioutil.TempFile("", "tf")
|
||||||
|
@ -47,7 +48,7 @@ func testAccCheckAWSS3BucketObjectDestroy(s *terraform.State) error {
|
||||||
&s3.HeadObjectInput{
|
&s3.HeadObjectInput{
|
||||||
Bucket: aws.String(rs.Primary.Attributes["bucket"]),
|
Bucket: aws.String(rs.Primary.Attributes["bucket"]),
|
||||||
Key: aws.String(rs.Primary.Attributes["key"]),
|
Key: aws.String(rs.Primary.Attributes["key"]),
|
||||||
IfMatch: aws.String(rs.Primary.ID),
|
IfMatch: aws.String(rs.Primary.Attributes["etag"]),
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return fmt.Errorf("AWS S3 Object still exists: %s", rs.Primary.ID)
|
return fmt.Errorf("AWS S3 Object still exists: %s", rs.Primary.ID)
|
||||||
|
@ -75,7 +76,7 @@ func testAccCheckAWSS3BucketObjectExists(n string) resource.TestCheckFunc {
|
||||||
&s3.GetObjectInput{
|
&s3.GetObjectInput{
|
||||||
Bucket: aws.String(rs.Primary.Attributes["bucket"]),
|
Bucket: aws.String(rs.Primary.Attributes["bucket"]),
|
||||||
Key: aws.String(rs.Primary.Attributes["key"]),
|
Key: aws.String(rs.Primary.Attributes["key"]),
|
||||||
IfMatch: aws.String(rs.Primary.ID),
|
IfMatch: aws.String(rs.Primary.Attributes["etag"]),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("S3Bucket Object error: %s", err)
|
return fmt.Errorf("S3Bucket Object error: %s", err)
|
||||||
|
@ -89,10 +90,10 @@ var testAccAWSS3BucketObjectConfig = fmt.Sprintf(`
|
||||||
resource "aws_s3_bucket" "object_bucket" {
|
resource "aws_s3_bucket" "object_bucket" {
|
||||||
bucket = "tf-object-test-bucket-%d"
|
bucket = "tf-object-test-bucket-%d"
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_s3_bucket_object" "object" {
|
resource "aws_s3_bucket_object" "object" {
|
||||||
depends_on = "aws_s3_bucket.object_bucket"
|
bucket = "${aws_s3_bucket.object_bucket.bucket}"
|
||||||
bucket = "tf-object-test-bucket-%d"
|
|
||||||
key = "test-key"
|
key = "test-key"
|
||||||
source = "%s"
|
source = "%s"
|
||||||
}
|
}
|
||||||
`, randomBucket, randomBucket, tf.Name())
|
`, randomBucket, tf.Name())
|
||||||
|
|
Loading…
Reference in New Issue