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"
|
2014-07-21 20:34:47 +02:00
|
|
|
"github.com/mitchellh/goamz/s3"
|
|
|
|
)
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsS3Bucket() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsS3BucketCreate,
|
|
|
|
Read: resourceAwsS3BucketRead,
|
|
|
|
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,
|
|
|
|
},
|
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
|
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)
|
|
|
|
s3Bucket := s3conn.Bucket(bucket)
|
|
|
|
err := s3Bucket.PutBucket(s3.ACL(acl))
|
|
|
|
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
|
|
|
|
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 resourceAwsS3BucketRead(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
|
|
|
bucket := s3conn.Bucket(d.Id())
|
2014-07-21 20:34:47 +02:00
|
|
|
resp, err := bucket.Head("/")
|
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return err
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
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())
|
|
|
|
bucket := s3conn.Bucket(d.Id())
|
|
|
|
|
|
|
|
return bucket.DelBucket()
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|