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{
|
2015-05-01 15:19:54 +02:00
|
|
|
Type: schema.TypeList,
|
2015-04-29 18:16:01 +02:00
|
|
|
Optional: true,
|
2015-05-01 15:19:54 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"index_document": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"error_document": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-04-29 21:47:21 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"website_endpoint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2015-04-29 18:16:01 +02:00
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
if err := resourceAwsS3BucketWebsiteUpdate(s3conn, d); err != nil {
|
2015-04-26 01:36:00 +02:00
|
|
|
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-05-06 14:12:40 +02:00
|
|
|
// Read the website configuration
|
|
|
|
ws, err := s3conn.GetBucketWebsite(&s3.GetBucketWebsiteInput{
|
|
|
|
Bucket: aws.String(d.Id()),
|
|
|
|
})
|
|
|
|
var websites []map[string]interface{}
|
|
|
|
if err == nil {
|
2015-05-06 14:40:32 +02:00
|
|
|
w := make(map[string]interface{})
|
|
|
|
|
|
|
|
w["index_document"] = *ws.IndexDocument.Suffix
|
|
|
|
|
|
|
|
if v := ws.ErrorDocument; v != nil {
|
|
|
|
w["error_document"] = *v.Key
|
|
|
|
}
|
|
|
|
|
|
|
|
websites = append(websites, w)
|
2015-05-06 14:12:40 +02:00
|
|
|
}
|
|
|
|
if err := d.Set("website", websites); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:47:21 +02:00
|
|
|
// Add website_endpoint as an output
|
|
|
|
endpoint, err := websiteEndpoint(s3conn, d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := d.Set("website_endpoint", endpoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
func resourceAwsS3BucketWebsiteUpdate(s3conn *s3.S3, d *schema.ResourceData) error {
|
|
|
|
if !d.HasChange("website") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := d.Get("website").([]interface{})
|
|
|
|
|
|
|
|
if len(ws) == 1 {
|
|
|
|
w := ws[0].(map[string]interface{})
|
|
|
|
return resourceAwsS3BucketWebsitePut(s3conn, d, w)
|
|
|
|
} else if len(ws) == 0 {
|
|
|
|
return resourceAwsS3BucketWebsiteDelete(s3conn, d)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Cannot specify more than one website.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsS3BucketWebsitePut(s3conn *s3.S3, d *schema.ResourceData, website map[string]interface{}) error {
|
2015-04-26 01:36:00 +02:00
|
|
|
bucket := d.Get("bucket").(string)
|
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
indexDocument := website["index_document"].(string)
|
|
|
|
errorDocument := website["error_document"].(string)
|
2015-04-29 18:16:01 +02:00
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
websiteConfiguration := &s3.WebsiteConfiguration{}
|
2015-04-29 18:16:01 +02:00
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
websiteConfiguration.IndexDocument = &s3.IndexDocument{Suffix: aws.String(indexDocument)}
|
2015-04-29 18:16:01 +02:00
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
if errorDocument != "" {
|
|
|
|
websiteConfiguration.ErrorDocument = &s3.ErrorDocument{Key: aws.String(errorDocument)}
|
|
|
|
}
|
2015-04-26 01:36:00 +02:00
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
putInput := &s3.PutBucketWebsiteInput{
|
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
WebsiteConfiguration: websiteConfiguration,
|
|
|
|
}
|
2015-04-26 01:36:00 +02:00
|
|
|
|
2015-05-07 17:03:28 +02:00
|
|
|
log.Printf("[DEBUG] S3 put bucket website: %#v", putInput)
|
2015-04-26 01:36:00 +02:00
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
_, err := s3conn.PutBucketWebsite(putInput)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error putting S3 website: %s", err)
|
|
|
|
}
|
2015-04-26 01:36:00 +02:00
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsS3BucketWebsiteDelete(s3conn *s3.S3, d *schema.ResourceData) error {
|
|
|
|
bucket := d.Get("bucket").(string)
|
|
|
|
deleteInput := &s3.DeleteBucketWebsiteInput{Bucket: aws.String(bucket)}
|
|
|
|
|
2015-05-07 17:03:28 +02:00
|
|
|
log.Printf("[DEBUG] S3 delete bucket website: %#v", deleteInput)
|
2015-05-01 15:19:54 +02:00
|
|
|
|
|
|
|
_, err := s3conn.DeleteBucketWebsite(deleteInput)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting S3 website: %s", err)
|
2015-04-26 01:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-29 21:47:21 +02:00
|
|
|
|
2015-05-01 15:37:02 +02:00
|
|
|
func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (string, error) {
|
2015-05-01 15:19:54 +02:00
|
|
|
// If the bucket doesn't have a website configuration, return an empty
|
|
|
|
// endpoint
|
2015-05-01 15:38:11 +02:00
|
|
|
if _, ok := d.GetOk("website"); !ok {
|
2015-05-01 15:37:02 +02:00
|
|
|
return "", nil
|
2015-04-29 21:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bucket := d.Get("bucket").(string)
|
|
|
|
|
|
|
|
// Lookup the region for this bucket
|
2015-05-01 15:19:54 +02:00
|
|
|
location, err := s3conn.GetBucketLocation(
|
|
|
|
&s3.GetBucketLocationInput{
|
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
},
|
|
|
|
)
|
2015-04-29 21:47:21 +02:00
|
|
|
if err != nil {
|
2015-05-01 15:37:02 +02:00
|
|
|
return "", err
|
2015-04-29 21:47:21 +02:00
|
|
|
}
|
|
|
|
var region string
|
|
|
|
if location.LocationConstraint != nil {
|
|
|
|
region = *location.LocationConstraint
|
|
|
|
}
|
|
|
|
|
2015-05-01 15:50:49 +02:00
|
|
|
// Default to us-east-1 if the bucket doesn't have a region:
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
|
2015-04-29 21:47:21 +02:00
|
|
|
if region == "" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
2015-05-01 15:37:02 +02:00
|
|
|
endpoint := fmt.Sprintf("%s.s3-website-%s.amazonaws.com", bucket, region)
|
2015-04-29 21:47:21 +02:00
|
|
|
|
2015-05-01 15:37:02 +02:00
|
|
|
return endpoint, nil
|
2015-04-29 21:47:21 +02:00
|
|
|
}
|