Debian Codegraph @

fastly/health_check.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // HealthCheck represents a health check response from the Fastly API.
11 type HealthCheck struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Name string `mapstructure:"name"`
16 Comment string `mapstructure:"comment"`
17 Method string `mapstructure:"method"`
18 Host string `mapstructure:"host"`
19 Path string `mapstructure:"path"`
20 HTTPVersion string `mapstructure:"http_version"`
21 Timeout uint `mapstructure:"timeout"`
22 CheckInterval uint `mapstructure:"check_interval"`
23 ExpectedResponse uint `mapstructure:"expected_response"`
24 Window uint `mapstructure:"window"`
25 Threshold uint `mapstructure:"threshold"`
26 Initial uint `mapstructure:"initial"`
27 CreatedAt *time.Time `mapstructure:"created_at"`
28 UpdatedAt *time.Time `mapstructure:"updated_at"`
29 DeletedAt *time.Time `mapstructure:"deleted_at"`
30 }
31
32 // healthChecksByName is a sortable list of health checks.
33 type healthChecksByName []*HealthCheck
34
35 // Len, Swap, and Less implement the sortable interface.
36 func (s healthChecksByName) Len() int { return len(s) }
37 func (s healthChecksByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
38 func (s healthChecksByName) Less(i, j int) bool {
39 return s[i].Name < s[j].Name
40 }
41
42 // ListHealthChecksInput is used as input to the ListHealthChecks function.
43 type ListHealthChecksInput struct {
44 // Service is the ID of the service (required).
45 Service string
46
47 // Version is the specific configuration version (required).
48 Version int
49 }
50
51 // ListHealthChecks returns the list of health checks for the configuration
52 // version.
53 func (c *Client) ListHealthChecks(i *ListHealthChecksInput) ([]*HealthCheck, error) {
54 if i.Service == "" {
55 return nil, ErrMissingService
56 }
57
58 if i.Version == 0 {
59 return nil, ErrMissingVersion
60 }
61
62 path := fmt.Sprintf("/service/%s/version/%d/healthcheck", i.Service, i.Version)
63 resp, err := c.Get(path, nil)
64 if err != nil {
65 return nil, err
66 }
67
68 var hcs []*HealthCheck
69 if err := decodeJSON(&hcs, resp.Body); err != nil {
70 return nil, err
71 }
72 sort.Stable(healthChecksByName(hcs))
73 return hcs, nil
74 }
75
76 // CreateHealthCheckInput is used as input to the CreateHealthCheck function.
77 type CreateHealthCheckInput struct {
78 // Service is the ID of the service. Version is the specific configuration
79 // version. Both fields are required.
80 Service string
81 Version int
82
83 Name string `form:"name,omitempty"`
84 Comment string `form:"comment,omitempty"`
85 Method string `form:"method,omitempty"`
86 Host string `form:"host,omitempty"`
87 Path string `form:"path,omitempty"`
88 HTTPVersion string `form:"http_version,omitempty"`
89 Timeout uint `form:"timeout,omitempty"`
90 CheckInterval uint `form:"check_interval,omitempty"`
91 ExpectedResponse uint `form:"expected_response,omitempty"`
92 Window uint `form:"window,omitempty"`
93 Threshold uint `form:"threshold,omitempty"`
94 Initial uint `form:"initial,omitempty"`
95 }
96
97 // CreateHealthCheck creates a new Fastly health check.
98 func (c *Client) CreateHealthCheck(i *CreateHealthCheckInput) (*HealthCheck, error) {
99 if i.Service == "" {
100 return nil, ErrMissingService
101 }
102
103 if i.Version == 0 {
104 return nil, ErrMissingVersion
105 }
106
107 path := fmt.Sprintf("/service/%s/version/%d/healthcheck", i.Service, i.Version)
108 resp, err := c.PostForm(path, i, nil)
109 if err != nil {
110 return nil, err
111 }
112
113 var h *HealthCheck
114 if err := decodeJSON(&h, resp.Body); err != nil {
115 return nil, err
116 }
117 return h, nil
118 }
119
120 // GetHealthCheckInput is used as input to the GetHealthCheck function.
121 type GetHealthCheckInput struct {
122 // Service is the ID of the service. Version is the specific configuration
123 // version. Both fields are required.
124 Service string
125 Version int
126
127 // Name is the name of the health check to fetch.
128 Name string
129 }
130
131 // GetHealthCheck gets the health check configuration with the given parameters.
132 func (c *Client) GetHealthCheck(i *GetHealthCheckInput) (*HealthCheck, error) {
133 if i.Service == "" {
134 return nil, ErrMissingService
135 }
136
137 if i.Version == 0 {
138 return nil, ErrMissingVersion
139 }
140
141 if i.Name == "" {
142 return nil, ErrMissingName
143 }
144
145 path := fmt.Sprintf("/service/%s/version/%d/healthcheck/%s", i.Service, i.Version, url.PathEscape(i.Name))
146 resp, err := c.Get(path, nil)
147 if err != nil {
148 return nil, err
149 }
150
151 var h *HealthCheck
152 if err := decodeJSON(&h, resp.Body); err != nil {
153 return nil, err
154 }
155 return h, nil
156 }
157
158 // UpdateHealthCheckInput is used as input to the UpdateHealthCheck function.
159 type UpdateHealthCheckInput struct {
160 // Service is the ID of the service. Version is the specific configuration
161 // version. Both fields are required.
162 Service string
163 Version int
164
165 // Name is the name of the health check to update.
166 Name string
167
168 NewName string `form:"name,omitempty"`
169 Comment string `form:"comment,omitempty"`
170 Method string `form:"method,omitempty"`
171 Host string `form:"host,omitempty"`
172 Path string `form:"path,omitempty"`
173 HTTPVersion string `form:"http_version,omitempty"`
174 Timeout uint `form:"timeout,omitempty"`
175 CheckInterval uint `form:"check_interval,omitempty"`
176 ExpectedResponse uint `form:"expected_response,omitempty"`
177 Window uint `form:"window,omitempty"`
178 Threshold uint `form:"threshold,omitempty"`
179 Initial uint `form:"initial,omitempty"`
180 }
181
182 // UpdateHealthCheck updates a specific health check.
183 func (c *Client) UpdateHealthCheck(i *UpdateHealthCheckInput) (*HealthCheck, error) {
184 if i.Service == "" {
185 return nil, ErrMissingService
186 }
187
188 if i.Version == 0 {
189 return nil, ErrMissingVersion
190 }
191
192 if i.Name == "" {
193 return nil, ErrMissingName
194 }
195
196 path := fmt.Sprintf("/service/%s/version/%d/healthcheck/%s", i.Service, i.Version, url.PathEscape(i.Name))
197 resp, err := c.PutForm(path, i, nil)
198 if err != nil {
199 return nil, err
200 }
201
202 var h *HealthCheck
203 if err := decodeJSON(&h, resp.Body); err != nil {
204 return nil, err
205 }
206 return h, nil
207 }
208
209 // DeleteHealthCheckInput is the input parameter to DeleteHealthCheck.
210 type DeleteHealthCheckInput struct {
211 // Service is the ID of the service. Version is the specific configuration
212 // version. Both fields are required.
213 Service string
214 Version int
215
216 // Name is the name of the health check to delete (required).
217 Name string
218 }
219
220 // DeleteHealthCheck deletes the given health check.
221 func (c *Client) DeleteHealthCheck(i *DeleteHealthCheckInput) error {
222 if i.Service == "" {
223 return ErrMissingService
224 }
225
226 if i.Version == 0 {
227 return ErrMissingVersion
228 }
229
230 if i.Name == "" {
231 return ErrMissingName
232 }
233
234 path := fmt.Sprintf("/service/%s/version/%d/healthcheck/%s", i.Service, i.Version, url.PathEscape(i.Name))
235 resp, err := c.Delete(path, nil)
236 if err != nil {
237 return err
238 }
239
240 var r *statusResp
241 if err := decodeJSON(&r, resp.Body); err != nil {
242 return err
243 }
244 if !r.Ok() {
245 return fmt.Errorf("Not Ok")
246 }
247 return nil
248 }