-
Notifications
You must be signed in to change notification settings - Fork 24
/
loadbalancer.go
223 lines (195 loc) · 9.39 KB
/
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
package civogo
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
// HealthCheck represents the health check configuration for an instance pool.
type HealthCheck struct {
Port int32 `json:"port"`
Path string `json:"path"`
}
// LoadBalancerBackend represents a backend instance being load-balanced
type LoadBalancerBackend struct {
IP string `json:"ip"`
Protocol string `json:"protocol,omitempty"`
SourcePort int32 `json:"source_port"`
TargetPort int32 `json:"target_port"`
HealthCheckPort int32 `json:"health_check_port,omitempty"`
}
// InstancePool represents an instance pool configuration in a load balancer.
type InstancePool struct {
Tags []string `json:"tags"`
Names []string `json:"names"`
Protocol string `json:"protocol,omitempty"`
SourcePort int32 `json:"source_port"`
TargetPort int32 `json:"target_port"`
HealthCheck HealthCheck `json:"health_check"`
}
// LoadBalancerBackendConfig is the configuration for creating backends
type LoadBalancerBackendConfig struct {
IP string `json:"ip"`
Protocol string `json:"protocol,omitempty"`
SourcePort int32 `json:"source_port"`
TargetPort int32 `json:"target_port"`
HealthCheckPort int32 `json:"health_check_port,omitempty"`
}
// LoadBalancerInstancePoolConfig represents an instance pool configuration in a load balancer.
type LoadBalancerInstancePoolConfig struct {
Tags []string `json:"tags"`
Names []string `json:"names"`
Protocol string `json:"protocol,omitempty"`
SourcePort int32 `json:"source_port"`
TargetPort int32 `json:"target_port"`
HealthCheck HealthCheck `json:"health_check"`
}
// LoadBalancer represents a load balancer configuration within Civo
type LoadBalancer struct {
ID string `json:"id"`
Name string `json:"name"`
ServiceName string `json:"service_name,omitempty"`
Algorithm string `json:"algorithm"`
Backends []LoadBalancerBackend `json:"backends,omitempty"`
InstancePool []InstancePool `json:"instance_pools,omitempty"`
ExternalTrafficPolicy string `json:"external_traffic_policy,omitempty"`
SessionAffinity string `json:"session_affinity,omitempty"`
SessionAffinityConfigTimeout int32 `json:"session_affinity_config_timeout,omitempty"`
EnableProxyProtocol string `json:"enable_proxy_protocol,omitempty"`
PublicIP string `json:"public_ip"`
PrivateIP string `json:"private_ip"`
FirewallID string `json:"firewall_id"`
ClusterID string `json:"cluster_id,omitempty"`
State string `json:"state"`
ReservedIPID string `json:"reserved_ip_id,omitempty"`
ReservedIPName string `json:"reserved_ip_name,omitempty"`
ReservedIP string `json:"reserved_ip,omitempty"`
MaxConcurrentRequests int `json:"max_concurrent_requests,omitempty"`
Options *LoadBalancerOptions `json:"options,omitempty"`
}
// LoadBalancerConfig represents a load balancer to be created
type LoadBalancerConfig struct {
Region string `json:"region"`
Name string `json:"name"`
ServiceName string `json:"service_name,omitempty"`
NetworkID string `json:"network_id,omitempty"`
Algorithm string `json:"algorithm,omitempty"`
Backends []LoadBalancerBackendConfig `json:"backends,omitempty"`
InstancePools []LoadBalancerInstancePoolConfig `json:"instance_pools,omitempty"`
ExternalTrafficPolicy string `json:"external_traffic_policy,omitempty"`
SessionAffinity string `json:"session_affinity,omitempty"`
SessionAffinityConfigTimeout int32 `json:"session_affinity_config_timeout,omitempty"`
EnableProxyProtocol string `json:"enable_proxy_protocol,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
FirewallID string `json:"firewall_id,omitempty"`
FirewallRules string `json:"firewall_rule,omitempty"`
MaxConcurrentRequests *int `json:"max_concurrent_requests,omitempty"`
LoadBalancerOptions *LoadBalancerOptions `json:"options,omitempty"`
}
// LoadBalancerOptions are additional loadbalancer options
type LoadBalancerOptions struct {
ServerTimeout string `json:"server_timeout,omitempty"`
ClientTimeout string `json:"client_timeout,omitempty"`
}
// LoadBalancerUpdateConfig represents a load balancer to be updated
type LoadBalancerUpdateConfig struct {
Region string `json:"region"`
Name string `json:"name,omitempty"`
ServiceName string `json:"service_name,omitempty"`
Algorithm string `json:"algorithm,omitempty"`
Backends []LoadBalancerBackendConfig `json:"backends,omitempty"`
InstancePools []LoadBalancerInstancePoolConfig `json:"instance_pools,omitempty"`
ExternalTrafficPolicy string `json:"external_traffic_policy,omitempty"`
SessionAffinity string `json:"session_affinity,omitempty"`
SessionAffinityConfigTimeout int32 `json:"session_affinity_config_timeout,omitempty"`
EnableProxyProtocol string `json:"enable_proxy_protocol,omitempty"`
FirewallID string `json:"firewall_id,omitempty"`
MaxConcurrentRequests *int `json:"max_concurrent_requests,omitempty"`
LoadBalancerOptions *LoadBalancerOptions `json:"options,omitempty"`
}
// ListLoadBalancers returns all load balancers owned by the calling API account
func (c *Client) ListLoadBalancers() ([]LoadBalancer, error) {
resp, err := c.SendGetRequest("/v2/loadbalancers")
if err != nil {
return nil, decodeError(err)
}
loadbalancer := make([]LoadBalancer, 0)
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&loadbalancer); err != nil {
return nil, decodeError(err)
}
return loadbalancer, nil
}
// GetLoadBalancer returns a load balancer
func (c *Client) GetLoadBalancer(id string) (*LoadBalancer, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/loadbalancers/%s", id))
if err != nil {
return nil, decodeError(err)
}
loadbalancer := &LoadBalancer{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&loadbalancer); err != nil {
return nil, decodeError(err)
}
return loadbalancer, nil
}
// FindLoadBalancer finds a load balancer by either part of the ID or part of the name
func (c *Client) FindLoadBalancer(search string) (*LoadBalancer, error) {
lbs, err := c.ListLoadBalancers()
if err != nil {
return nil, decodeError(err)
}
exactMatch := false
partialMatchesCount := 0
result := LoadBalancer{}
for _, value := range lbs {
if value.Name == search || value.ID == search {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if !exactMatch {
result = value
partialMatchesCount++
}
}
}
if exactMatch || partialMatchesCount == 1 {
return &result, nil
} else if partialMatchesCount > 1 {
err := fmt.Errorf("unable to find %s because there were multiple matches", search)
return nil, MultipleMatchesError.wrap(err)
} else {
err := fmt.Errorf("unable to find %s, zero matches", search)
return nil, ZeroMatchesError.wrap(err)
}
}
// CreateLoadBalancer creates a new load balancer
func (c *Client) CreateLoadBalancer(r *LoadBalancerConfig) (*LoadBalancer, error) {
body, err := c.SendPostRequest("/v2/loadbalancers", r)
if err != nil {
return nil, decodeError(err)
}
loadbalancer := &LoadBalancer{}
if err := json.NewDecoder(bytes.NewReader(body)).Decode(loadbalancer); err != nil {
return nil, err
}
return loadbalancer, nil
}
// UpdateLoadBalancer updates a load balancer
func (c *Client) UpdateLoadBalancer(id string, r *LoadBalancerUpdateConfig) (*LoadBalancer, error) {
body, err := c.SendPutRequest(fmt.Sprintf("/v2/loadbalancers/%s", id), r)
if err != nil {
return nil, decodeError(err)
}
loadbalancer := &LoadBalancer{}
if err := json.NewDecoder(bytes.NewReader(body)).Decode(loadbalancer); err != nil {
return nil, err
}
return loadbalancer, nil
}
// DeleteLoadBalancer deletes a load balancer
func (c *Client) DeleteLoadBalancer(id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/loadbalancers/%s", id))
if err != nil {
return nil, decodeError(err)
}
return c.DecodeSimpleResponse(resp)
}