forked from bizflycloud/gobizfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadbalancer_loadbalancer.go
238 lines (211 loc) · 7.44 KB
/
loadbalancer_loadbalancer.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package gobizfly
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
)
var _ LoadBalancerService = (*loadbalancer)(nil)
// LoadBalancerService is an interface to interact with Bizfly API Load Balancers endpoint.
type LoadBalancerService interface {
List(ctx context.Context, opts *ListOptions) ([]*LoadBalancer, error)
Create(ctx context.Context, req *LoadBalancerCreateRequest) (*LoadBalancer, error)
Get(ctx context.Context, id string) (*LoadBalancer, error)
Update(ctx context.Context, id string, req *LoadBalancerUpdateRequest) (*LoadBalancer, error)
Delete(ctx context.Context, req *LoadBalancerDeleteRequest) error
Resize(ctx context.Context, id string, newType string) error
}
type ListenerHealthMonitor struct {
Type string `json:"type"`
URLPath string `json:"url_path"`
HTTPMethod string `json:"http_method"`
ExpectedCodes string `json:"expected_codes"`
MaxRetries int `json:"max_retries"`
MaxRetriesDown int `json:"max_retries_down"`
Delay int `json:"delay"`
Timeout int `json:"timeout"`
}
type LoadBalancerResizeRequest struct {
Action string `json:"action"`
NewType string `json:"new_type"`
}
type ListenerPool struct {
LbAlgorithm string `json:"lb_algorithm"`
Name string `json:"name"`
Protocol string `json:"protocol"`
Members []string `json:"members"`
HealthMonitor ListenerHealthMonitor `json:"healthmonitor"`
}
type LoadBalancerListener struct {
Name string `json:"name"`
Protocol string `json:"protocol"`
ProtocolPort int `json:"protocol_port"`
DefaultTLSRef string `json:"default_tls_container_ref,omitempty"`
DefaultPool ListenerPool `json:"default_pool"`
}
// LoadBalancerCreateRequest represents create new load balancer request payload.
type LoadBalancerCreateRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
NetworkType string `json:"network_type"`
VPCNetworkID string `json:"vip_network_id"`
Listeners []LoadBalancerListener `json:"listeners,omitempty"`
Type string `json:"type"`
}
// LoadBalancerUpdateRequest represents update load balancer request payload.
type LoadBalancerUpdateRequest struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
AdminStateUp *bool `json:"admin_state_up,omitempty"`
}
// LoadBalancerDeleteRequest represents delete load balancer request payload.
type LoadBalancerDeleteRequest struct {
Cascade bool `json:"cascade"`
ID string `json:"loadbalancer_id"`
}
// LoadBalancer contains load balancer information.
type LoadBalancer struct {
ID string `json:"id"`
FlavorID string `json:"flavor_id"`
Description string `json:"description"`
Provider string `json:"provider"`
UpdatedAt string `json:"updated_at"`
Listeners []resourceID `json:"listeners"`
VipSubnetID string `json:"vip_subnet_id"`
ProjectID string `json:"project_id"`
VipQosPolicyID string `json:"vip_qos_policy_id"`
VipNetworkID string `json:"vip_network_id"`
NetworkType string `json:"network_type"`
VipAddress string `json:"vip_address"`
VipPortID string `json:"vip_port_id"`
AdminStateUp bool `json:"admin_state_up"`
Name string `json:"name"`
OperatingStatus string `json:"operating_status"`
ProvisioningStatus string `json:"provisioning_status"`
Pools []resourceID `json:"pools"`
Type string `json:"type"`
TenantID string `json:"tenant_id"`
CreatedAt string `json:"created_at"`
}
type loadbalancer struct {
client *Client
}
func (l *loadbalancer) resourcePath() string {
return loadBalancersPath
}
func (l *loadbalancer) itemPath(id string) string {
return strings.Join([]string{loadBalancerResourcePath, id}, "/")
}
// List returns a list of load balancers' information.
func (l *loadbalancer) List(ctx context.Context, opts *ListOptions) ([]*LoadBalancer, error) {
req, err := l.client.NewRequest(ctx, http.MethodGet, loadBalancerServiceName, l.resourcePath(), nil)
if err != nil {
return nil, err
}
resp, err := l.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
LoadBalancers []*LoadBalancer `json:"loadbalancers"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.LoadBalancers, nil
}
// Create - creates a new load balancer.
func (l *loadbalancer) Create(ctx context.Context, lbcr *LoadBalancerCreateRequest) (*LoadBalancer, error) {
var data struct {
LoadBalancer *LoadBalancerCreateRequest `json:"loadbalancer"`
}
data.LoadBalancer = lbcr
req, err := l.client.NewRequest(ctx, http.MethodPost, loadBalancerServiceName, l.resourcePath(), &data)
if err != nil {
return nil, err
}
resp, err := l.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respData struct {
LoadBalancer *LoadBalancer `json:"loadbalancer"`
}
if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil {
return nil, err
}
return respData.LoadBalancer, nil
}
// Get - retrieves a load balancer by its ID.
func (l *loadbalancer) Get(ctx context.Context, id string) (*LoadBalancer, error) {
req, err := l.client.NewRequest(ctx, http.MethodGet, loadBalancerServiceName, l.itemPath(id), nil)
if err != nil {
return nil, err
}
resp, err := l.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
lb := &LoadBalancer{}
if err := json.NewDecoder(resp.Body).Decode(lb); err != nil {
return nil, err
}
return lb, nil
}
// Update - update the load balancer's information.
func (l *loadbalancer) Update(ctx context.Context, id string, lbur *LoadBalancerUpdateRequest) (*LoadBalancer, error) {
var data struct {
LoadBalancer *LoadBalancerUpdateRequest `json:"loadbalancer"`
}
data.LoadBalancer = lbur
req, err := l.client.NewRequest(ctx, http.MethodPut, loadBalancerServiceName, l.itemPath(id), &data)
if err != nil {
return nil, err
}
resp, err := l.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respData struct {
LoadBalancer *LoadBalancer `json:"loadbalancer"`
}
if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil {
return nil, err
}
return respData.LoadBalancer, nil
}
// Delete - deletes a load balancer by its ID.
func (l *loadbalancer) Delete(ctx context.Context, lbdr *LoadBalancerDeleteRequest) error {
req, err := l.client.NewRequest(ctx, http.MethodDelete, loadBalancerServiceName, l.itemPath(lbdr.ID), lbdr)
if err != nil {
return err
}
resp, err := l.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}
func (l *loadbalancer) Resize(ctx context.Context, id string, newType string) error {
lrq := &LoadBalancerResizeRequest{
NewType: newType,
Action: "resize",
}
req, err := l.client.NewRequest(ctx, http.MethodPost, loadBalancerServiceName, l.itemPath(id)+"/action", lrq)
if err != nil {
return err
}
resp, err := l.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}