Skip to content

Commit

Permalink
CW Metric Stream: Add statistics_configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dmahfigma committed Jun 23, 2022
1 parent e8dfac7 commit 11503e5
Show file tree
Hide file tree
Showing 4 changed files with 436 additions and 38 deletions.
24 changes: 23 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,21 @@ github.com/aws/aws-sdk-go v1.15.78/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3A
github.com/aws/aws-sdk-go v1.25.3/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.31.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.42.18/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go v1.42.52/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc=
github.com/aws/aws-sdk-go v1.44.29 h1:53YWlelsMiYmGxuTRpAq7Xp+pE+0esAVqNFiNyekU+A=
github.com/aws/aws-sdk-go v1.44.29/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go-v2 v1.16.3/go.mod h1:ytwTPBG6fXTZLxxeeCCWj2/EMYp/xDUgX+OET6TLNNU=
github.com/aws/aws-sdk-go-v2/config v1.15.4/go.mod h1:ZijHHh0xd/A+ZY53az0qzC5tT46kt4JVCePf2NX9Lk4=
github.com/aws/aws-sdk-go-v2/credentials v1.12.0/go.mod h1:9YWk7VW+eyKsoIL6/CljkTrNVWBSK9pkqOPUuijid4A=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.4/go.mod h1:u/s5/Z+ohUQOPXl00m2yJVyioWDECsbpXTQlaqSlufc=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.10/go.mod h1:F+EZtuIwjlv35kRJPyBGcsA4f7bnSoz15zOQ2lJq1Z4=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.4/go.mod h1:8glyUqVIM4AmeenIsPo0oVh3+NUwnsQml2OFupfQW+0=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.11/go.mod h1:0MR+sS1b/yxsfAPvAESrw8NfwUoxMinDyw6EYR9BS2U=
github.com/aws/aws-sdk-go-v2/service/iam v1.18.4/go.mod h1:FpNvAfCZyIQ3qeNJUOw4CShKvdizHblXqAvSk0qmyL4=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.4/go.mod h1:uKkN7qmSIsNJVyMtxNQoCEYMvFEXbOg9fwCJPdfp2u8=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.4/go.mod h1:cPDwJwsP4Kff9mldCXAmddjJL6JGQqtA3Mzer2zyr88=
github.com/aws/aws-sdk-go-v2/service/sts v1.16.4/go.mod h1:lfSYenAXtavyX2A1LsViglqlG9eEFYxNryTZS5rn3QE=
github.com/aws/smithy-go v1.11.2/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
Expand Down Expand Up @@ -149,8 +162,10 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0 h1:pMen7vLs8nvgEYhywH3KDWJIJTeEr2ULsVWHWYHQyBs=
Expand All @@ -174,7 +189,13 @@ github.com/hashicorp/aws-sdk-go-base v1.0.0/go.mod h1:2fRjWDv3jJBeN6mVWFHV6hFTNe
github.com/hashicorp/awspolicyequivalence v1.4.0 h1:mpQ7/MnyOsaMcXQcJiYbE3LAONzMH1MnwEK/HMvE6Ss=
github.com/hashicorp/awspolicyequivalence v1.4.0/go.mod h1:9IOaIHx+a7C0NfUNk1A93M7kHd5rJ19aoUx37LZGC14=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.16 h1:Ac/qv9kXBBeyIAGkZdy+Idmw4Avb3mdxPVdT0rI/+aY=
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.16/go.mod h1:xySJRdcDB8hVSmboo3X+evGhZPzBYwPmZbQxLgfGfBw=
github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2 v2.0.0-beta.17 h1:hUG69tIA35oNI2hoeURCnShSViDNCmcQdLSngP89124=
github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2 v2.0.0-beta.17/go.mod h1:MS242y2IOhTXoT0nhbJt/Z1p/6hlisZunHwvrWAcJyY=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
Expand Down Expand Up @@ -428,6 +449,7 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down
217 changes: 198 additions & 19 deletions internal/service/cloudwatch/metric_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)
Expand Down Expand Up @@ -89,15 +90,15 @@ func ResourceMetricStream() *schema.Resource {
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateCloudWatchMetricStreamName,
ValidateFunc: validateMetricStreamName,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: validateCloudWatchMetricStreamName,
ValidateFunc: validateMetricStreamName,
},
"output_format": {
Type: schema.TypeString,
Expand All @@ -113,6 +114,55 @@ func ResourceMetricStream() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"statistics_configuration": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"additional_statistics": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.All(
validation.Any(
validation.StringMatch(
regexp.MustCompile(`(^IQM$)|(^(p|tc|tm|ts|wm)(100|\d{1,2})(\.\d{0,10})?$)|(^[ou]\d+(\.\d*)?$)`),
"invalid statistic, see: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Statistics-definitions.html",
),
validation.StringMatch(
regexp.MustCompile(`^(TM|TC|TS|WM)\(((((\d{1,2})(\.\d{0,10})?|100(\.0{0,10})?)%)?:((\d{1,2})(\.\d{0,10})?|100(\.0{0,10})?)%|((\d{1,2})(\.\d{0,10})?|100(\.0{0,10})?)%:(((\d{1,2})(\.\d{0,10})?|100(\.0{0,10})?)%)?)\)|(TM|TC|TS|WM|PR)\(((\d+(\.\d{0,10})?|(\d+(\.\d{0,10})?[Ee][+-]?\d+)):((\d+(\.\d{0,10})?|(\d+(\.\d{0,10})?[Ee][+-]?\d+)))?|((\d+(\.\d{0,10})?|(\d+(\.\d{0,10})?[Ee][+-]?\d+)))?:(\d+(\.\d{0,10})?|(\d+(\.\d{0,10})?[Ee][+-]?\d+)))\)$`),
"invalid statistic, see: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Statistics-definitions.html",
),
),
validation.StringDoesNotMatch(
regexp.MustCompile(`^p0(\.0{0,10})?|p100(\.\d{0,10})?$`),
"invalid statistic, see: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Statistics-definitions.html",
),
),
},
},
"include_metric": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"metric_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"namespace": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
},
},
},
},
},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},
Expand All @@ -131,24 +181,56 @@ func resourceMetricStreamCreate(ctx context.Context, d *schema.ResourceData, met
FirehoseArn: aws.String(d.Get("firehose_arn").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
OutputFormat: aws.String(d.Get("output_format").(string)),
Tags: Tags(tags.IgnoreAWS()),
}

if len(tags) > 0 {
params.Tags = Tags(tags.IgnoreAWS())
}

if v, ok := d.GetOk("include_filter"); ok && v.(*schema.Set).Len() > 0 {
params.IncludeFilters = expandCloudWatchMetricStreamFilters(v.(*schema.Set))
params.IncludeFilters = expandMetricStreamFilters(v.(*schema.Set))
}

if v, ok := d.GetOk("exclude_filter"); ok && v.(*schema.Set).Len() > 0 {
params.ExcludeFilters = expandCloudWatchMetricStreamFilters(v.(*schema.Set))
params.ExcludeFilters = expandMetricStreamFilters(v.(*schema.Set))
}

if v, ok := d.GetOk("statistics_configuration"); ok && v.(*schema.Set).Len() > 0 {
params.StatisticsConfigurations = expandMetricStreamStatisticsConfigurations(v.(*schema.Set))
}

log.Printf("[DEBUG] Putting CloudWatch Metric Stream: %#v", params)
output, err := conn.PutMetricStreamWithContext(ctx, &params)

// Some partitions (i.e., ISO) may not support tag-on-create
if params.Tags != nil && verify.CheckISOErrorTagsUnsupported(err) {
log.Printf("[WARN] failed creating CloudWatch Metric Stream (%s) with tags: %s. Trying create without tags.", name, err)
params.Tags = nil

output, err = conn.PutMetricStreamWithContext(ctx, &params)
}

log.Printf("[DEBUG] Putting CloudWatch MetricStream: %#v", params)
_, err := conn.PutMetricStreamWithContext(ctx, &params)
if err != nil {
return diag.FromErr(fmt.Errorf("putting metric_stream failed: %s", err))
return diag.Errorf("failed creating CloudWatch Metric Stream (%s): %s", name, err)
}

d.SetId(name)
log.Println("[INFO] CloudWatch MetricStream put finished")
log.Println("[INFO] CloudWatch Metric Stream put finished")

// Some partitions (i.e., ISO) may not support tag-on-create, attempt tag after create
if params.Tags == nil && len(tags) > 0 {
err := UpdateTags(conn, aws.StringValue(output.Arn), nil, tags)

// If default tags only, log and continue. Otherwise, error.
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.CheckISOErrorTagsUnsupported(err) {
log.Printf("[WARN] failed adding tags after create for CloudWatch Metric Stream (%s): %s", d.Id(), err)
return resourceMetricStreamRead(ctx, d, meta)
}

if err != nil {
return diag.Errorf("failed adding tags after create for CloudWatch Metric Stream (%s): %s", d.Id(), err)
}
}

return resourceMetricStreamRead(ctx, d, meta)
}
Expand Down Expand Up @@ -185,21 +267,33 @@ func resourceMetricStreamRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("state", output.State)

if output.IncludeFilters != nil {
if err := d.Set("include_filter", flattenCloudWatchMetricStreamFilters(output.IncludeFilters)); err != nil {
if err := d.Set("include_filter", flattenMetricStreamFilters(output.IncludeFilters)); err != nil {
return diag.FromErr(fmt.Errorf("error setting include_filter error: %w", err))
}
}

if output.ExcludeFilters != nil {
if err := d.Set("exclude_filter", flattenCloudWatchMetricStreamFilters(output.ExcludeFilters)); err != nil {
if err := d.Set("exclude_filter", flattenMetricStreamFilters(output.ExcludeFilters)); err != nil {
return diag.FromErr(fmt.Errorf("error setting exclude_filter error: %w", err))
}
}

if output.StatisticsConfigurations != nil {
if err := d.Set("statistics_configuration", flattenMetricStreamStatisticsConfigurations(output.StatisticsConfigurations)); err != nil {
return diag.FromErr(fmt.Errorf("error setting statistics_configuration error: %w", err))
}
}

tags, err := ListTags(conn, aws.StringValue(output.Arn))

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(err) {
log.Printf("[WARN] failed listing tags for CloudWatch Metric Stream (%s): %s", d.Id(), err)
return nil
}

if err != nil {
return diag.FromErr(fmt.Errorf("error listing tags for CloudWatch Metric Stream (%s): %w", d.Id(), err))
return diag.Errorf("failed listing tags for CloudWatch Metric Stream (%s): %s", d.Id(), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)
Expand All @@ -217,33 +311,33 @@ func resourceMetricStreamRead(ctx context.Context, d *schema.ResourceData, meta
}

func resourceMetricStreamDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] Deleting CloudWatch MetricStream %s", d.Id())
log.Printf("[INFO] Deleting CloudWatch Metric Stream %s", d.Id())
conn := meta.(*conns.AWSClient).CloudWatchConn
params := cloudwatch.DeleteMetricStreamInput{
Name: aws.String(d.Id()),
}

if _, err := conn.DeleteMetricStreamWithContext(ctx, &params); err != nil {
return diag.FromErr(fmt.Errorf("error deleting CloudWatch MetricStream: %s", err))
return diag.FromErr(fmt.Errorf("error deleting CloudWatch Metric Stream: %s", err))
}

if _, err := WaitMetricStreamDeleted(ctx, conn, d.Id()); err != nil {
return diag.FromErr(fmt.Errorf("error while waiting for CloudWatch Metric Stream (%s) to become deleted: %w", d.Id(), err))
}

log.Printf("[INFO] CloudWatch MetricStream %s deleted", d.Id())
log.Printf("[INFO] CloudWatch Metric Stream %s deleted", d.Id())

return nil
}

func validateCloudWatchMetricStreamName(v interface{}, k string) (ws []string, errors []error) {
func validateMetricStreamName(v interface{}, k string) (ws []string, errors []error) {
return validation.All(
validation.StringLenBetween(1, 255),
validation.StringMatch(regexp.MustCompile(`^[\-_A-Za-z0-9]*$`), "must match [\\-_A-Za-z0-9]"),
)(v, k)
}

func expandCloudWatchMetricStreamFilters(s *schema.Set) []*cloudwatch.MetricStreamFilter {
func expandMetricStreamFilters(s *schema.Set) []*cloudwatch.MetricStreamFilter {
var filters []*cloudwatch.MetricStreamFilter

for _, filterRaw := range s.List() {
Expand All @@ -260,7 +354,7 @@ func expandCloudWatchMetricStreamFilters(s *schema.Set) []*cloudwatch.MetricStre
return filters
}

func flattenCloudWatchMetricStreamFilters(s []*cloudwatch.MetricStreamFilter) []map[string]interface{} {
func flattenMetricStreamFilters(s []*cloudwatch.MetricStreamFilter) []map[string]interface{} {
filters := make([]map[string]interface{}, 0)

for _, bd := range s {
Expand All @@ -278,3 +372,88 @@ func flattenCloudWatchMetricStreamFilters(s []*cloudwatch.MetricStreamFilter) []

return nil
}

func expandMetricStreamStatisticsConfigurations(s *schema.Set) []*cloudwatch.MetricStreamStatisticsConfiguration {
var configurations []*cloudwatch.MetricStreamStatisticsConfiguration

for _, configurationRaw := range s.List() {
configuration := &cloudwatch.MetricStreamStatisticsConfiguration{}
mConfiguration := configurationRaw.(map[string]interface{})

if v, ok := mConfiguration["additional_statistics"].(*schema.Set); ok && v.Len() > 0 {
log.Printf("[DEBUG] CloudWatch Metric Stream StatisticsConfigurations additional_statistics: %#v", v)
configuration.AdditionalStatistics = flex.ExpandStringSet(v)
}

if v, ok := mConfiguration["include_metric"].(*schema.Set); ok && v.Len() > 0 {
log.Printf("[DEBUG] CloudWatch Metric Stream StatisticsConfigurations include_metrics: %#v", v)
configuration.IncludeMetrics = expandMetricStreamStatisticsConfigurationsIncludeMetrics(v)
}

configurations = append(configurations, configuration)

}

log.Printf("[DEBUG] statistics_configurations: %#v", configurations)

if len(configurations) > 0 {
return configurations
}

return nil
}

func expandMetricStreamStatisticsConfigurationsIncludeMetrics(metrics *schema.Set) []*cloudwatch.MetricStreamStatisticsMetric {
var includeMetrics []*cloudwatch.MetricStreamStatisticsMetric

for _, metricRaw := range metrics.List() {
metric := &cloudwatch.MetricStreamStatisticsMetric{}
mMetric := metricRaw.(map[string]interface{})

if v, ok := mMetric["metric_name"].(string); ok && v != "" {
metric.MetricName = aws.String(v)
}

if v, ok := mMetric["namespace"].(string); ok && v != "" {
metric.Namespace = aws.String(v)
}

includeMetrics = append(includeMetrics, metric)
}

if len(includeMetrics) > 0 {
return includeMetrics
}

return nil
}

func flattenMetricStreamStatisticsConfigurations(configurations []*cloudwatch.MetricStreamStatisticsConfiguration) []map[string]interface{} {
flatConfigurations := make([]map[string]interface{}, len(configurations))

for i, configuration := range configurations {
flatConfiguration := map[string]interface{}{
"additional_statistics": flex.FlattenStringSet(configuration.AdditionalStatistics),
"include_metric": flattenMetricStreamStatisticsConfigurationsIncludeMetrics(configuration.IncludeMetrics),
}

flatConfigurations[i] = flatConfiguration
}

return flatConfigurations
}

func flattenMetricStreamStatisticsConfigurationsIncludeMetrics(metrics []*cloudwatch.MetricStreamStatisticsMetric) []map[string]interface{} {
flatMetrics := make([]map[string]interface{}, len(metrics))

for i, metric := range metrics {
flatMetric := map[string]interface{}{
"metric_name": aws.StringValue(metric.MetricName),
"namespace": aws.StringValue(metric.Namespace),
}

flatMetrics[i] = flatMetric
}

return flatMetrics
}
Loading

0 comments on commit 11503e5

Please sign in to comment.