-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
New Resource: aws_s3_bucket_metric #916
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,217 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
) | ||
|
||
func resourceAwsS3BucketMetric() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsS3BucketMetricPut, | ||
Read: resourceAwsS3BucketMetricRead, | ||
Update: resourceAwsS3BucketMetricPut, | ||
Delete: resourceAwsS3BucketMetricDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"bucket": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"filter": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"prefix": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"tags": tagsSchema(), | ||
}, | ||
}, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsS3BucketMetricPut(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).s3conn | ||
bucket := d.Get("bucket").(string) | ||
name := d.Get("name").(string) | ||
|
||
metricsConfiguration := &s3.MetricsConfiguration{ | ||
Id: aws.String(name), | ||
} | ||
|
||
if v, ok := d.GetOk("filter"); ok { | ||
metricsConfiguration.Filter = expandS3MetricsFilter(v.([]interface{})[0].(map[string]interface{})) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it might be worth splitting these casts out to be clearer if there's a crash? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! I'll split it out like this: if v, ok := d.GetOk("filter"); ok {
filterList := v.([]interface{})
filterMap := filterList[0].(map[string]interface{})
metricsConfiguration.Filter = expandS3MetricsFilter(filterMap)
} |
||
} | ||
|
||
input := &s3.PutBucketMetricsConfigurationInput{ | ||
Bucket: aws.String(bucket), | ||
Id: aws.String(name), | ||
MetricsConfiguration: metricsConfiguration, | ||
} | ||
|
||
log.Printf("[DEBUG] Putting metric configuration: %s", input) | ||
err := resource.Retry(1*time.Minute, func() *resource.RetryError { | ||
_, err := conn.PutBucketMetricsConfiguration(input) | ||
if err != nil { | ||
if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") { | ||
return resource.RetryableError(err) | ||
} | ||
return resource.NonRetryableError(err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error putting S3 metric configuration: %s", err) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s:%s", bucket, name)) | ||
|
||
return resourceAwsS3BucketMetricRead(d, meta) | ||
} | ||
|
||
func resourceAwsS3BucketMetricDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).s3conn | ||
|
||
bucket, name, err := resourceAwsS3BucketMetricParseID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
input := &s3.DeleteBucketMetricsConfigurationInput{ | ||
Bucket: aws.String(bucket), | ||
Id: aws.String(name), | ||
} | ||
|
||
log.Printf("[DEBUG] Deleting S3 bucket metric configuration: %s", input) | ||
_, err = conn.DeleteBucketMetricsConfiguration(input) | ||
if err != nil { | ||
if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NoSuchConfiguration", "The specified configuration does not exist.") { | ||
log.Printf("[WARN] %s S3 bucket metrics configuration not found, removing from state.", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error deleting S3 metric configuration: %s", err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func resourceAwsS3BucketMetricRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).s3conn | ||
|
||
bucket, name, err := resourceAwsS3BucketMetricParseID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set("bucket", bucket) | ||
d.Set("name", name) | ||
|
||
input := &s3.GetBucketMetricsConfigurationInput{ | ||
Bucket: aws.String(bucket), | ||
Id: aws.String(name), | ||
} | ||
|
||
log.Printf("[DEBUG] Reading S3 bucket metrics configuration: %s", input) | ||
output, err := conn.GetBucketMetricsConfiguration(input) | ||
if err != nil { | ||
if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NoSuchConfiguration", "The specified configuration does not exist.") { | ||
log.Printf("[WARN] %s S3 bucket metrics configuration not found, removing from state.", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
if output.MetricsConfiguration.Filter != nil { | ||
if err := d.Set("filter", []interface{}{flattenS3MetricsFilter(output.MetricsConfiguration.Filter)}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func expandS3MetricsFilter(m map[string]interface{}) *s3.MetricsFilter { | ||
var prefix string | ||
if v, ok := m["prefix"]; ok { | ||
prefix = v.(string) | ||
} | ||
|
||
var tags []*s3.Tag | ||
if v, ok := m["tags"]; ok { | ||
tags = tagsFromMapS3(v.(map[string]interface{})) | ||
} | ||
|
||
metricsFilter := &s3.MetricsFilter{} | ||
if prefix != "" && len(tags) > 0 { | ||
metricsFilter.And = &s3.MetricsAndOperator{ | ||
Prefix: aws.String(prefix), | ||
Tags: tags, | ||
} | ||
} else if len(tags) > 1 { | ||
metricsFilter.And = &s3.MetricsAndOperator{ | ||
Tags: tags, | ||
} | ||
} else if len(tags) == 1 { | ||
metricsFilter.Tag = tags[0] | ||
} else { | ||
metricsFilter.Prefix = aws.String(prefix) | ||
} | ||
return metricsFilter | ||
} | ||
|
||
func flattenS3MetricsFilter(metricsFilter *s3.MetricsFilter) map[string]interface{} { | ||
m := make(map[string]interface{}) | ||
|
||
if metricsFilter.And != nil { | ||
and := *metricsFilter.And | ||
if and.Prefix != nil { | ||
m["prefix"] = *and.Prefix | ||
} | ||
if and.Tags != nil { | ||
m["tags"] = tagsToMapS3(and.Tags) | ||
} | ||
} else if metricsFilter.Prefix != nil { | ||
m["prefix"] = *metricsFilter.Prefix | ||
} else if metricsFilter.Tag != nil { | ||
tags := []*s3.Tag{ | ||
metricsFilter.Tag, | ||
} | ||
m["tags"] = tagsToMapS3(tags) | ||
} | ||
return m | ||
} | ||
|
||
func resourceAwsS3BucketMetricParseID(id string) (string, string, error) { | ||
idParts := strings.Split(id, ":") | ||
if len(idParts) != 2 { | ||
return "", "", fmt.Errorf("please make sure the ID is in the form BUCKET:NAME (i.e. my-bucket:EntireBucket") | ||
} | ||
bucket := idParts[0] | ||
name := idParts[1] | ||
return bucket, name, nil | ||
} |
Oops, something went wrong.
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.
drop this space
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.
👍 absolutely, thanks!