forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_cloudwatch_log_metric_filter.go
195 lines (164 loc) · 5.58 KB
/
resource_aws_cloudwatch_log_metric_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package aws
import (
"fmt"
"log"
"strings"
"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/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
func resourceAwsCloudWatchLogMetricFilter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloudWatchLogMetricFilterUpdate,
Read: resourceAwsCloudWatchLogMetricFilterRead,
Update: resourceAwsCloudWatchLogMetricFilterUpdate,
Delete: resourceAwsCloudWatchLogMetricFilterDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateLogMetricFilterName,
},
"pattern": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateMaxLength(1024),
StateFunc: func(v interface{}) string {
s, ok := v.(string)
if !ok {
return ""
}
return strings.TrimSpace(s)
},
},
"log_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateLogGroupName,
},
"metric_transformation": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateLogMetricFilterTransformationName,
},
"namespace": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateLogMetricFilterTransformationName,
},
"value": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateMaxLength(100),
},
"default_value": {
Type: schema.TypeFloat,
Optional: true,
},
},
},
},
},
}
}
func resourceAwsCloudWatchLogMetricFilterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
input := cloudwatchlogs.PutMetricFilterInput{
FilterName: aws.String(d.Get("name").(string)),
FilterPattern: aws.String(strings.TrimSpace(d.Get("pattern").(string))),
LogGroupName: aws.String(d.Get("log_group_name").(string)),
}
transformations := d.Get("metric_transformation").([]interface{})
o := transformations[0].(map[string]interface{})
metricsTransformations, err := expandCloudWachLogMetricTransformations(o)
if err != nil {
return err
}
input.MetricTransformations = metricsTransformations
log.Printf("[DEBUG] Creating/Updating CloudWatch Log Metric Filter: %s", input)
_, err = conn.PutMetricFilter(&input)
if err != nil {
return fmt.Errorf("Creating/Updating CloudWatch Log Metric Filter failed: %s", err)
}
d.SetId(d.Get("name").(string))
log.Println("[INFO] CloudWatch Log Metric Filter created/updated")
return resourceAwsCloudWatchLogMetricFilterRead(d, meta)
}
func resourceAwsCloudWatchLogMetricFilterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
mf, err := lookupCloudWatchLogMetricFilter(conn, d.Get("name").(string),
d.Get("log_group_name").(string), nil)
if err != nil {
if _, ok := err.(*resource.NotFoundError); ok {
log.Printf("[WARN] Removing CloudWatch Log Metric Filter as it is gone")
d.SetId("")
return nil
}
return fmt.Errorf("Failed reading CloudWatch Log Metric Filter: %s", err)
}
log.Printf("[DEBUG] Found CloudWatch Log Metric Filter: %s", mf)
d.Set("name", mf.FilterName)
d.Set("pattern", mf.FilterPattern)
d.Set("metric_transformation", flattenCloudWachLogMetricTransformations(mf.MetricTransformations))
return nil
}
func lookupCloudWatchLogMetricFilter(conn *cloudwatchlogs.CloudWatchLogs,
name, logGroupName string, nextToken *string) (*cloudwatchlogs.MetricFilter, error) {
input := cloudwatchlogs.DescribeMetricFiltersInput{
FilterNamePrefix: aws.String(name),
LogGroupName: aws.String(logGroupName),
NextToken: nextToken,
}
log.Printf("[DEBUG] Reading CloudWatch Log Metric Filter: %s", input)
resp, err := conn.DescribeMetricFilters(&input)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFoundException" {
return nil, &resource.NotFoundError{
Message: fmt.Sprintf("CloudWatch Log Metric Filter %q / %q not found via"+
" initial DescribeMetricFilters call", name, logGroupName),
LastError: err,
LastRequest: input,
}
}
return nil, fmt.Errorf("Failed describing CloudWatch Log Metric Filter: %s", err)
}
for _, mf := range resp.MetricFilters {
if *mf.FilterName == name {
return mf, nil
}
}
if resp.NextToken != nil {
return lookupCloudWatchLogMetricFilter(conn, name, logGroupName, resp.NextToken)
}
return nil, &resource.NotFoundError{
Message: fmt.Sprintf("CloudWatch Log Metric Filter %q / %q not found "+
"in given results from DescribeMetricFilters", name, logGroupName),
LastResponse: resp,
LastRequest: input,
}
}
func resourceAwsCloudWatchLogMetricFilterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
input := cloudwatchlogs.DeleteMetricFilterInput{
FilterName: aws.String(d.Get("name").(string)),
LogGroupName: aws.String(d.Get("log_group_name").(string)),
}
log.Printf("[INFO] Deleting CloudWatch Log Metric Filter: %s", d.Id())
_, err := conn.DeleteMetricFilter(&input)
if err != nil {
return fmt.Errorf("Error deleting CloudWatch Log Metric Filter: %s", err)
}
log.Println("[INFO] CloudWatch Log Metric Filter deleted")
d.SetId("")
return nil
}