2014-07-21 20:34:47 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-02-19 22:38:56 +01:00
|
|
|
|
2015-04-14 18:07:05 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/s3"
|
2014-07-21 20:34:47 +02:00
|
|
|
)
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsS3Bucket() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsS3BucketCreate,
|
|
|
|
Read: resourceAwsS3BucketRead,
|
2015-03-26 15:17:27 +01:00
|
|
|
Update: resourceAwsS3BucketUpdate,
|
2014-11-21 17:58:34 +01:00
|
|
|
Delete: resourceAwsS3BucketDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"bucket": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"acl": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Default: "private",
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-03-26 15:17:27 +01:00
|
|
|
|
2015-04-26 01:36:00 +02:00
|
|
|
"website": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Default: false,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
"index_document": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
|
2015-04-29 18:16:01 +02:00
|
|
|
"error_document": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
|
2015-03-26 15:17:27 +01:00
|
|
|
"tags": tagsSchema(),
|
2014-07-21 20:34:47 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
s3conn := meta.(*AWSClient).s3conn
|
2015-02-19 22:38:56 +01:00
|
|
|
awsRegion := meta.(*AWSClient).region
|
2014-07-21 20:34:47 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// Get the bucket and acl
|
|
|
|
bucket := d.Get("bucket").(string)
|
|
|
|
acl := d.Get("acl").(string)
|
2014-07-21 20:34:47 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] S3 bucket create: %s, ACL: %s", bucket, acl)
|
2015-02-19 22:38:56 +01:00
|
|
|
|
2015-04-14 18:07:05 +02:00
|
|
|
req := &s3.CreateBucketInput{
|
2015-02-19 22:38:56 +01:00
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
ACL: aws.String(acl),
|
2015-03-02 18:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special case us-east-1 region and do not set the LocationConstraint.
|
|
|
|
// See "Request Elements: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
|
|
|
|
if awsRegion != "us-east-1" {
|
|
|
|
req.CreateBucketConfiguration = &s3.CreateBucketConfiguration{
|
2015-02-19 22:38:56 +01:00
|
|
|
LocationConstraint: aws.String(awsRegion),
|
2015-03-02 18:13:19 +01:00
|
|
|
}
|
2015-02-19 22:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := s3conn.CreateBucket(req)
|
2014-07-21 20:34:47 +02:00
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return fmt.Errorf("Error creating S3 bucket: %s", err)
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the bucket name as the resource ID
|
2014-11-21 17:58:34 +01:00
|
|
|
d.SetId(bucket)
|
2014-07-21 20:34:47 +02:00
|
|
|
|
2015-03-26 15:17:27 +01:00
|
|
|
return resourceAwsS3BucketUpdate(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
s3conn := meta.(*AWSClient).s3conn
|
|
|
|
if err := setTagsS3(s3conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-26 01:36:00 +02:00
|
|
|
|
|
|
|
if err := updateWebsite(s3conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:17:27 +01:00
|
|
|
return resourceAwsS3BucketRead(d, meta)
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
s3conn := meta.(*AWSClient).s3conn
|
2014-07-21 20:34:47 +02:00
|
|
|
|
2015-04-14 18:07:05 +02:00
|
|
|
_, err := s3conn.HeadBucket(&s3.HeadBucketInput{
|
2015-02-19 22:38:56 +01:00
|
|
|
Bucket: aws.String(d.Id()),
|
|
|
|
})
|
2014-07-21 20:34:47 +02:00
|
|
|
if err != nil {
|
2015-04-17 19:30:31 +02:00
|
|
|
if awsError, ok := err.(aws.APIError); ok && awsError.StatusCode == 404 {
|
|
|
|
d.SetId("")
|
|
|
|
} else {
|
|
|
|
// some of the AWS SDK's errors can be empty strings, so let's add
|
|
|
|
// some additional context.
|
2015-04-20 03:13:04 +02:00
|
|
|
return fmt.Errorf("error reading S3 bucket \"%s\": %s", d.Id(), err)
|
2015-04-17 19:30:31 +02:00
|
|
|
}
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|
2015-03-26 17:45:16 +01:00
|
|
|
|
2015-04-01 14:57:50 +02:00
|
|
|
tagSet, err := getTagSetS3(s3conn, d.Id())
|
2015-03-26 17:45:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-01 14:57:50 +02:00
|
|
|
if err := d.Set("tags", tagsToMapS3(tagSet)); err != nil {
|
2015-03-26 17:45:16 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
s3conn := meta.(*AWSClient).s3conn
|
2014-07-21 20:34:47 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[DEBUG] S3 Delete Bucket: %s", d.Id())
|
2015-04-14 18:07:05 +02:00
|
|
|
_, err := s3conn.DeleteBucket(&s3.DeleteBucketInput{
|
2015-02-19 22:38:56 +01:00
|
|
|
Bucket: aws.String(d.Id()),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|
2015-04-26 01:36:00 +02:00
|
|
|
|
|
|
|
func updateWebsite(s3conn *s3.S3, d *schema.ResourceData) error {
|
|
|
|
website := d.Get("website").(bool)
|
|
|
|
bucket := d.Get("bucket").(string)
|
|
|
|
indexDocument := d.Get("index_document").(string)
|
2015-04-29 18:16:01 +02:00
|
|
|
errorDocument := d.Get("error_document").(string)
|
2015-04-26 01:36:00 +02:00
|
|
|
|
|
|
|
if website {
|
2015-04-29 18:16:01 +02:00
|
|
|
websiteConfiguration := &s3.WebsiteConfiguration{}
|
|
|
|
|
|
|
|
if indexDocument != "" {
|
|
|
|
websiteConfiguration.IndexDocument = &s3.IndexDocument{Suffix: aws.String(indexDocument)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errorDocument != "" {
|
|
|
|
websiteConfiguration.ErrorDocument = &s3.ErrorDocument{Key: aws.String(errorDocument)}
|
|
|
|
}
|
|
|
|
|
|
|
|
putInput := &s3.PutBucketWebsiteInput{
|
2015-04-26 01:36:00 +02:00
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
WebsiteConfiguration: websiteConfiguration,
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:16:01 +02:00
|
|
|
log.Printf("[DEBUG] S3 put bucket website: %s", putInput)
|
2015-04-26 01:36:00 +02:00
|
|
|
|
2015-04-29 18:16:01 +02:00
|
|
|
_, err := s3conn.PutBucketWebsite(putInput)
|
2015-04-26 01:36:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error putting S3 website: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deleteInput := &s3.DeleteBucketWebsiteInput{Bucket: aws.String(bucket)}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] S3 delete bucket website: %s", deleteInput)
|
|
|
|
|
|
|
|
_, err := s3conn.DeleteBucketWebsite(deleteInput)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting S3 website: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|