forked from grafana/terraform-provider-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_alert_notification.go
186 lines (153 loc) · 3.9 KB
/
resource_alert_notification.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
package grafana
import (
"errors"
"fmt"
"log"
"strconv"
"time"
"github.com/hashicorp/terraform/helper/schema"
gapi "github.com/nytm/go-grafana-api"
)
var (
ErrFrequencyMustBeSet = errors.New("frequency must be set when send_reminder is set to 'true'")
)
func ResourceAlertNotification() *schema.Resource {
return &schema.Resource{
Create: CreateAlertNotification,
Update: UpdateAlertNotification,
Delete: DeleteAlertNotification,
Read: ReadAlertNotification,
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"is_default": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"send_reminder": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"frequency": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"settings": {
Type: schema.TypeMap,
Optional: true,
Sensitive: true,
},
},
}
}
func CreateAlertNotification(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
alertNotification, err := makeAlertNotification(d)
if err != nil {
return err
}
id, err := client.NewAlertNotification(alertNotification)
if err != nil {
return err
}
d.SetId(strconv.FormatInt(id, 10))
return ReadAlertNotification(d, meta)
}
func UpdateAlertNotification(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
alertNotification, err := makeAlertNotification(d)
if err != nil {
return err
}
return client.UpdateAlertNotification(alertNotification)
}
func ReadAlertNotification(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
idStr := d.Id()
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("Invalid id: %#v", idStr)
}
alertNotification, err := client.AlertNotification(id)
if err != nil {
if err.Error() == "404 Not Found" {
log.Printf("[WARN] removing datasource %s from state because it no longer exists in grafana", d.Get("name").(string))
d.SetId("")
return nil
}
return err
}
settings := map[string]interface{}{}
for k, v := range alertNotification.Settings.(map[string]interface{}) {
boolVal, ok := v.(bool)
if ok && boolVal {
settings[k] = "true"
} else if ok && !boolVal {
settings[k] = "false"
} else {
settings[k] = v
}
}
d.Set("id", alertNotification.Id)
d.Set("is_default", alertNotification.IsDefault)
d.Set("name", alertNotification.Name)
d.Set("type", alertNotification.Type)
d.Set("settings", settings)
return nil
}
func DeleteAlertNotification(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
idStr := d.Id()
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("Invalid id: %#v", idStr)
}
return client.DeleteAlertNotification(id)
}
func makeAlertNotification(d *schema.ResourceData) (*gapi.AlertNotification, error) {
idStr := d.Id()
var id int64
var err error
if idStr != "" {
id, err = strconv.ParseInt(idStr, 10, 64)
}
settings := map[string]interface{}{}
for k, v := range d.Get("settings").(map[string]interface{}) {
strVal, ok := v.(string)
if ok && strVal == "true" {
settings[k] = true
} else if ok && strVal == "false" {
settings[k] = false
} else {
settings[k] = v
}
}
sendReminder := d.Get("send_reminder").(bool)
frequency := d.Get("frequency").(string)
if sendReminder {
if frequency == "" {
return nil, ErrFrequencyMustBeSet
}
if _, err := time.ParseDuration(frequency); err != nil {
return nil, err
}
}
return &gapi.AlertNotification{
Id: id,
Name: d.Get("name").(string),
Type: d.Get("type").(string),
IsDefault: d.Get("is_default").(bool),
SendReminder: sendReminder,
Frequency: frequency,
Settings: settings,
}, err
}