This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
forked from newrelic/terraform-provider-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_newrelic_synthetics_monitor.go
250 lines (213 loc) · 7.4 KB
/
resource_newrelic_synthetics_monitor.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
package newrelic
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/newrelic/newrelic-client-go/pkg/errors"
"github.com/newrelic/newrelic-client-go/pkg/synthetics"
)
func resourceNewRelicSyntheticsMonitor() *schema.Resource {
return &schema.Resource{
Create: resourceNewRelicSyntheticsMonitorCreate,
Read: resourceNewRelicSyntheticsMonitorRead,
Update: resourceNewRelicSyntheticsMonitorUpdate,
Delete: resourceNewRelicSyntheticsMonitorDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The monitor type. Valid values are SIMPLE, BROWSER, SCRIPT_BROWSER, and SCRIPT_API.",
ValidateFunc: validation.StringInSlice([]string{
"SIMPLE",
"BROWSER",
"SCRIPT_API",
"SCRIPT_BROWSER",
}, false),
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The title of this monitor.",
},
"frequency": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: intInSlice([]int{1, 5, 10, 15, 30, 60, 360, 720, 1440}),
Description: "The interval (in minutes) at which this monitor should run. Valid values are 1, 5, 10, 15, 30, 60, 360, 720, or 1440.",
},
"uri": {
Type: schema.TypeString,
Optional: true,
Description: "The URI for the monitor to hit.",
// TODO: ValidateFunc (required if SIMPLE or BROWSER)
},
"locations": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
MinItems: 1,
Required: true,
Description: "The locations in which this monitor should be run.",
},
"status": {
Type: schema.TypeString,
Required: true,
Description: "The monitor status (i.e. ENABLED, MUTED, DISABLED).",
ValidateFunc: validation.StringInSlice([]string{
"ENABLED",
"MUTED",
"DISABLED",
}, false),
},
"sla_threshold": {
Type: schema.TypeFloat,
Optional: true,
Default: 7,
Description: "The base threshold for the SLA report.",
},
// TODO: ValidationFunc (options only valid if SIMPLE or BROWSER)
"validation_string": {
Type: schema.TypeString,
Optional: true,
Description: "The string to validate against in the response.",
},
"verify_ssl": {
Type: schema.TypeBool,
Optional: true,
Description: "Verify SSL.",
},
"bypass_head_request": {
Type: schema.TypeBool,
Optional: true,
Description: "Bypass HEAD request.",
},
"treat_redirect_as_failure": {
Type: schema.TypeBool,
Optional: true,
Description: "Fail the monitor check if redirected.",
},
},
}
}
func buildSyntheticsMonitorStruct(d *schema.ResourceData) synthetics.Monitor {
monitor := synthetics.Monitor{
Name: d.Get("name").(string),
Type: synthetics.MonitorType(d.Get("type").(string)),
Frequency: uint(d.Get("frequency").(int)),
Status: synthetics.MonitorStatusType(d.Get("status").(string)),
SLAThreshold: d.Get("sla_threshold").(float64),
}
if uri, ok := d.GetOk("uri"); ok {
monitor.URI = uri.(string)
}
locationsRaw := d.Get("locations").(*schema.Set)
locations := make([]string, locationsRaw.Len())
for i, v := range locationsRaw.List() {
locations[i] = fmt.Sprint(v)
}
if validationString, ok := d.GetOk("validation_string"); ok {
monitor.Options.ValidationString = validationString.(string)
}
if verifySSL, ok := d.GetOkExists("verify_ssl"); ok {
monitor.Options.VerifySSL = verifySSL.(bool)
}
if bypassHeadRequest, ok := d.GetOkExists("bypass_head_request"); ok {
monitor.Options.BypassHEADRequest = bypassHeadRequest.(bool)
}
if treatRedirectAsFailure, ok := d.GetOkExists("treat_redirect_as_failure"); ok {
monitor.Options.TreatRedirectAsFailure = treatRedirectAsFailure.(bool)
}
monitor.Locations = locations
return monitor
}
func buildSyntheticsUpdateMonitorArgs(d *schema.ResourceData) *synthetics.Monitor {
monitor := synthetics.Monitor{
ID: d.Id(),
Name: d.Get("name").(string),
Type: synthetics.MonitorType(d.Get("type").(string)),
Frequency: uint(d.Get("frequency").(int)),
Status: synthetics.MonitorStatusType(d.Get("status").(string)),
SLAThreshold: d.Get("sla_threshold").(float64),
}
if uri, ok := d.GetOk("uri"); ok {
monitor.URI = uri.(string)
}
locationsRaw := d.Get("locations").(*schema.Set)
locations := make([]string, locationsRaw.Len())
for i, v := range locationsRaw.List() {
locations[i] = fmt.Sprint(v)
}
if validationString, ok := d.GetOk("validation_string"); ok {
monitor.Options.ValidationString = validationString.(string)
}
if verifySSL, ok := d.GetOkExists("verify_ssl"); ok {
monitor.Options.VerifySSL = verifySSL.(bool)
}
if bypassHeadRequest, ok := d.GetOkExists("bypass_head_request"); ok {
monitor.Options.BypassHEADRequest = bypassHeadRequest.(bool)
}
if treatRedirectAsFailure, ok := d.GetOkExists("treat_redirect_as_failure"); ok {
monitor.Options.TreatRedirectAsFailure = treatRedirectAsFailure.(bool)
}
monitor.Locations = locations
return &monitor
}
func readSyntheticsMonitorStruct(monitor *synthetics.Monitor, d *schema.ResourceData) error {
d.Set("name", monitor.Name)
d.Set("type", monitor.Type)
d.Set("frequency", monitor.Frequency)
d.Set("uri", monitor.URI)
d.Set("locations", monitor.Locations)
d.Set("status", monitor.Status)
d.Set("sla_threshold", monitor.SLAThreshold)
d.Set("verify_ssl", monitor.Options.VerifySSL)
d.Set("validation_string", monitor.Options.ValidationString)
d.Set("bypass_head_request", monitor.Options.BypassHEADRequest)
d.Set("treat_redirect_as_failure", monitor.Options.TreatRedirectAsFailure)
return nil
}
func resourceNewRelicSyntheticsMonitorCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).NewClient
monitorStruct := buildSyntheticsMonitorStruct(d)
log.Printf("[INFO] Creating New Relic Synthetics monitor %s", monitorStruct.Name)
monitor, err := client.Synthetics.CreateMonitor(monitorStruct)
if err != nil {
return err
}
d.SetId(monitor.ID)
return resourceNewRelicSyntheticsMonitorRead(d, meta)
}
func resourceNewRelicSyntheticsMonitorRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).NewClient
log.Printf("[INFO] Reading New Relic Synthetics monitor %s", d.Id())
monitor, err := client.Synthetics.GetMonitor(d.Id())
if err != nil {
if _, ok := err.(*errors.NotFound); ok {
d.SetId("")
return nil
}
return err
}
return readSyntheticsMonitorStruct(monitor, d)
}
func resourceNewRelicSyntheticsMonitorUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).NewClient
log.Printf("[INFO] Updating New Relic Synthetics monitor %s", d.Id())
_, err := client.Synthetics.UpdateMonitor(*buildSyntheticsUpdateMonitorArgs(d))
if err != nil {
return err
}
return resourceNewRelicSyntheticsMonitorRead(d, meta)
}
func resourceNewRelicSyntheticsMonitorDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).NewClient
log.Printf("[INFO] Deleting New Relic Synthetics monitor %s", d.Id())
if err := client.Synthetics.DeleteMonitor(d.Id()); err != nil {
return err
}
return nil
}