-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthcheck.go
200 lines (179 loc) · 5.86 KB
/
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
package client
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type Healthcheck struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Type string `json:"type"`
Labels map[string]string `json:"labels,omitempty"`
Timeout string `json:"timeout" validate:"required"`
Interval string `json:"interval" validate:"required"`
CreatedAt time.Time `json:"created-at"`
Enabled bool `json:"enabled"`
Definition any `json:",inline"`
}
type internalHealthcheck struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Type string `json:"type"`
Labels map[string]string `json:"labels,omitempty"`
Enabled bool `json:"enabled"`
Interval string `json:"interval" validate:"required"`
Timeout string `json:"timeout" validate:"required"`
CreatedAt time.Time `json:"created-at"`
}
func (h *Healthcheck) MarshalJSON() ([]byte, error) {
result := make(map[string]any)
internal := internalHealthcheck{
ID: h.ID,
Name: h.Name,
Enabled: h.Enabled,
Description: h.Description,
Type: h.Type,
Timeout: h.Timeout,
Labels: h.Labels,
Interval: h.Interval,
CreatedAt: h.CreatedAt,
}
internalStr, err := json.Marshal(internal)
if err != nil {
return nil, err
}
defStr, err := json.Marshal(h.Definition)
if err != nil {
return nil, err
}
err = json.Unmarshal(internalStr, &result)
if err != nil {
return nil, err
}
err = json.Unmarshal(defStr, &result)
if err != nil {
return nil, err
}
return json.Marshal(result)
}
func (h *Healthcheck) UnmarshalJSON(data []byte) error {
var internal internalHealthcheck
if err := json.Unmarshal(data, &internal); err != nil {
return err
}
if internal.Type == "dns" {
var definition HealthcheckDNSDefinition
if err := json.Unmarshal(data, &definition); err != nil {
return err
}
h.Definition = definition
} else if internal.Type == "tcp" {
var definition HealthcheckTCPDefinition
if err := json.Unmarshal(data, &definition); err != nil {
return err
}
h.Definition = definition
} else if internal.Type == "http" {
var definition HealthcheckHTTPDefinition
if err := json.Unmarshal(data, &definition); err != nil {
return err
}
h.Definition = definition
} else if internal.Type == "tls" {
var definition HealthcheckTLSDefinition
if err := json.Unmarshal(data, &definition); err != nil {
return err
}
h.Definition = definition
} else if internal.Type == "command" {
var definition HealthcheckCommandDefinition
if err := json.Unmarshal(data, &definition); err != nil {
return err
}
h.Definition = definition
} else {
return fmt.Errorf("Unknown healthcheck type %s", h.Type)
}
h.ID = internal.ID
h.Name = internal.Name
h.Timeout = internal.Timeout
h.Description = internal.Description
h.Type = internal.Type
h.Enabled = internal.Enabled
h.Labels = internal.Labels
h.Interval = internal.Interval
h.CreatedAt = internal.CreatedAt
return nil
}
type DeleteHealthcheckInput struct {
ID string `param:"id" description:"Healthcheck ID" validate:"required,uuid"`
}
type GetHealthcheckInput struct {
Identifier string `param:"identifier" description:"Healthcheck Name or ID"`
}
type ListHealthchecksInput struct {
NamePattern string `query:"name-pattern" description:"Returns all health checks whose names are matching this regular expression"`
}
type ListHealthchecksOutput struct {
Result []Healthcheck `json:"result"`
}
type CabourotteDiscoveryInput struct {
// foo=bar,a=b
Labels string `query:"labels"`
Prober uint `query:"prober"`
}
type CabourotteDiscoveryOutput struct {
DNSChecks []Healthcheck `json:"dns-checks,omitempty"`
TCPChecks []Healthcheck `json:"tcp-checks,omitempty"`
HTTPChecks []Healthcheck `json:"http-checks,omitempty"`
TLSChecks []Healthcheck `json:"tls-checks,omitempty"`
CommandChecks []Healthcheck `json:"command-checks,omitempty"`
}
type internalUpdateHealthcheckInput struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Interval string `json:"interval"`
Enabled bool `json:"enabled"`
Timeout string `json:"timeout"`
}
func (c *Client) DeleteHealthcheck(ctx context.Context, input DeleteHealthcheckInput) (Response, error) {
var result Response
_, err := c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/%s", input.ID), http.MethodDelete, nil, &result, nil)
if err != nil {
return Response{}, err
}
return result, nil
}
func (c *Client) GetHealthcheck(ctx context.Context, input GetHealthcheckInput) (Healthcheck, error) {
var result Healthcheck
_, err := c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/%s", input.Identifier), http.MethodGet, nil, &result, nil)
if err != nil {
return Healthcheck{}, err
}
return result, nil
}
func (c *Client) ListHealthchecks(ctx context.Context) (ListHealthchecksOutput, error) {
var result ListHealthchecksOutput
_, err := c.sendRequest(ctx, "/api/v1/healthcheck", http.MethodGet, nil, &result, nil)
if err != nil {
return ListHealthchecksOutput{}, err
}
return result, nil
}
func (c *Client) CabourotteDiscovery(ctx context.Context, input CabourotteDiscoveryInput) (CabourotteDiscoveryOutput, error) {
var result CabourotteDiscoveryOutput
queryParams := make(map[string]string)
if input.Labels != "" {
queryParams["labels"] = input.Labels
}
_, err := c.sendRequest(ctx, "/cabourotte/discovery", http.MethodGet, nil, &result, queryParams)
if err != nil {
return CabourotteDiscoveryOutput{}, err
}
return result, nil
}