-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_balancer.go
339 lines (310 loc) · 11.7 KB
/
load_balancer.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package elestio
import (
"fmt"
)
type LoadBalancerHandler struct {
client *Client
}
const (
LoadBalancerDeploymentStatusDeployed string = "Deployed"
LoadBalancerDeploymentStatusInProgress string = "IN PROGRESS"
)
type (
LoadBalancer struct {
ID string
ProjectID string
ProviderName string
Datacenter string
ServerType string
Config LoadBalancerConfig
CreatedAt string
CreatorName string
DeploymentStatus string
IPV4 string
IPV6 string
CNAME string
Country string
City string
GlobalIP string
Cores int64
RAMSizeGB string
StorageSizeGB int64
PricePerHour string
}
LoadBalancerConfig struct {
HostHeader string
IsAccessLogsEnabled bool
IsForceHTTPSEnabled bool
IPRateLimit int64
IsIPRateLimitEnabled bool
OutputCacheInSeconds int64
IsStickySessionEnabled bool
IsProxyProtocolEnabled bool
SSLDomains []string
ForwardRules []LoadBalancerConfigForwardRule
OutputHeaders []LoadBalancerConfigOutputHeader
TargetServices []string
RemoveResponseHeaders []string
}
LoadBalancerConfigForwardRule struct {
Protocol string `json:"protocol"`
Port string `json:"listeningPort"`
TargetProtocol string `json:"targetProtocol"`
TargetPort string `json:"targetPort"`
}
LoadBalancerConfigOutputHeader struct {
Key string `json:"key"`
Value string `json:"value"`
}
)
func (h *LoadBalancerHandler) Get(projectID, loadBalancerID string) (*LoadBalancer, error) {
// Fetch load balancer details
reqDetails := struct {
ProjectID string `json:"projectID"`
LoadBalancerID string `json:"vmID"`
JWT string `json:"jwt"`
}{
ProjectID: projectID,
LoadBalancerID: loadBalancerID,
JWT: h.client.jwt,
}
btsDetails, err := h.client.sendPostRequest(
fmt.Sprintf("%s/api/servers/getServerDetails", h.client.BaseURL),
reqDetails,
)
if err != nil {
return nil, err
}
var resDetails struct {
APIResponse
Services []struct {
CreatedAt string `json:"creationDate"`
CreatorName string `json:"creatorName"`
DeploymentStatus string `json:"deploymentStatus"`
IPV4 string `json:"ipv4"`
IPV6 string `json:"ipv6"`
CNAME string `json:"cname"`
Country string `json:"country"`
City string `json:"city"`
GlobalIP string `json:"globalIP"`
Cores int64 `json:"cores"`
RAMSizeGB string `json:"ramGB"`
StorageSizeGB int64 `json:"storageSizeGB"`
PricePerHour string `json:"pricePerHour"`
// TODO: Add all API available fields later
// SSHKeys []ServiceSSHKey `json:"sshKeys"`
// AlertsEnabled NumberAsBool `json:"isAlertsActivated"`
} `json:"serviceInfos"`
}
if err = checkAPIResponse(btsDetails, &resDetails); err != nil {
return nil, err
}
// API returns an array of services, but we only need the first one
if len(resDetails.Services) == 0 {
return nil, fmt.Errorf("load balancer not found")
}
details := resDetails.Services[0]
// Fetch load balancer config
reqConfig := struct {
ProjectID string `json:"projectID"`
LoadBalancerID string `json:"loadBalancerID"`
IsRestoreLb bool `json:"isRestoreLb"`
IsActiveService bool `json:"isActiveService"`
JWT string `json:"jwt"`
}{
ProjectID: projectID,
LoadBalancerID: loadBalancerID,
IsRestoreLb: false,
IsActiveService: true,
JWT: h.client.jwt,
}
btsConfig, err := h.client.sendPostRequest(
fmt.Sprintf("%s/api/loadBalancer/getLBDetails", h.client.BaseURL),
reqConfig,
)
if err != nil {
return nil, err
}
var resConfig struct {
APIResponse
Data struct {
ProjectID string `json:"projectID"`
ProviderName string `json:"providerName"`
Datacenter string `json:"providerRegion"`
ServerType string `json:"planType"`
HostHeader string `json:"hostHeader"`
IsAccessLogsEnabled bool `json:"accessLog"`
IsForceHTTPSEnabled bool `json:"forceHttps"`
IPRateLimit int64 `json:"ipRateLimit"`
IsIPRateLimitEnabled bool `json:"isIpRateLimiter"`
OutputCacheInSeconds int64 `json:"outputCache"`
IsStickySessionEnabled bool `json:"isStickySessions"`
IsProxyProtocolEnabled bool `json:"proxyProtocol"`
SSLDomains []string `json:"sslDomains"`
ForwardRules []LoadBalancerConfigForwardRule `json:"forwardingRules"`
OutputHeaders []LoadBalancerConfigOutputHeader `json:"outputHeaders"`
TargetServices []string `json:"targetServiceIDs"`
RemoveResponseHeaders []string `json:"removeResponseHeaders"`
} `json:"data"`
}
if err = checkAPIResponse(btsConfig, &resConfig); err != nil {
return nil, err
}
config := resConfig.Data
// Build load balancer struct
loadBalancer := LoadBalancer{
ID: loadBalancerID,
ProjectID: config.ProjectID,
ProviderName: config.ProviderName,
Datacenter: config.Datacenter,
ServerType: config.ServerType,
Config: LoadBalancerConfig{
HostHeader: config.HostHeader,
IsAccessLogsEnabled: config.IsAccessLogsEnabled,
IsForceHTTPSEnabled: config.IsForceHTTPSEnabled,
IPRateLimit: config.IPRateLimit,
IsIPRateLimitEnabled: config.IsIPRateLimitEnabled,
OutputCacheInSeconds: config.OutputCacheInSeconds,
IsStickySessionEnabled: config.IsStickySessionEnabled,
IsProxyProtocolEnabled: config.IsProxyProtocolEnabled,
SSLDomains: config.SSLDomains,
ForwardRules: config.ForwardRules,
OutputHeaders: config.OutputHeaders,
TargetServices: config.TargetServices,
RemoveResponseHeaders: config.RemoveResponseHeaders,
},
CreatedAt: details.CreatedAt,
CreatorName: details.CreatorName,
DeploymentStatus: details.DeploymentStatus,
IPV4: details.IPV4,
IPV6: details.IPV6,
CNAME: details.CNAME,
Country: details.Country,
City: details.City,
GlobalIP: details.GlobalIP,
Cores: details.Cores,
RAMSizeGB: details.RAMSizeGB,
StorageSizeGB: details.StorageSizeGB,
PricePerHour: details.PricePerHour,
}
return &loadBalancer, nil
}
type CreateLoadBalancerRequest struct {
ProjectID string `json:"projectId"`
ProviderName string `json:"providerName"`
Datacenter string `json:"datacenter"`
ServerType string `json:"serverType"`
Config CreateLoadBalancerRequestConfig `json:"loadBalancerPayload"`
CreatedFrom string `json:"createdFrom"`
}
type CreateLoadBalancerRequestConfig struct {
HostHeader string `json:"hostHeader"`
IsAccessLogsEnabled bool `json:"accessLog"`
IsForceHTTPSEnabled bool `json:"forceHttps"`
IPRateLimit int64 `json:"ipRateLimit"`
IsIPRateLimitEnabled bool `json:"isIpRateLimiter"`
OutputCacheInSeconds int64 `json:"outputCache"`
IsStickySessionEnabled bool `json:"stickySession"`
IsProxyProtocolEnabled bool `json:"proxyProtocol"`
SSLDomains []string `json:"sslDomains"`
ForwardRules []LoadBalancerConfigForwardRule `json:"forwardRules"`
OutputHeaders []LoadBalancerConfigOutputHeader `json:"outputHeaders"`
TargetServices []string `json:"targetServiceIDs"`
RemoveResponseHeaders []string `json:"removeResponseHeaders"`
}
func (h *LoadBalancerHandler) Create(req CreateLoadBalancerRequest) (*LoadBalancer, error) {
if req.CreatedFrom == "" {
req.CreatedFrom = "goClient"
}
fullReq := struct {
CreateLoadBalancerRequest
ServiceType string `json:"serviceType"`
TemplateID string `json:"templateID"`
JWT string `json:"jwt"`
}{
CreateLoadBalancerRequest: req,
ServiceType: "LB",
JWT: h.client.jwt,
TemplateID: "218",
}
bts, err := h.client.sendPostRequest(
fmt.Sprintf("%s/api/servers/createServer", h.client.BaseURL),
fullReq,
)
if err != nil {
return nil, err
}
var res struct {
APIResponse
ID []FlexString `json:"providerServerID"`
}
if err = checkAPIResponse(bts, &res); err != nil {
return nil, err
}
return h.Get(req.ProjectID, (string)(res.ID[0]))
}
type UpdateLoadBalancerConfigRequest struct {
HostHeader string `json:"hostHeader"`
IsAccessLogsEnabled bool `json:"accessLog"`
IsForceHTTPSEnabled bool `json:"forceHttps"`
IPRateLimit int64 `json:"ipRateLimit"`
IsIPRateLimitEnabled bool `json:"isIpRateLimiter"`
OutputCacheInSeconds int64 `json:"outputCache"`
IsStickySessionEnabled bool `json:"stickySession"`
IsProxyProtocolEnabled bool `json:"proxyProtocol"`
SSLDomains []string `json:"sslDomains"`
ForwardRules []LoadBalancerConfigForwardRule `json:"forwardRules"`
OutputHeaders []LoadBalancerConfigOutputHeader `json:"outputHeaders"`
TargetServices []string `json:"targetServiceIDs"`
RemoveResponseHeaders []string `json:"removeResponseHeaders"`
}
func (h *LoadBalancerHandler) UpdateConfig(projectID string, loadBalancerID string, req UpdateLoadBalancerConfigRequest) (*LoadBalancer, error) {
fullReq := struct {
UpdateLoadBalancerConfigRequest
LoadBalancerID string `json:"vmID"`
Action string `json:"action"`
JWT string `json:"jwt"`
}{
UpdateLoadBalancerConfigRequest: req,
LoadBalancerID: loadBalancerID,
Action: "updateLBSetting",
JWT: h.client.jwt,
}
bts, err := h.client.sendPostRequest(fmt.Sprintf("%s/api/servers/DoActionOnServer", h.client.BaseURL), fullReq)
if err != nil {
return nil, err
}
var res struct {
APIResponse
}
if err = checkAPIResponse(bts, &res); err != nil {
return nil, err
}
return h.Get(projectID, loadBalancerID)
}
func (h *LoadBalancerHandler) Delete(projectID, loadBalancerID string, keepBackups bool) error {
type deleteLoadBalancerRequest struct {
ProjectID string `json:"projectID"`
LoadBalancerID string `json:"vmID"`
IsWithoutBackup bool `json:"isDeleteServiceWithBackup"`
JWT string `json:"jwt"`
}
type deleteLoadBalancerResponse struct {
APIResponse
}
req := deleteLoadBalancerRequest{
ProjectID: projectID,
LoadBalancerID: loadBalancerID,
IsWithoutBackup: !keepBackups,
JWT: h.client.jwt,
}
bts, err := h.client.sendPostRequest(fmt.Sprintf("%s/api/servers/deleteServer", h.client.BaseURL), req)
if err != nil {
return err
}
var res deleteLoadBalancerResponse
if err = checkAPIResponse(bts, &res); err != nil {
return err
}
return nil
}