github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_s3_bucket.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/mitchellh/goamz/s3"
     9  )
    10  
    11  func resourceAwsS3Bucket() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceAwsS3BucketCreate,
    14  		Read:   resourceAwsS3BucketRead,
    15  		Delete: resourceAwsS3BucketDelete,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"bucket": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  			},
    23  
    24  			"acl": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Default:  "private",
    27  				Optional: true,
    28  				ForceNew: true,
    29  			},
    30  		},
    31  	}
    32  }
    33  
    34  func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
    35  	s3conn := meta.(*AWSClient).s3conn
    36  
    37  	// Get the bucket and acl
    38  	bucket := d.Get("bucket").(string)
    39  	acl := d.Get("acl").(string)
    40  
    41  	log.Printf("[DEBUG] S3 bucket create: %s, ACL: %s", bucket, acl)
    42  	s3Bucket := s3conn.Bucket(bucket)
    43  	err := s3Bucket.PutBucket(s3.ACL(acl))
    44  	if err != nil {
    45  		return fmt.Errorf("Error creating S3 bucket: %s", err)
    46  	}
    47  
    48  	// Assign the bucket name as the resource ID
    49  	d.SetId(bucket)
    50  
    51  	return nil
    52  }
    53  
    54  func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
    55  	s3conn := meta.(*AWSClient).s3conn
    56  
    57  	bucket := s3conn.Bucket(d.Id())
    58  	resp, err := bucket.Head("/")
    59  	if err != nil {
    60  		return err
    61  	}
    62  	defer resp.Body.Close()
    63  	return nil
    64  }
    65  
    66  func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error {
    67  	s3conn := meta.(*AWSClient).s3conn
    68  
    69  	log.Printf("[DEBUG] S3 Delete Bucket: %s", d.Id())
    70  	bucket := s3conn.Bucket(d.Id())
    71  
    72  	return bucket.DelBucket()
    73  }