forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_cloudwatch_dashboard.go
121 lines (104 loc) · 3.49 KB
/
resource_aws_cloudwatch_dashboard.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsCloudWatchDashboard() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloudWatchDashboardPut,
Read: resourceAwsCloudWatchDashboardRead,
Update: resourceAwsCloudWatchDashboardPut,
Delete: resourceAwsCloudWatchDashboardDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
// Note that we specify both the `dashboard_body` and
// the `dashboard_name` as being required, even though
// according to the REST API documentation both are
// optional: http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutDashboard.html#API_PutDashboard_RequestParameters
Schema: map[string]*schema.Schema{
"dashboard_arn": {
Type: schema.TypeString,
Computed: true,
},
"dashboard_body": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateJsonString,
StateFunc: func(v interface{}) string {
json, _ := normalizeJsonString(v)
return json
},
DiffSuppressFunc: suppressEquivalentJsonDiffs,
},
"dashboard_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateCloudWatchDashboardName,
},
},
}
}
func resourceAwsCloudWatchDashboardRead(d *schema.ResourceData, meta interface{}) error {
dashboardName := d.Get("dashboard_name").(string)
log.Printf("[DEBUG] Reading CloudWatch Dashboard: %s", dashboardName)
conn := meta.(*AWSClient).cloudwatchconn
params := cloudwatch.GetDashboardInput{
DashboardName: aws.String(d.Id()),
}
resp, err := conn.GetDashboard(¶ms)
if err != nil {
if isCloudWatchDashboardNotFoundErr(err) {
log.Printf("[WARN] CloudWatch Dashboard %q not found, removing", dashboardName)
d.SetId("")
return nil
}
return fmt.Errorf("Reading dashboard failed: %s", err)
}
d.Set("dashboard_arn", resp.DashboardArn)
d.Set("dashboard_name", resp.DashboardName)
d.Set("dashboard_body", resp.DashboardBody)
return nil
}
func resourceAwsCloudWatchDashboardPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchconn
params := cloudwatch.PutDashboardInput{
DashboardBody: aws.String(d.Get("dashboard_body").(string)),
DashboardName: aws.String(d.Get("dashboard_name").(string)),
}
log.Printf("[DEBUG] Putting CloudWatch Dashboard: %#v", params)
_, err := conn.PutDashboard(¶ms)
if err != nil {
return fmt.Errorf("Putting dashboard failed: %s", err)
}
d.SetId(d.Get("dashboard_name").(string))
log.Println("[INFO] CloudWatch Dashboard put finished")
return resourceAwsCloudWatchDashboardRead(d, meta)
}
func resourceAwsCloudWatchDashboardDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Deleting CloudWatch Dashboard %s", d.Id())
conn := meta.(*AWSClient).cloudwatchconn
params := cloudwatch.DeleteDashboardsInput{
DashboardNames: []*string{aws.String(d.Id())},
}
if _, err := conn.DeleteDashboards(¶ms); err != nil {
if isCloudWatchDashboardNotFoundErr(err) {
log.Printf("[WARN] CloudWatch Dashboard %s is already gone", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error deleting CloudWatch Dashboard: %s", err)
}
log.Printf("[INFO] CloudWatch Dashboard %s deleted", d.Id())
d.SetId("")
return nil
}
func isCloudWatchDashboardNotFoundErr(err error) bool {
return isAWSErr(
err,
"ResourceNotFound",
"does not exist")
}