-
Notifications
You must be signed in to change notification settings - Fork 16
/
loadbalancer.go
259 lines (226 loc) · 7.3 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
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
package profitbricks
import (
"context"
"github.com/ionos-cloud/sdk-go/v5"
"net/http"
)
//Loadbalancer object
type Loadbalancer struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Properties LoadbalancerProperties `json:"properties,omitempty"`
Entities LoadbalancerEntities `json:"entities,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
//LoadbalancerProperties object
type LoadbalancerProperties struct {
Name string `json:"name,omitempty"`
IP string `json:"ip,omitempty"`
Dhcp bool `json:"dhcp,omitempty"`
}
//LoadbalancerEntities object
type LoadbalancerEntities struct {
Balancednics *BalancedNics `json:"balancednics,omitempty"`
}
//BalancedNics object
type BalancedNics struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Nic `json:"items,omitempty"`
}
//Loadbalancers object
type Loadbalancers struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Loadbalancer `json:"items,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
//ListLoadbalancers returns a Collection struct for loadbalancers in the Datacenter
func (c *Client) ListLoadbalancers(dcid string) (*Loadbalancers, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersGet(ctx, dcid).Execute()
ret := Loadbalancers{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := loadbalancersPath(dcid)
ret := &Loadbalancers{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
//CreateLoadbalancer creates a loadbalancer in the datacenter from a jason []byte and returns a Instance struct
func (c *Client) CreateLoadbalancer(dcid string, request Loadbalancer) (*Loadbalancer, error) {
input := ionoscloud.Loadbalancer{}
if errConvert := convertToCore(&request, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersPost(ctx, dcid).Loadbalancer(input).Execute()
ret := Loadbalancer{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := loadbalancersPath(dcid)
ret := &Loadbalancer{}
err := c.Post(url, request, ret, http.StatusAccepted)
return ret, err
*/
}
//GetLoadbalancer pulls data for the Loadbalancer where id = lbalid returns a Instance struct
func (c *Client) GetLoadbalancer(dcid, lbalid string) (*Loadbalancer, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersFindById(ctx, dcid, lbalid).Execute()
ret := Loadbalancer{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := loadbalancerPath(dcid, lbalid)
ret := &Loadbalancer{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
//UpdateLoadbalancer updates a load balancer
func (c *Client) UpdateLoadbalancer(dcid string, lbalid string, obj LoadbalancerProperties) (*Loadbalancer, error) {
input := ionoscloud.LoadbalancerProperties{}
if errConvert := convertToCore(&obj, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersPatch(ctx, dcid, lbalid).Loadbalancer(input).Execute()
ret := Loadbalancer{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := loadbalancerPath(dcid, lbalid)
ret := &Loadbalancer{}
err := c.Patch(url, obj, ret, http.StatusAccepted)
return ret, err
*/
}
//DeleteLoadbalancer deletes a load balancer
func (c *Client) DeleteLoadbalancer(dcid, lbalid string) (*http.Header, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
_, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersDelete(ctx, dcid, lbalid).Execute()
if apiResponse != nil {
return &apiResponse.Header, err
} else {
return nil, err
}
/*
url := loadbalancerPath(dcid, lbalid)
ret := &http.Header{}
err := c.Delete(url, ret, http.StatusAccepted)
return ret, err
*/
}
//ListBalancedNics lists balanced nics
func (c *Client) ListBalancedNics(dcid, lbalid string) (*Nics, error) {
rsp, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersBalancednicsGet(
context.TODO(), dcid, lbalid).Execute()
ret := Nics{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := balancedNicsPath(dcid, lbalid)
ret := &Nics{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
//AssociateNic attach a nic to load balancer
func (c *Client) AssociateNic(dcid string, lbalid string, nicid string) (*Nic, error) {
input := ionoscloud.Nic{
Id: &nicid,
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersBalancednicsPost(ctx, dcid, lbalid).Nic(input).Execute()
ret := Nic{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
sm := map[string]string{"id": nicid}
url := balancedNicsPath(dcid, lbalid)
ret := &Nic{}
err := c.Post(url, sm, ret, http.StatusAccepted)
return ret, err
*/
}
//GetBalancedNic gets a balanced nic
func (c *Client) GetBalancedNic(dcid, lbalid, balnicid string) (*Nic, error) {
rsp, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersBalancednicsFindByNicId(
context.TODO(), dcid, lbalid, balnicid).Execute()
ret := Nic{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := balancedNicPath(dcid, lbalid, balnicid)
ret := &Nic{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
//DeleteBalancedNic removes a balanced nic
func (c *Client) DeleteBalancedNic(dcid, lbalid, balnicid string) (*http.Header, error) {
_, apiResponse, err := c.CoreSdk.LoadBalancerApi.DatacentersLoadbalancersBalancednicsDelete(
context.TODO(), dcid, lbalid, balnicid).Execute()
if apiResponse != nil {
return &apiResponse.Header, err
} else {
return nil, err
}
/*
url := balancedNicPath(dcid, lbalid, balnicid)
ret := &http.Header{}
err := c.Delete(url, ret, http.StatusAccepted)
return ret, err
*/
}