/
eip.go
398 lines (330 loc) · 10.4 KB
/
eip.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ctyun
import (
"fmt"
"strconv"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
api "yunion.io/x/cloudmux/pkg/apis/compute"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
)
type SEip struct {
region *SRegion
multicloud.SEipBase
CtyunTags
IPVersion int64 `json:"ip_version"`
BandwidthShareType string `json:"bandwidth_share_type"`
Type string `json:"type"`
PrivateIPAddress string `json:"private_ip_address"`
EnterpriseProjectID string `json:"enterprise_project_id"`
Status string `json:"status"`
PublicIPAddress string `json:"public_ip_address"`
ID string `json:"id"`
TenantID string `json:"tenant_id"`
Profile Profile `json:"profile"`
BandwidthName string `json:"bandwidth_name"`
BandwidthID string `json:"bandwidth_id"`
PortID string `json:"port_id"`
BandwidthSize int `json:"bandwidth_size"`
CreateTime int64 `json:"create_time"`
MasterOrderID string `json:"masterOrderId"`
WorkOrderResourceID string `json:"workOrderResourceId"`
ExpireTime int64 `json:"expireTime"`
IsFreeze int64 `json:"isFreeze"`
}
func (self *SEip) GetBillingType() string {
if len(self.MasterOrderID) > 0 {
return billing_api.BILLING_TYPE_PREPAID
} else {
return billing_api.BILLING_TYPE_POSTPAID
}
}
func (self *SEip) GetCreatedAt() time.Time {
return time.Unix(self.CreateTime/1000, 0)
}
func (self *SEip) GetExpiredAt() time.Time {
if self.ExpireTime == 0 {
return time.Time{}
}
return time.Unix(self.ExpireTime/1000, 0)
}
func (self *SEip) GetId() string {
return self.ID
}
func (self *SEip) GetName() string {
return self.BandwidthName
}
func (self *SEip) GetGlobalId() string {
return self.GetId()
}
func (self *SEip) GetStatus() string {
switch self.Status {
case "ACTIVE", "DOWN":
return api.EIP_STATUS_READY
case "ERROR":
return api.EIP_STATUS_ALLOCATE_FAIL
default:
return api.EIP_STATUS_UNKNOWN
}
}
func (self *SEip) Refresh() error {
if self.IsEmulated() {
return nil
}
new, err := self.region.GetEip(self.ID)
if err != nil {
return err
}
return jsonutils.Update(self, new)
}
func (self *SEip) IsEmulated() bool {
return false
}
func (self *SEip) GetProjectId() string {
return ""
}
func (self *SEip) GetIpAddr() string {
return self.PublicIPAddress
}
func (self *SEip) GetMode() string {
return api.EIP_MODE_STANDALONE_EIP
}
func (self *SEip) GetINetworkId() string {
return ""
}
func (self *SEip) GetAssociationType() string {
orders, err := self.region.GetOrder(self.WorkOrderResourceID)
if err != nil {
// bugfix: 由于没有接口可以返向查询出关联的device,这里默认关联的是server?
if len(self.PortID) > 0 || len(self.PrivateIPAddress) > 0 {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
log.Errorf("SEip.GetAssociationType %s", err)
return ""
}
for i := range orders {
order := orders[i]
if strings.Contains(order.ResourceType, "LOADBALANCER") {
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
} else {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
}
return ""
}
// eip查询接口未返回 绑定的实例ID/网卡port id。导致不能正常找出关联的主机
func (self *SEip) GetAssociationExternalId() string {
vms, err := self.region.GetVMs()
if err != nil {
log.Errorf("SEip.GetAssociationExternalId.GetVMs %s", err)
return ""
}
for i := range vms {
vm := vms[i]
nics, err := self.region.GetNics(vm.GetId())
if err != nil {
log.Errorf("SEip.GetAssociationExternalId.GetNics %s", err)
return ""
}
for _, nic := range nics {
if nic.PortID == self.PortID {
return vm.GetGlobalId()
}
}
}
return ""
}
// http://ctyun-api-url/apiproxy/v3/queryNetworkDetail
func (self *SEip) GetBandwidth() int {
return self.BandwidthSize
}
func (self *SEip) GetInternetChargeType() string {
// todo: fix me
return ""
}
func (self *SEip) Delete() error {
return self.region.DeleteEip(self.GetId())
}
func (self *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
nics, err := self.region.GetNics(conf.InstanceId)
if err != nil {
return errors.Wrap(err, "Eip.Associate.GetNics")
}
if len(nics) == 0 {
return errors.Wrap(fmt.Errorf("no network card found"), "Eip.Associate.GetINics")
}
return self.region.AssociateEip(self.GetId(), nics[0].PortID)
}
func (self *SEip) Dissociate() error {
err := self.region.DissociateEip(self.GetId())
if err != nil {
return err
}
err = cloudprovider.WaitStatusWithDelay(self, api.EIP_STATUS_READY, 5*time.Second, 10*time.Second, 180*time.Second)
return errors.Wrap(err, "SEip.Dissociate")
}
func (self *SEip) ChangeBandwidth(bw int) error {
return self.region.ChangeBandwidthEip(self.GetName(), self.GetId(), strconv.Itoa(bw))
}
type Profile struct {
OrderID string `json:"order_id"`
RegionID string `json:"region_id"`
UserID string `json:"user_id"`
ProductID string `json:"product_id"`
}
func (self *SRegion) GetEips() ([]SEip, error) {
params := map[string]string{
"regionId": self.GetId(),
}
eips := make([]SEip, 0)
resp, err := self.client.DoGet("/apiproxy/v3/ondemand/queryIps", params)
if err != nil {
return nil, errors.Wrap(err, "SRegion.GetEips.DoGet")
}
err = resp.Unmarshal(&eips, "returnObj", "publicips")
if err != nil {
return nil, errors.Wrap(err, "SRegion.GetEips.Unmarshal")
}
for i := range eips {
eips[i].region = self
}
return eips, nil
}
func (self *SRegion) GetEip(eipId string) (*SEip, error) {
params := map[string]string{
"regionId": self.GetId(),
"publicIpId": eipId,
}
eips := make([]SEip, 0)
resp, err := self.client.DoGet("/apiproxy/v3/ondemand/queryIps", params)
if err != nil {
return nil, errors.Wrap(err, "SRegion.GetEip.DoGet")
}
err = resp.Unmarshal(&eips, "returnObj", "publicips")
if err != nil {
return nil, errors.Wrap(err, "SRegion.GetEip.Unmarshal")
}
if len(eips) == 0 {
return nil, errors.Wrap(cloudprovider.ErrNotFound, "SRegion.GetEip")
} else if len(eips) == 1 {
eips[0].region = self
return &eips[0], nil
} else {
return nil, errors.Wrap(cloudprovider.ErrDuplicateId, "SRegion.GetEip")
}
}
func (self *SRegion) CreateEip(zoneId, name, size, shareType, chargeType string) (*SEip, error) {
eipParams := jsonutils.NewDict()
eipParams.Set("regionId", jsonutils.NewString(self.GetId()))
eipParams.Set("zoneId", jsonutils.NewString(zoneId))
eipParams.Set("name", jsonutils.NewString(name))
eipParams.Set("type", jsonutils.NewString("5_telcom"))
eipParams.Set("size", jsonutils.NewString(size))
eipParams.Set("shareType", jsonutils.NewString(shareType))
eipParams.Set("chargeMode", jsonutils.NewString(chargeType))
params := map[string]jsonutils.JSONObject{
"createIpInfo": eipParams,
}
resp, err := self.client.DoPost("/apiproxy/v3/ondemand/createIp", params)
if err != nil {
return nil, errors.Wrap(err, "SRegion.CreateEip.DoPost")
}
eipId, err := resp.GetString("returnObj", "id")
if err != nil {
return nil, errors.Wrap(err, "SRegion.CreateEip.GetEipId")
}
return self.GetEip(eipId)
}
func (self *SRegion) DeleteEip(publicIpId string) error {
params := map[string]jsonutils.JSONObject{
"regionId": jsonutils.NewString(self.GetId()),
"publicIpId": jsonutils.NewString(publicIpId),
}
resp, err := self.client.DoPost("/apiproxy/v3/ondemand/deleteIp", params)
if err != nil {
return errors.Wrap(err, "SRegion.DeleteEip.DoPost")
}
var ok bool
err = resp.Unmarshal(&ok, "returnObj")
if !ok {
msg, _ := resp.GetString("message")
return errors.Wrap(fmt.Errorf(msg), "SRegion.DeleteEip.JobFailed")
}
return nil
}
// 这里networkCardId 实际指的是port id
func (self *SRegion) AssociateEip(publicIpId, networkCardId string) error {
params := map[string]jsonutils.JSONObject{
"regionId": jsonutils.NewString(self.GetId()),
"publicIpId": jsonutils.NewString(publicIpId),
"networkCardId": jsonutils.NewString(networkCardId),
}
_, err := self.client.DoPost("/apiproxy/v3/ondemand/bindIp", params)
if err != nil {
return errors.Wrap(err, "SRegion.AssociateEip.DoPost")
}
return nil
}
func (self *SRegion) DissociateEip(publicIpId string) error {
params := map[string]jsonutils.JSONObject{
"regionId": jsonutils.NewString(self.GetId()),
"publicIpId": jsonutils.NewString(publicIpId),
}
_, err := self.client.DoPost("/apiproxy/v3/ondemand/unbindIp", params)
if err != nil {
return errors.Wrap(err, "SRegion.DissociateEip.DoPost")
}
return nil
}
type SBandwidth struct {
PublicipInfo []PublicipInfo `json:"publicip_info"`
EnterpriseProjectID string `json:"enterprise_project_id"`
Name string `json:"name"`
ID string `json:"id"`
ShareType string `json:"share_type"`
Size int64 `json:"size"`
TenantID string `json:"tenant_id"`
ChargeMode string `json:"charge_mode"`
BandwidthType string `json:"bandwidth_type"`
}
type PublicipInfo struct {
PublicipType string `json:"publicip_type"`
PublicipAddress string `json:"publicip_address"`
IPVersion int64 `json:"ip_version"`
PublicipID string `json:"publicip_id"`
}
func (self *SRegion) ChangeBandwidthEip(name, publicIpId, sizeMb string) error {
params := map[string]jsonutils.JSONObject{
"regionId": jsonutils.NewString(self.GetId()),
"publicIpId": jsonutils.NewString(publicIpId),
"name": jsonutils.NewString(name),
"size": jsonutils.NewString(sizeMb),
}
resp, err := self.client.DoPost("/apiproxy/v3/ondemand/upgradeNetwork", params)
if err != nil {
return errors.Wrap(err, "SRegion.ChangeBandwidthEip.DoPost")
}
bandwidth := SBandwidth{}
err = resp.Unmarshal(&bandwidth, "returnObj")
if err != nil {
return errors.Wrap(err, "SRegion.ChangeBandwidthEip.Unmarshal")
}
return nil
}