forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthchecks.go
207 lines (191 loc) · 7.5 KB
/
healthchecks.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
// Healthcheck describes a Healthcheck object.
type Healthcheck struct {
ID string `json:"id,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
ModifiedOn *time.Time `json:"modified_on,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Suspended bool `json:"suspended"`
Address string `json:"address"`
Retries int `json:"retries,omitempty"`
Timeout int `json:"timeout,omitempty"`
Interval int `json:"interval,omitempty"`
ConsecutiveSuccesses int `json:"consecutive_successes,omitempty"`
ConsecutiveFails int `json:"consecutive_fails,omitempty"`
Type string `json:"type,omitempty"`
CheckRegions []string `json:"check_regions"`
HTTPConfig *HealthcheckHTTPConfig `json:"http_config,omitempty"`
TCPConfig *HealthcheckTCPConfig `json:"tcp_config,omitempty"`
Notification HealthcheckNotification `json:"notification,omitempty"`
Status string `json:"status"`
FailureReason string `json:"failure_reason"`
}
// HealthcheckHTTPConfig describes configuration for a HTTP healthcheck.
type HealthcheckHTTPConfig struct {
Method string `json:"method"`
Port uint16 `json:"port,omitempty"`
Path string `json:"path"`
ExpectedCodes []string `json:"expected_codes"`
ExpectedBody string `json:"expected_body"`
FollowRedirects bool `json:"follow_redirects"`
AllowInsecure bool `json:"allow_insecure"`
Header map[string][]string `json:"header"`
}
// HealthcheckTCPConfig describes configuration for a TCP healthcheck.
type HealthcheckTCPConfig struct {
Method string `json:"method"`
Port uint16 `json:"port,omitempty"`
}
// HealthcheckNotification describes notification configuration for a healthcheck.
type HealthcheckNotification struct {
Suspended bool `json:"suspended,omitempty"`
EmailAddresses []string `json:"email_addresses,omitempty"`
}
// HealthcheckListResponse is the API response, containing an array of healthchecks.
type HealthcheckListResponse struct {
Response
Result []Healthcheck `json:"result"`
ResultInfo `json:"result_info"`
}
// HealthcheckResponse is the API response, containing a single healthcheck.
type HealthcheckResponse struct {
Response
Result Healthcheck `json:"result"`
}
// Healthchecks returns all healthchecks for a zone.
//
// API reference: https://api.cloudflare.com/#health-checks-list-health-checks
func (api *API) Healthchecks(ctx context.Context, zoneID string) ([]Healthcheck, error) {
uri := fmt.Sprintf("/zones/%s/healthchecks", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []Healthcheck{}, err
}
var r HealthcheckListResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []Healthcheck{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// Healthcheck returns a single healthcheck by ID.
//
// API reference: https://api.cloudflare.com/#health-checks-health-check-details
func (api *API) Healthcheck(ctx context.Context, zoneID, healthcheckID string) (Healthcheck, error) {
uri := fmt.Sprintf("/zones/%s/healthchecks/%s", zoneID, healthcheckID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return Healthcheck{}, err
}
var r HealthcheckResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Healthcheck{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// CreateHealthcheck creates a new healthcheck in a zone.
//
// API reference: https://api.cloudflare.com/#health-checks-create-health-check
func (api *API) CreateHealthcheck(ctx context.Context, zoneID string, healthcheck Healthcheck) (Healthcheck, error) {
uri := fmt.Sprintf("/zones/%s/healthchecks", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, healthcheck)
if err != nil {
return Healthcheck{}, err
}
var r HealthcheckResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Healthcheck{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// UpdateHealthcheck updates an existing healthcheck.
//
// API reference: https://api.cloudflare.com/#health-checks-update-health-check
func (api *API) UpdateHealthcheck(ctx context.Context, zoneID string, healthcheckID string, healthcheck Healthcheck) (Healthcheck, error) {
uri := fmt.Sprintf("/zones/%s/healthchecks/%s", zoneID, healthcheckID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, healthcheck)
if err != nil {
return Healthcheck{}, err
}
var r HealthcheckResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Healthcheck{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// DeleteHealthcheck deletes a healthcheck in a zone.
//
// API reference: https://api.cloudflare.com/#health-checks-delete-health-check
func (api *API) DeleteHealthcheck(ctx context.Context, zoneID string, healthcheckID string) error {
uri := fmt.Sprintf("/zones/%s/healthchecks/%s", zoneID, healthcheckID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
var r HealthcheckResponse
err = json.Unmarshal(res, &r)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}
// CreateHealthcheckPreview creates a new preview of a healthcheck in a zone.
//
// API reference: https://api.cloudflare.com/#health-checks-create-preview-health-check
func (api *API) CreateHealthcheckPreview(ctx context.Context, zoneID string, healthcheck Healthcheck) (Healthcheck, error) {
uri := fmt.Sprintf("/zones/%s/healthchecks/preview", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, healthcheck)
if err != nil {
return Healthcheck{}, err
}
var r HealthcheckResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Healthcheck{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// HealthcheckPreview returns a single healthcheck preview by its ID.
//
// API reference: https://api.cloudflare.com/#health-checks-health-check-preview-details
func (api *API) HealthcheckPreview(ctx context.Context, zoneID, id string) (Healthcheck, error) {
uri := fmt.Sprintf("/zones/%s/healthchecks/preview/%s", zoneID, id)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return Healthcheck{}, err
}
var r HealthcheckResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Healthcheck{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// DeleteHealthcheckPreview deletes a healthcheck preview in a zone if it exists.
//
// API reference: https://api.cloudflare.com/#health-checks-delete-preview-health-check
func (api *API) DeleteHealthcheckPreview(ctx context.Context, zoneID string, id string) error {
uri := fmt.Sprintf("/zones/%s/healthchecks/preview/%s", zoneID, id)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
var r HealthcheckResponse
err = json.Unmarshal(res, &r)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}