forked from hashicorp/terraform-provider-aws
/
resource_aws_cloudwatch_log_destination.go
150 lines (122 loc) · 3.58 KB
/
resource_aws_cloudwatch_log_destination.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
package aws
import (
"fmt"
"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/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsCloudWatchLogDestination() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloudWatchLogDestinationPut,
Update: resourceAwsCloudWatchLogDestinationPut,
Read: resourceAwsCloudWatchLogDestinationRead,
Delete: resourceAwsCloudWatchLogDestinationDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.Set("name", d.Id())
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"role_arn": {
Type: schema.TypeString,
Required: true,
},
"target_arn": {
Type: schema.TypeString,
Required: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsCloudWatchLogDestinationPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
name := d.Get("name").(string)
role_arn := d.Get("role_arn").(string)
target_arn := d.Get("target_arn").(string)
params := &cloudwatchlogs.PutDestinationInput{
DestinationName: aws.String(name),
RoleArn: aws.String(role_arn),
TargetArn: aws.String(target_arn),
}
return resource.Retry(3*time.Minute, func() *resource.RetryError {
resp, err := conn.PutDestination(params)
if err == nil {
d.SetId(name)
d.Set("arn", *resp.Destination.Arn)
}
awsErr, ok := err.(awserr.Error)
if !ok {
return resource.RetryableError(err)
}
if awsErr.Code() == "InvalidParameterException" {
if strings.Contains(awsErr.Message(), "Could not deliver test message to specified") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return resource.NonRetryableError(err)
})
}
func resourceAwsCloudWatchLogDestinationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
name := d.Get("name").(string)
destination, exists, err := lookupCloudWatchLogDestination(conn, name, nil)
if err != nil {
return err
}
if !exists {
d.SetId("")
return nil
}
d.SetId(name)
d.Set("arn", destination.Arn)
d.Set("role_arn", destination.RoleArn)
d.Set("target_arn", destination.TargetArn)
return nil
}
func resourceAwsCloudWatchLogDestinationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
name := d.Get("name").(string)
params := &cloudwatchlogs.DeleteDestinationInput{
DestinationName: aws.String(name),
}
_, err := conn.DeleteDestination(params)
if err != nil {
return fmt.Errorf("Error deleting Destination with name %s", name)
}
return nil
}
func lookupCloudWatchLogDestination(conn *cloudwatchlogs.CloudWatchLogs,
name string, nextToken *string) (*cloudwatchlogs.Destination, bool, error) {
input := &cloudwatchlogs.DescribeDestinationsInput{
DestinationNamePrefix: aws.String(name),
NextToken: nextToken,
}
resp, err := conn.DescribeDestinations(input)
if err != nil {
return nil, true, err
}
for _, destination := range resp.Destinations {
if *destination.DestinationName == name {
return destination, true, nil
}
}
if resp.NextToken != nil {
return lookupCloudWatchLogDestination(conn, name, resp.NextToken)
}
return nil, false, nil
}