Skip to content

Commit

Permalink
providers/aws: Add support for policy on S3 bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
justincampbell committed May 16, 2015
1 parent 9f52192 commit c70c632
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
47 changes: 42 additions & 5 deletions builtin/providers/aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func resourceAwsS3Bucket() *schema.Resource {
ForceNew: true,
},

"policy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},

"website": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -121,8 +126,14 @@ func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}

if err := resourceAwsS3BucketWebsiteUpdate(s3conn, d); err != nil {
return err
if d.HasChange("website") {
if err := resourceAwsS3BucketPolicyUpdate(s3conn, d); err != nil {
return err
}

if err := resourceAwsS3BucketWebsiteUpdate(s3conn, d); err != nil {
return err
}
}

return resourceAwsS3BucketRead(d, meta)
Expand Down Expand Up @@ -228,11 +239,37 @@ func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceAwsS3BucketWebsiteUpdate(s3conn *s3.S3, d *schema.ResourceData) error {
if !d.HasChange("website") {
return nil
func resourceAwsS3BucketPolicyUpdate(s3conn *s3.S3, d *schema.ResourceData) error {
bucket := d.Get("bucket").(string)
policy := d.Get("policy").(string)

if policy != "" {
input := &s3.PutBucketPolicyInput{
Bucket: aws.String(bucket),
Policy: aws.String(policy),
}

log.Printf("[DEBUG] S3 put bucket policy: %s", input)

_, err := s3conn.PutBucketPolicy(input)
if err != nil {
return fmt.Errorf("Error putting S3 policy: %s", err)
}
} else {
input := &s3.DeleteBucketPolicyInput{
Bucket: aws.String(bucket),
}

_, err := s3conn.DeleteBucketPolicy(input)
if err != nil {
return fmt.Errorf("Error deleting S3 policy: %s", err)
}
}

return nil
}

func resourceAwsS3BucketWebsiteUpdate(s3conn *s3.S3, d *schema.ResourceData) error {
ws := d.Get("website").([]interface{})

if len(ws) == 1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The following arguments are supported:

* `bucket` - (Required) The name of the bucket.
* `acl` - (Optional) The [canned ACL](http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply. Defaults to "private".
* `policy` - (Optional) A valid [bucket policy](http://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html) JSON document.
* `tags` - (Optional) A mapping of tags to assign to the bucket.
* `website` - (Optional) A website object (documented below).

Expand Down

0 comments on commit c70c632

Please sign in to comment.