terraform/builtin/providers/aws/resource_aws_s3_bucket.go

74 lines
1.5 KiB
Go

package aws
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/s3"
)
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,
},
},
}
}
func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
// Get the bucket and acl
bucket := d.Get("bucket").(string)
acl := d.Get("acl").(string)
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 fmt.Errorf("Error creating S3 bucket: %s", err)
}
// Assign the bucket name as the resource ID
d.SetId(bucket)
return nil
}
func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
bucket := s3conn.Bucket(d.Id())
resp, err := bucket.Head("/")
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
log.Printf("[DEBUG] S3 Delete Bucket: %s", d.Id())
bucket := s3conn.Bucket(d.Id())
return bucket.DelBucket()
}