-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
resource_aws_cloudwatch_log_subscription_filter.go
193 lines (163 loc) · 6.01 KB
/
resource_aws_cloudwatch_log_subscription_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
package aws
import (
"bytes"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsCloudwatchLogSubscriptionFilter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloudwatchLogSubscriptionFilterCreate,
Read: resourceAwsCloudwatchLogSubscriptionFilterRead,
Update: resourceAwsCloudwatchLogSubscriptionFilterUpdate,
Delete: resourceAwsCloudwatchLogSubscriptionFilterDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"destination_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"filter_pattern": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"log_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"role_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"distribution": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAwsCloudwatchLogSubscriptionFilterCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
params := getAwsCloudWatchLogsSubscriptionFilterInput(d)
log.Printf("[DEBUG] Creating SubscriptionFilter %#v", params)
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.PutSubscriptionFilter(¶ms)
if err == nil {
d.SetId(cloudwatchLogsSubscriptionFilterId(d.Get("log_group_name").(string)))
log.Printf("[DEBUG] Cloudwatch logs subscription %q created", d.Id())
}
awsErr, ok := err.(awserr.Error)
if !ok {
return resource.RetryableError(err)
}
if awsErr.Code() == "InvalidParameterException" {
log.Printf("[DEBUG] Caught message: %q, code: %q: Retrying", awsErr.Message(), awsErr.Code())
if strings.Contains(awsErr.Message(), "Could not deliver test message to specified") {
return resource.RetryableError(err)
}
if strings.Contains(awsErr.Message(), "Could not execute the lambda function") {
return resource.RetryableError(err)
}
resource.NonRetryableError(err)
}
return resource.NonRetryableError(err)
})
}
func resourceAwsCloudwatchLogSubscriptionFilterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
params := getAwsCloudWatchLogsSubscriptionFilterInput(d)
log.Printf("[DEBUG] Update SubscriptionFilter %#v", params)
_, err := conn.PutSubscriptionFilter(¶ms)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
return fmt.Errorf("[WARN] Error updating SubscriptionFilter (%s) for LogGroup (%s), message: \"%s\", code: \"%s\"",
d.Get("name").(string), d.Get("log_group_name").(string), awsErr.Message(), awsErr.Code())
}
return err
}
d.SetId(cloudwatchLogsSubscriptionFilterId(d.Get("log_group_name").(string)))
return resourceAwsCloudwatchLogSubscriptionFilterRead(d, meta)
}
func getAwsCloudWatchLogsSubscriptionFilterInput(d *schema.ResourceData) cloudwatchlogs.PutSubscriptionFilterInput {
name := d.Get("name").(string)
destination_arn := d.Get("destination_arn").(string)
filter_pattern := d.Get("filter_pattern").(string)
log_group_name := d.Get("log_group_name").(string)
params := cloudwatchlogs.PutSubscriptionFilterInput{
FilterName: aws.String(name),
DestinationArn: aws.String(destination_arn),
FilterPattern: aws.String(filter_pattern),
LogGroupName: aws.String(log_group_name),
}
if _, ok := d.GetOk("role_arn"); ok {
params.RoleArn = aws.String(d.Get("role_arn").(string))
}
if _, ok := d.GetOk("distribution"); ok {
params.Distribution = aws.String(d.Get("distribution").(string))
}
return params
}
func resourceAwsCloudwatchLogSubscriptionFilterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
log_group_name := d.Get("log_group_name").(string)
name := d.Get("name").(string) // "name" is a required field in the schema
req := &cloudwatchlogs.DescribeSubscriptionFiltersInput{
LogGroupName: aws.String(log_group_name),
FilterNamePrefix: aws.String(name),
}
resp, err := conn.DescribeSubscriptionFilters(req)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFoundException" {
log.Printf("[WARN] SubscriptionFilters (%q) Not Found", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading SubscriptionFilters for log group %s with name prefix %s: %#v", log_group_name, d.Get("name").(string), err)
}
for _, subscriptionFilter := range resp.SubscriptionFilters {
if *subscriptionFilter.LogGroupName == log_group_name {
d.SetId(cloudwatchLogsSubscriptionFilterId(log_group_name))
return nil // OK, matching subscription filter found
}
}
log.Printf("[DEBUG] Subscription Filter%q Not Found", name)
d.SetId("")
return nil
}
func resourceAwsCloudwatchLogSubscriptionFilterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
log.Printf("[INFO] Deleting CloudWatch Log Group Subscription: %s", d.Id())
log_group_name := d.Get("log_group_name").(string)
name := d.Get("name").(string)
params := &cloudwatchlogs.DeleteSubscriptionFilterInput{
FilterName: aws.String(name), // Required
LogGroupName: aws.String(log_group_name), // Required
}
_, err := conn.DeleteSubscriptionFilter(params)
if err != nil {
return fmt.Errorf(
"Error deleting Subscription Filter from log group: %s with name filter name %s: %+v", log_group_name, name, err)
}
d.SetId("")
return nil
}
func cloudwatchLogsSubscriptionFilterId(log_group_name string) string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("%s-", log_group_name)) // only one filter allowed per log_group_name at the moment
return fmt.Sprintf("cwlsf-%d", hashcode.String(buf.String()))
}