forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_notifylist.go
140 lines (117 loc) · 3.41 KB
/
resource_notifylist.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
package ns1
import (
"github.com/hashicorp/terraform/helper/schema"
ns1 "gopkg.in/ns1/ns1-go.v2/rest"
"gopkg.in/ns1/ns1-go.v2/rest/model/monitor"
)
func notifyListResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"notifications": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"config": &schema.Schema{
Type: schema.TypeMap,
Required: true,
},
},
},
},
},
Create: NotifyListCreate,
Read: NotifyListRead,
Update: NotifyListUpdate,
Delete: NotifyListDelete,
}
}
func notifyListToResourceData(d *schema.ResourceData, nl *monitor.NotifyList) error {
d.SetId(nl.ID)
d.Set("name", nl.Name)
if len(nl.Notifications) > 0 {
notifications := make([]map[string]interface{}, len(nl.Notifications))
for i, n := range nl.Notifications {
ni := make(map[string]interface{})
ni["type"] = n.Type
if n.Config != nil {
ni["config"] = n.Config
}
notifications[i] = ni
}
d.Set("notifications", notifications)
}
return nil
}
func resourceDataToNotifyList(nl *monitor.NotifyList, d *schema.ResourceData) error {
nl.ID = d.Id()
if rawNotifications := d.Get("notifications").([]interface{}); len(rawNotifications) > 0 {
ns := make([]*monitor.Notification, len(rawNotifications))
for i, notificationRaw := range rawNotifications {
ni := notificationRaw.(map[string]interface{})
config := ni["config"].(map[string]interface{})
switch ni["type"].(string) {
case "webhook":
ns[i] = monitor.NewWebNotification(config["url"].(string))
case "email":
ns[i] = monitor.NewEmailNotification(config["email"].(string))
case "datafeed":
ns[i] = monitor.NewFeedNotification(config["sourceid"].(string))
}
}
nl.Notifications = ns
}
return nil
}
// NotifyListCreate creates an ns1 notifylist
func NotifyListCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
nl := monitor.NewNotifyList(d.Get("name").(string))
if err := resourceDataToNotifyList(nl, d); err != nil {
return err
}
if _, err := client.Notifications.Create(nl); err != nil {
return err
}
return notifyListToResourceData(d, nl)
}
// NotifyListRead fetches info for the given notifylist from ns1
func NotifyListRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
nl, _, err := client.Notifications.Get(d.Id())
if err != nil {
return err
}
return notifyListToResourceData(d, nl)
}
// NotifyListDelete deletes the given notifylist from ns1
func NotifyListDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
_, err := client.Notifications.Delete(d.Id())
d.SetId("")
return err
}
// NotifyListUpdate updates the notifylist with given parameters
func NotifyListUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
nl := monitor.NewNotifyList(d.Get("name").(string))
if err := resourceDataToNotifyList(nl, d); err != nil {
return err
}
if _, err := client.Notifications.Update(nl); err != nil {
return err
}
return notifyListToResourceData(d, nl)
}