-
Notifications
You must be signed in to change notification settings - Fork 137
/
block_fastly_service_v1_healthcheck.go
214 lines (192 loc) · 5.74 KB
/
block_fastly_service_v1_healthcheck.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
package fastly
import (
"fmt"
"log"
gofastly "github.com/fastly/go-fastly/fastly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
type HealthCheckServiceAttributeHandler struct {
*DefaultServiceAttributeHandler
}
func NewServiceHealthCheck(sa ServiceMetadata) ServiceAttributeDefinition {
return &HealthCheckServiceAttributeHandler{
&DefaultServiceAttributeHandler{
key: "healthcheck",
serviceMetadata: sa,
},
}
}
func (h *HealthCheckServiceAttributeHandler) Process(d *schema.ResourceData, latestVersion int, conn *gofastly.Client) error {
oh, nh := d.GetChange(h.GetKey())
if oh == nil {
oh = new(schema.Set)
}
if nh == nil {
nh = new(schema.Set)
}
ohs := oh.(*schema.Set)
nhs := nh.(*schema.Set)
removeHealthCheck := ohs.Difference(nhs).List()
addHealthCheck := nhs.Difference(ohs).List()
// DELETE old healthcheck configurations
for _, hRaw := range removeHealthCheck {
hf := hRaw.(map[string]interface{})
opts := gofastly.DeleteHealthCheckInput{
Service: d.Id(),
Version: latestVersion,
Name: hf["name"].(string),
}
log.Printf("[DEBUG] Fastly Healthcheck removal opts: %#v", opts)
err := conn.DeleteHealthCheck(&opts)
if errRes, ok := err.(*gofastly.HTTPError); ok {
if errRes.StatusCode != 404 {
return err
}
} else if err != nil {
return err
}
}
// POST new/updated Healthcheck
for _, hRaw := range addHealthCheck {
hf := hRaw.(map[string]interface{})
opts := gofastly.CreateHealthCheckInput{
Service: d.Id(),
Version: latestVersion,
Name: hf["name"].(string),
Host: hf["host"].(string),
Path: hf["path"].(string),
CheckInterval: uint(hf["check_interval"].(int)),
ExpectedResponse: uint(hf["expected_response"].(int)),
HTTPVersion: hf["http_version"].(string),
Initial: uint(hf["initial"].(int)),
Method: hf["method"].(string),
Threshold: uint(hf["threshold"].(int)),
Timeout: uint(hf["timeout"].(int)),
Window: uint(hf["window"].(int)),
}
log.Printf("[DEBUG] Create Healthcheck Opts: %#v", opts)
_, err := conn.CreateHealthCheck(&opts)
if err != nil {
return err
}
}
return nil
}
func (h *HealthCheckServiceAttributeHandler) Read(d *schema.ResourceData, s *gofastly.ServiceDetail, conn *gofastly.Client) error {
log.Printf("[DEBUG] Refreshing Healthcheck for (%s)", d.Id())
healthcheckList, err := conn.ListHealthChecks(&gofastly.ListHealthChecksInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
})
if err != nil {
return fmt.Errorf("[ERR] Error looking up Healthcheck for (%s), version (%v): %s", d.Id(), s.ActiveVersion.Number, err)
}
hcl := flattenHealthchecks(healthcheckList)
if err := d.Set(h.GetKey(), hcl); err != nil {
log.Printf("[WARN] Error setting Healthcheck for (%s): %s", d.Id(), err)
}
return nil
}
func (h *HealthCheckServiceAttributeHandler) Register(s *schema.Resource) error {
s.Schema[h.GetKey()] = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// required fields
"name": {
Type: schema.TypeString,
Required: true,
Description: "A name to refer to this healthcheck",
},
"host": {
Type: schema.TypeString,
Required: true,
Description: "Which host to check",
},
"path": {
Type: schema.TypeString,
Required: true,
Description: "The path to check",
},
// optional fields
"check_interval": {
Type: schema.TypeInt,
Optional: true,
Default: 5000,
Description: "How often to run the healthcheck in milliseconds",
},
"expected_response": {
Type: schema.TypeInt,
Optional: true,
Default: 200,
Description: "The status code expected from the host",
},
"http_version": {
Type: schema.TypeString,
Optional: true,
Default: "1.1",
Description: "Whether to use version 1.0 or 1.1 HTTP",
},
"initial": {
Type: schema.TypeInt,
Optional: true,
Default: 2,
Description: "When loading a config, the initial number of probes to be seen as OK",
},
"method": {
Type: schema.TypeString,
Optional: true,
Default: "HEAD",
Description: "Which HTTP method to use",
},
"threshold": {
Type: schema.TypeInt,
Optional: true,
Default: 3,
Description: "How many healthchecks must succeed to be considered healthy",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 500,
Description: "Timeout in milliseconds",
},
"window": {
Type: schema.TypeInt,
Optional: true,
Default: 5,
Description: "The number of most recent healthcheck queries to keep for this healthcheck",
},
},
},
}
return nil
}
func flattenHealthchecks(healthcheckList []*gofastly.HealthCheck) []map[string]interface{} {
var hl []map[string]interface{}
for _, h := range healthcheckList {
// Convert HealthChecks to a map for saving to state.
nh := map[string]interface{}{
"name": h.Name,
"host": h.Host,
"path": h.Path,
"check_interval": h.CheckInterval,
"expected_response": h.ExpectedResponse,
"http_version": h.HTTPVersion,
"initial": h.Initial,
"method": h.Method,
"threshold": h.Threshold,
"timeout": h.Timeout,
"window": h.Window,
}
// prune any empty values that come from the default string value in structs
for k, v := range nh {
if v == "" {
delete(nh, k)
}
}
hl = append(hl, nh)
}
return hl
}