forked from yunionio/cloudmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eip.go
289 lines (244 loc) · 7.86 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
// 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 ucloud
import (
"fmt"
"strings"
"time"
"github.com/nyl1001/pkg/jsonutils"
"yunion.io/x/log"
billing_api "github.com/nyl1001/cloudmux/pkg/apis/billing"
api "github.com/nyl1001/cloudmux/pkg/apis/compute"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
"github.com/nyl1001/cloudmux/pkg/multicloud"
)
const (
EIP_CHARGE_TYPE_BY_TRAFFIC = "traffic"
EIP_CHARGE_TYPE_BY_BANDWIDTH = "bandwidth"
)
// https://docs.ucloud.cn/api/unet-api/describe_eip
type SEip struct {
region *SRegion
multicloud.SEipBase
UcloudTags
BandwidthMb int `json:"Bandwidth"`
BandwidthType int `json:"BandwidthType"`
ChargeType string `json:"ChargeType"`
CreateTime int64 `json:"CreateTime"`
EIPAddr []EIPAddr `json:"EIPAddr"`
EIPID string `json:"EIPId"`
Expire bool `json:"Expire"`
ExpireTime int64 `json:"ExpireTime"`
Name string `json:"Name"`
PayMode string `json:"PayMode"`
Remark string `json:"Remark"`
Resource Resource `json:"Resource"`
ShareBandwidthSet ShareBandwidthSet `json:"ShareBandwidthSet"`
Status string `json:"Status"`
Tag string `json:"Tag"`
Weight int `json:"Weight"`
}
func (self *SEip) GetProjectId() string {
return self.region.client.projectId
}
type EIPAddr struct {
IP string `json:"IP"`
OperatorName string `json:"OperatorName"`
}
type Resource struct {
ResourceID string `json:"ResourceID"`
ResourceName string `json:"ResourceName"`
ResourceType string `json:"ResourceType"`
Zone string `json:"Zone"`
}
type ShareBandwidthSet struct {
ShareBandwidth int `json:"ShareBandwidth"`
ShareBandwidthID string `json:"ShareBandwidthId"`
ShareBandwidthName string `json:"ShareBandwidthName"`
}
func (self *SEip) GetId() string {
return self.EIPID
}
func (self *SEip) GetName() string {
if len(self.Name) == 0 {
return self.GetId()
}
return self.Name
}
func (self *SEip) GetGlobalId() string {
return self.GetId()
}
// 弹性IP的资源绑定状态, 枚举值为: used: 已绑定, free: 未绑定, freeze: 已冻结
func (self *SEip) GetStatus() string {
switch self.Status {
case "used":
return api.EIP_STATUS_ASSOCIATE // ?
case "free":
return api.EIP_STATUS_READY
case "freeze":
return api.EIP_STATUS_UNKNOWN
default:
return api.EIP_STATUS_UNKNOWN
}
}
func (self *SEip) Refresh() error {
if self.IsEmulated() {
return nil
}
new, err := self.region.GetEipById(self.GetId())
if err != nil {
return err
}
return jsonutils.Update(self, new)
}
func (self *SEip) IsEmulated() bool {
return false
}
// 付费方式, 枚举值为: Year, 按年付费; Month, 按月付费; Dynamic, 按小时付费; Trial, 试用. 按小时付费和试用这两种付费模式需要开通权限.
func (self *SEip) GetBillingType() string {
switch self.ChargeType {
case "Year", "Month":
return billing_api.BILLING_TYPE_PREPAID
default:
return billing_api.BILLING_TYPE_POSTPAID
}
}
func (self *SEip) GetCreatedAt() time.Time {
return time.Unix(self.CreateTime, 0)
}
func (self *SEip) GetExpiredAt() time.Time {
return time.Unix(self.ExpireTime, 0)
}
func (self *SEip) GetIpAddr() string {
if len(self.EIPAddr) > 1 {
log.Warningf("GetIpAddr %d eip addr found", len(self.EIPAddr))
} else if len(self.EIPAddr) == 0 {
return ""
}
return self.EIPAddr[0].IP
}
func (self *SEip) GetMode() string {
return api.EIP_MODE_STANDALONE_EIP
}
func (self *SEip) GetAssociationType() string {
switch self.Resource.ResourceType {
case "uhost":
return api.EIP_ASSOCIATE_TYPE_SERVER
case "natgw":
return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
case "ulb":
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
default:
return self.Resource.ResourceType
}
}
// 已绑定的资源类型, 枚举值为: uhost, 云主机;natgw:NAT网关;ulb:负载均衡器;upm: 物理机; hadoophost: 大数据集群;fortresshost:堡垒机;udockhost:容器;udhost:私有专区主机;vpngw:IPSec VPN;ucdr:云灾备;dbaudit:数据库审计。
func (self *SEip) GetAssociationExternalId() string {
return self.Resource.ResourceID
}
func (self *SEip) GetBandwidth() int {
return self.BandwidthMb
}
func (self *SEip) GetINetworkId() string {
return ""
}
// 弹性IP的计费模式, 枚举值为: "Bandwidth", 带宽计费; "Traffic", 流量计费; "ShareBandwidth",共享带宽模式. 默认为 "Bandwidth".
func (self *SEip) GetInternetChargeType() string {
switch self.PayMode {
case "Bandwidth":
return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
case "Traffic":
return api.EIP_CHARGE_TYPE_BY_TRAFFIC
default:
return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
}
}
func (self *SEip) Delete() error {
return self.region.DeallocateEIP(self.GetId())
}
func (self *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
return self.region.AssociateEip(self.GetId(), conf.InstanceId)
}
func (self *SEip) Dissociate() error {
return self.region.DissociateEip(self.GetId(), self.Resource.ResourceID)
}
func (self *SEip) ChangeBandwidth(bw int) error {
return self.region.UpdateEipBandwidth(self.GetId(), bw)
}
// https://docs.ucloud.cn/api/unet-api/allocate_eip
// 增加共享带宽模式ShareBandwidth
func (self *SRegion) CreateEIP(eip *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
if len(eip.BGPType) == 0 {
if strings.HasPrefix(self.GetId(), "cn-") {
eip.BGPType = "Bgp"
} else {
eip.BGPType = "International"
}
}
params := NewUcloudParams()
params.Set("OperatorName", eip.BGPType)
params.Set("Bandwidth", eip.BandwidthMbps)
params.Set("Name", eip.Name)
var payMode string
switch eip.ChargeType {
case api.EIP_CHARGE_TYPE_BY_TRAFFIC:
payMode = "Traffic"
case api.EIP_CHARGE_TYPE_BY_BANDWIDTH:
payMode = "Bandwidth"
}
params.Set("PayMode", payMode)
params.Set("ChargeType", "Dynamic") // 按需付费
eips := make([]SEip, 0)
err := self.DoAction("AllocateEIP", params, &eips)
if err != nil {
return nil, err
}
if len(eips) == 1 {
eip := eips[0]
eip.region = self
eip.Refresh()
return &eip, nil
} else {
return nil, fmt.Errorf("CreateEIP %d eip created", len(eips))
}
}
// https://docs.ucloud.cn/api/unet-api/release_eip
func (self *SRegion) DeallocateEIP(eipId string) error {
params := NewUcloudParams()
params.Set("EIPId", eipId)
return self.DoAction("ReleaseEIP", params, nil)
}
// https://docs.ucloud.cn/api/unet-api/bind_eip
func (self *SRegion) AssociateEip(eipId string, instanceId string) error {
params := NewUcloudParams()
params.Set("EIPId", eipId)
params.Set("ResourceType", "uhost")
params.Set("ResourceId", instanceId)
return self.DoAction("BindEIP", params, nil)
}
// https://docs.ucloud.cn/api/unet-api/unbind_eip
func (self *SRegion) DissociateEip(eipId string, instanceId string) error {
params := NewUcloudParams()
params.Set("EIPId", eipId)
params.Set("ResourceType", "uhost")
params.Set("ResourceId", instanceId)
return self.DoAction("UnBindEIP", params, nil)
}
// https://docs.ucloud.cn/api/unet-api/modify_eip_bandwidth
func (self *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
params := NewUcloudParams()
params.Set("EIPId", eipId)
params.Set("Bandwidth", bw)
return self.DoAction("ModifyEIPBandwidth", params, nil)
}