forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3ab901d
added policy string to schema
m-s-austin 0975a70
working policy assignment
m-s-austin 455b409
dummy resource provider
m-s-austin e14a1ad
hook new resource provider into configuration
m-s-austin 233a5f4
merged origin/master
m-s-austin b926b11
moved policy to it's own provider
m-s-austin b41c737
Revert "working policy assignment"
m-s-austin 31ffdea
add name for use with resouce id
m-s-austin 0d3d51a
Merge remote-tracking branch 'hashi_origin/master' into 2544-terrafor…
m-s-austin cd45e45
added code to read bucket policy from bucket, however, it's not worki…
m-s-austin ac50cae
added code to delete policy from s3 bucket
m-s-austin 0de912d
cleaned up code and run go fmt
m-s-austin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.