2014-07-21 20:34:47 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/config"
|
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/s3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resource_aws_s3_bucket_validation() *config.Validator {
|
|
|
|
return &config.Validator{
|
|
|
|
Required: []string{
|
|
|
|
"bucket",
|
|
|
|
},
|
|
|
|
Optional: []string{
|
|
|
|
"acl",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_s3_bucket_create(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff,
|
2014-09-17 02:44:42 +02:00
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-21 20:34:47 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
s3conn := p.s3conn
|
|
|
|
|
|
|
|
// Merge the diff into the state so that we have all the attributes
|
|
|
|
// properly.
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
// Get the bucket and optional acl
|
|
|
|
bucket := rs.Attributes["bucket"]
|
|
|
|
acl := "private"
|
|
|
|
if other, ok := rs.Attributes["acl"]; ok {
|
|
|
|
acl = other
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] S3 bucket create: %s, ACL: %s", bucket, acl)
|
|
|
|
s3Bucket := s3conn.Bucket(bucket)
|
|
|
|
err := s3Bucket.PutBucket(s3.ACL(acl))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating S3 bucket: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the bucket name as the resource ID
|
|
|
|
rs.ID = bucket
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_s3_bucket_destroy(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-21 20:34:47 +02:00
|
|
|
meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
s3conn := p.s3conn
|
|
|
|
|
|
|
|
name := s.Attributes["bucket"]
|
|
|
|
bucket := s3conn.Bucket(name)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] S3 Delete Bucket: %s", name)
|
|
|
|
return bucket.DelBucket()
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_s3_bucket_refresh(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-21 20:34:47 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
s3conn := p.s3conn
|
|
|
|
|
|
|
|
bucket := s3conn.Bucket(s.Attributes["bucket"])
|
|
|
|
resp, err := bucket.Head("/")
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_s3_bucket_diff(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-21 20:34:47 +02:00
|
|
|
c *terraform.ResourceConfig,
|
2014-09-18 01:33:24 +02:00
|
|
|
meta interface{}) (*terraform.InstanceDiff, error) {
|
2014-07-21 20:34:47 +02:00
|
|
|
|
|
|
|
b := &diff.ResourceBuilder{
|
|
|
|
Attrs: map[string]diff.AttrType{
|
|
|
|
"bucket": diff.AttrTypeCreate,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|