forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_pagerduty_escalation_policy.go
169 lines (135 loc) · 4.02 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
package pagerduty
import (
"log"
"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform/helper/schema"
)
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 {
escalationRules := d.Get("rule").([]interface{})
escalationPolicy := pagerduty.EscalationPolicy{
Name: d.Get("name").(string),
EscalationRules: expandEscalationRules(escalationRules),
}
if attr, ok := d.GetOk("description"); ok {
escalationPolicy.Description = attr.(string)
}
if attr, ok := d.GetOk("num_loops"); ok {
escalationPolicy.NumLoops = uint(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.CreateEscalationPolicy(*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{}
escalationPolicy, err := client.GetEscalationPolicy(d.Id(), o)
if err != nil {
return err
}
d.Set("name", escalationPolicy.Name)
d.Set("teams", escalationPolicy.Teams)
d.Set("description", escalationPolicy.Description)
d.Set("num_loops", escalationPolicy.NumLoops)
if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
return 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())
if _, err := client.UpdateEscalationPolicy(d.Id(), escalationPolicy); err != nil {
return err
}
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.DeleteEscalationPolicy(d.Id()); err != nil {
return err
}
d.SetId("")
return nil
}