forked from hashicorp/terraform-provider-aws
/
resource_aws_cloudwatch_log_destination_policy.go
88 lines (70 loc) · 2.26 KB
/
resource_aws_cloudwatch_log_destination_policy.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
package aws
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
func resourceAwsCloudWatchLogDestinationPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloudWatchLogDestinationPolicyPut,
Update: resourceAwsCloudWatchLogDestinationPolicyPut,
Read: resourceAwsCloudWatchLogDestinationPolicyRead,
Delete: resourceAwsCloudWatchLogDestinationPolicyDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.Set("destination_name", d.Id())
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"destination_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"access_policy": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceAwsCloudWatchLogDestinationPolicyPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
destination_name := d.Get("destination_name").(string)
access_policy := d.Get("access_policy").(string)
params := &cloudwatchlogs.PutDestinationPolicyInput{
DestinationName: aws.String(destination_name),
AccessPolicy: aws.String(access_policy),
}
_, err := conn.PutDestinationPolicy(params)
if err != nil {
return fmt.Errorf("Error creating DestinationPolicy with destination_name %s: %#v", destination_name, err)
}
d.SetId(destination_name)
return resourceAwsCloudWatchLogDestinationPolicyRead(d, meta)
}
func resourceAwsCloudWatchLogDestinationPolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
destination_name := d.Get("destination_name").(string)
destination, exists, err := lookupCloudWatchLogDestination(conn, destination_name, nil)
if err != nil {
return err
}
if !exists {
d.SetId("")
return nil
}
if destination.AccessPolicy != nil {
d.SetId(destination_name)
d.Set("access_policy", *destination.AccessPolicy)
} else {
d.SetId("")
}
return nil
}
func resourceAwsCloudWatchLogDestinationPolicyDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}