forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_cloudwatch_log_stream.go
133 lines (108 loc) · 3.38 KB
/
resource_aws_cloudwatch_log_stream.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
package aws
import (
"fmt"
"log"
"regexp"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsCloudWatchLogStream() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloudWatchLogStreamCreate,
Read: resourceAwsCloudWatchLogStreamRead,
Delete: resourceAwsCloudWatchLogStreamDelete,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCloudWatchLogStreamName,
},
"log_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsCloudWatchLogStreamCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
log.Printf("[DEBUG] Creating CloudWatch Log Stream: %s", d.Get("name").(string))
_, err := conn.CreateLogStream(&cloudwatchlogs.CreateLogStreamInput{
LogGroupName: aws.String(d.Get("log_group_name").(string)),
LogStreamName: aws.String(d.Get("name").(string)),
})
if err != nil {
return errwrap.Wrapf("Creating CloudWatch Log Stream failed: %s", err)
}
d.SetId(d.Get("name").(string))
return resourceAwsCloudWatchLogStreamRead(d, meta)
}
func resourceAwsCloudWatchLogStreamRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
ls, exists, err := lookupCloudWatchLogStream(conn, d.Id(), d.Get("log_group_name").(string), nil)
if err != nil {
return err
}
if !exists {
log.Printf("[DEBUG] CloudWatch Stream %q Not Found. Removing from state", d.Id())
d.SetId("")
return nil
}
d.Set("arn", ls.Arn)
d.Set("name", ls.LogStreamName)
return nil
}
func resourceAwsCloudWatchLogStreamDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
log.Printf("[INFO] Deleting CloudWatch Log Stream: %s", d.Id())
params := &cloudwatchlogs.DeleteLogStreamInput{
LogGroupName: aws.String(d.Get("log_group_name").(string)),
LogStreamName: aws.String(d.Id()),
}
_, err := conn.DeleteLogStream(params)
if err != nil {
return errwrap.Wrapf("Error deleting CloudWatch Log Stream: %s", err)
}
return nil
}
func lookupCloudWatchLogStream(conn *cloudwatchlogs.CloudWatchLogs,
name string, logStreamName string, nextToken *string) (*cloudwatchlogs.LogStream, bool, error) {
input := &cloudwatchlogs.DescribeLogStreamsInput{
LogStreamNamePrefix: aws.String(name),
LogGroupName: aws.String(logStreamName),
NextToken: nextToken,
}
resp, err := conn.DescribeLogStreams(input)
if err != nil {
return nil, true, err
}
for _, ls := range resp.LogStreams {
if *ls.LogStreamName == name {
return ls, true, nil
}
}
if resp.NextToken != nil {
return lookupCloudWatchLogStream(conn, name, logStreamName, resp.NextToken)
}
return nil, false, nil
}
func validateCloudWatchLogStreamName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if regexp.MustCompile(`:`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"colons not allowed in %q:", k))
}
if len(value) < 1 || len(value) > 512 {
errors = append(errors, fmt.Errorf(
"%q must be between 1 and 512 characters: %q", k, value))
}
return
}