forked from PagerDuty/terraform-provider-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_pagerduty_escalation_policy.go
258 lines (207 loc) · 6.42 KB
/
resource_pagerduty_escalation_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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package pagerduty
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)
func resourcePagerDutyEscalationPolicy() *schema.Resource {
return &schema.Resource{
Create: resourcePagerDutyEscalationPolicyCreate,
Read: resourcePagerDutyEscalationPolicyRead,
Update: resourcePagerDutyEscalationPolicyUpdate,
Delete: resourcePagerDutyEscalationPolicyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
},
"num_loops": {
Type: schema.TypeInt,
Optional: true,
},
"teams": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"rule": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"escalation_delay_in_minutes": {
Type: schema.TypeInt,
Required: true,
},
"target": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
Default: "user_reference",
},
"id": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
},
}
}
func buildEscalationPolicyStruct(d *schema.ResourceData) *pagerduty.EscalationPolicy {
escalationPolicy := &pagerduty.EscalationPolicy{
Name: d.Get("name").(string),
EscalationRules: expandEscalationRules(d.Get("rule").([]interface{})),
}
if attr, ok := d.GetOk("description"); ok {
escalationPolicy.Description = attr.(string)
}
if attr, ok := d.GetOk("num_loops"); ok {
escalationPolicy.NumLoops = attr.(int)
}
if attr, ok := d.GetOk("teams"); ok {
escalationPolicy.Teams = expandTeams(attr.([]interface{}))
}
return escalationPolicy
}
func resourcePagerDutyEscalationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
escalationPolicy := buildEscalationPolicyStruct(d)
log.Printf("[INFO] Creating PagerDuty escalation policy: %s", escalationPolicy.Name)
escalationPolicy, _, err := client.EscalationPolicies.Create(escalationPolicy)
if err != nil {
return err
}
d.SetId(escalationPolicy.ID)
return resourcePagerDutyEscalationPolicyRead(d, meta)
}
func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
log.Printf("[INFO] Reading PagerDuty escalation policy: %s", d.Id())
o := &pagerduty.GetEscalationPolicyOptions{}
return resource.Retry(2*time.Minute, func() *resource.RetryError {
escalationPolicy, _, err := client.EscalationPolicies.Get(d.Id(), o)
if err != nil {
errResp := handleNotFoundError(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}
return nil
}
d.Set("name", escalationPolicy.Name)
d.Set("description", escalationPolicy.Description)
d.Set("num_loops", escalationPolicy.NumLoops)
if err := d.Set("teams", flattenTeams(escalationPolicy.Teams)); err != nil {
return resource.NonRetryableError(fmt.Errorf("error setting teams: %s", err))
}
if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
return resource.NonRetryableError(err)
}
return nil
})
}
func resourcePagerDutyEscalationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
escalationPolicy := buildEscalationPolicyStruct(d)
log.Printf("[INFO] Updating PagerDuty escalation policy: %s", d.Id())
retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if _, _, err := client.EscalationPolicies.Update(d.Id(), escalationPolicy); err != nil {
return resource.RetryableError(err)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}
return nil
}
func resourcePagerDutyEscalationPolicyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
log.Printf("[INFO] Deleting PagerDuty escalation policy: %s", d.Id())
if _, err := client.EscalationPolicies.Delete(d.Id()); err != nil {
return err
}
d.SetId("")
return nil
}
func expandEscalationRules(v interface{}) []*pagerduty.EscalationRule {
var escalationRules []*pagerduty.EscalationRule
for _, er := range v.([]interface{}) {
rer := er.(map[string]interface{})
escalationRule := &pagerduty.EscalationRule{
EscalationDelayInMinutes: rer["escalation_delay_in_minutes"].(int),
}
for _, ert := range rer["target"].([]interface{}) {
rert := ert.(map[string]interface{})
escalationRuleTarget := &pagerduty.EscalationTargetReference{
ID: rert["id"].(string),
Type: rert["type"].(string),
}
escalationRule.Targets = append(escalationRule.Targets, escalationRuleTarget)
}
escalationRules = append(escalationRules, escalationRule)
}
return escalationRules
}
func flattenEscalationRules(v []*pagerduty.EscalationRule) []map[string]interface{} {
var escalationRules []map[string]interface{}
for _, er := range v {
escalationRule := map[string]interface{}{
"id": er.ID,
"escalation_delay_in_minutes": er.EscalationDelayInMinutes,
}
var targets []map[string]interface{}
for _, ert := range er.Targets {
escalationRuleTarget := map[string]interface{}{"id": ert.ID, "type": ert.Type}
targets = append(targets, escalationRuleTarget)
}
escalationRule["target"] = targets
escalationRules = append(escalationRules, escalationRule)
}
return escalationRules
}
func expandTeams(v interface{}) []*pagerduty.TeamReference {
var teams []*pagerduty.TeamReference
for _, t := range v.([]interface{}) {
team := &pagerduty.TeamReference{
ID: t.(string),
Type: "team_reference",
}
teams = append(teams, team)
}
return teams
}
func flattenTeams(teams []*pagerduty.TeamReference) []string {
res := make([]string, len(teams))
for i, t := range teams {
res[i] = t.ID
}
return res
}