forked from kevholditch/gokong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upstreams.go
200 lines (157 loc) · 6.32 KB
/
upstreams.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 gokong
import (
"encoding/json"
"fmt"
)
type UpstreamClient struct {
config *Config
}
type UpstreamRequest struct {
Name string `json:"name"`
Slots int `json:"slots,omitempty"`
HashOn string `json:"hash_on,omitempty"`
HashFallback string `json:"hash_fallback,omitempty"`
HashOnHeader string `json:"hash_on_header,omitempty"`
HashFallbackHeader string `json:"hash_fallback_header,omitempty"`
HashOnCookie string `json:"hash_on_cookie,omitempty"`
HashOnCookiePath string `json:"hash_on_cookie_path,omitempty"`
HealthChecks *UpstreamHealthCheck `json:"healthchecks,omitempty"`
}
type UpstreamHealthCheck struct {
Active *UpstreamHealthCheckActive `json:"active,omitempty"`
Passive *UpstreamHealthCheckPassive `json:"passive,omitempty"`
}
type UpstreamHealthCheckActive struct {
Type string `json:"type,omitempty"`
Concurrency int `json:"concurrency,omitempty"`
Healthy *ActiveHealthy `json:"healthy,omitempty"`
HttpPath string `json:"http_path,omitempty"`
HttpsVerifyCertificate bool `json:"https_verify_certificate"`
HttpsSni *string `json:"https_sni,omitempty"`
Timeout int `json:"timeout,omitempty"`
Unhealthy *ActiveUnhealthy `json:"unhealthy,omitempty"`
}
type ActiveHealthy struct {
HttpStatuses []int `json:"http_statuses,omitempty"`
Interval int `json:"interval,omitempty"`
Successes int `json:"successes,omitempty"`
}
type ActiveUnhealthy struct {
HttpFailures int `json:"http_failures,omitempty"`
HttpStatuses []int `json:"http_statuses,omitempty"`
Interval int `json:"interval,omitempty"`
TcpFailures int `json:"tcp_failures,omitempty"`
Timeouts int `json:"timeouts,omitempty"`
}
type UpstreamHealthCheckPassive struct {
Type string `json:"type,omitempty"`
Healthy *PassiveHealthy `json:"healthy,omitempty"`
Unhealthy *PassiveUnhealthy `json:"unhealthy,omitempty"`
}
type PassiveHealthy struct {
HttpStatuses []int `json:"http_statuses,omitempty"`
Successes int `json:"successes,omitempty"`
}
type PassiveUnhealthy struct {
HttpFailures int `json:"http_failures,omitempty"`
HttpStatuses []int `json:"http_statuses,omitempty"`
TcpFailures int `json:"tcp_failures,omitempty"`
Timeouts int `json:"timeouts,omitempty"`
}
type Upstream struct {
Id string `json:"id,omitempty"`
UpstreamRequest
}
type Upstreams struct {
Results []*Upstream `json:"data,omitempty"`
Next string `json:"next,omitempty"`
}
const UpstreamsPath = "/upstreams/"
func (upstreamClient *UpstreamClient) GetByName(name string) (*Upstream, error) {
return upstreamClient.GetById(name)
}
func (upstreamClient *UpstreamClient) GetById(id string) (*Upstream, error) {
r, body, errs := newGet(upstreamClient.config, upstreamClient.config.HostAddress+UpstreamsPath+id).End()
if errs != nil {
return nil, fmt.Errorf("could not get upstream, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
upstream := &Upstream{}
err := json.Unmarshal([]byte(body), upstream)
if err != nil {
return nil, fmt.Errorf("could not parse upstream get response, error: %v", err)
}
if upstream.Id == "" {
return nil, nil
}
return upstream, nil
}
func (upstreamClient *UpstreamClient) Create(upstreamRequest *UpstreamRequest) (*Upstream, error) {
r, body, errs := newPost(upstreamClient.config, upstreamClient.config.HostAddress+UpstreamsPath).Send(upstreamRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not create new upstream, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
createdUpstream := &Upstream{}
err := json.Unmarshal([]byte(body), createdUpstream)
if err != nil {
return nil, fmt.Errorf("could not parse upstream creation response, error: %v", err)
}
if createdUpstream.Id == "" {
return nil, fmt.Errorf("could not create update, error: %v", body)
}
return createdUpstream, nil
}
func (upstreamClient *UpstreamClient) DeleteByName(name string) error {
return upstreamClient.DeleteById(name)
}
func (upstreamClient *UpstreamClient) DeleteById(id string) error {
r, body, errs := newDelete(upstreamClient.config, upstreamClient.config.HostAddress+UpstreamsPath+id).End()
if errs != nil {
return fmt.Errorf("could not delete upstream, result: %v error: %v", r, errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return fmt.Errorf("not authorised, message from kong: %s", body)
}
return nil
}
func (upstreamClient *UpstreamClient) List() (*Upstreams, error) {
r, body, errs := newGet(upstreamClient.config, upstreamClient.config.HostAddress+UpstreamsPath).End()
if errs != nil {
return nil, fmt.Errorf("could not get upstreams, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
upstreams := &Upstreams{}
err := json.Unmarshal([]byte(body), upstreams)
if err != nil {
return nil, fmt.Errorf("could not parse upstreams list response, error: %v", err)
}
return upstreams, nil
}
func (upstreamClient *UpstreamClient) UpdateByName(name string, upstreamRequest *UpstreamRequest) (*Upstream, error) {
return upstreamClient.UpdateById(name, upstreamRequest)
}
func (upstreamClient *UpstreamClient) UpdateById(id string, upstreamRequest *UpstreamRequest) (*Upstream, error) {
r, body, errs := newPatch(upstreamClient.config, upstreamClient.config.HostAddress+UpstreamsPath+id).Send(upstreamRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not update upstream, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
updatedUpstream := &Upstream{}
err := json.Unmarshal([]byte(body), updatedUpstream)
if err != nil {
return nil, fmt.Errorf("could not parse upstream update response, error: %v", err)
}
if updatedUpstream.Id == "" {
return nil, fmt.Errorf("could not update upstream, error: %v", body)
}
return updatedUpstream, nil
}