Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2544 terraform s3 policy #1

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func Provider() terraform.ResourceProvider {
"aws_route_table_association": resourceAwsRouteTableAssociation(),
"aws_route_table": resourceAwsRouteTable(),
"aws_s3_bucket": resourceAwsS3Bucket(),
"aws_s3_bucket_policy": resourceAwsS3BucketPolicy(),
"aws_security_group": resourceAwsSecurityGroup(),
"aws_security_group_rule": resourceAwsSecurityGroupRule(),
"aws_subnet": resourceAwsSubnet(),
Expand Down
100 changes: 100 additions & 0 deletions builtin/providers/aws/resource_aws_s3_bucket_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package aws

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"

"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/aws/awsutil"
"github.com/awslabs/aws-sdk-go/service/s3"
)

func resourceAwsS3BucketPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsS3BucketPolicyPut,
Update: resourceAwsS3BucketPolicyPut,
Read: resourceAwsS3BucketPolicyRead,
Delete: resourceAwsS3BucketPolicyDelete,

Schema: map[string]*schema.Schema{
"bucket": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"policy": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

func resourceAwsS3BucketPolicyPut(d *schema.ResourceData, meta interface{}) error {

s3conn := meta.(*AWSClient).s3conn
bucket := d.Get("bucket").(string)
policy := d.Get("policy").(string)
name := d.Get("name").(string)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I also do not know Go, but would it be possible to give these variables some more descriptive names? What is 'd', for instance?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's basically their standard:
https://godoc.org/github.com/hashicorp/terraform/helper/schema#pkg-index

d *ResourceData technically if I was going strictly by their practices I probably should have stuck with m for meta.

I agree it's not very descriptive.


resp, err := s3conn.PutBucketPolicy(
&s3.PutBucketPolicyInput{
Bucket: aws.String(bucket),
Policy: aws.String(policy),
})

log.Printf("[DEBUG] S3 bucket policy set (response): %s", awsutil.StringValue(resp))

if err != nil {
return fmt.Errorf("Error adding policy to S3 bucket: %s", err)
}

d.SetId(fmt.Sprintf("%s:%s", bucket, name))
return nil
}

func resourceAwsS3BucketPolicyRead(d *schema.ResourceData, meta interface{}) error {

s3conn := meta.(*AWSClient).s3conn
bucket := d.Get("bucket").(string)

resp, err := s3conn.GetBucketPolicy(
&s3.GetBucketPolicyInput{
Bucket: aws.String(bucket),
},
)

if resp != nil {
if resp.Policy != nil {
d.Set("policy", resp.Policy)
}
}

if err != nil {
return fmt.Errorf("Error getting policy for S3 bucket (%s): %s", bucket, err)
}
return nil
}

func resourceAwsS3BucketPolicyDelete(d *schema.ResourceData, meta interface{}) error {

s3conn := meta.(*AWSClient).s3conn
bucket := d.Get("bucket").(string)

_, err := s3conn.DeleteBucketPolicy(
&s3.DeleteBucketPolicyInput{
Bucket: aws.String(bucket),
},
)

if err != nil {
return fmt.Errorf("Error deleting policy for S3 bucket (%s): %s", bucket, err)
}

return nil
}