forked from yunionio/cloudmux
/
natgateway.go
354 lines (312 loc) · 9.8 KB
/
natgateway.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
// 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 hcso
import (
"strings"
"time"
"github.com/nyl1001/pkg/errors"
"github.com/nyl1001/pkg/jsonutils"
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"
"github.com/nyl1001/cloudmux/pkg/multicloud/huawei"
)
type SNatGateway struct {
multicloud.SNatGatewayBase
huawei.HuaweiTags
region *SRegion
ID string
Name string
Description string
Spec string
Status string
InternalNetworkId string
CreatedTime string `json:"created_at"`
}
func (gateway *SNatGateway) GetId() string {
return gateway.ID
}
func (gateway *SNatGateway) GetName() string {
return gateway.Name
}
func (gateway *SNatGateway) GetGlobalId() string {
return gateway.GetId()
}
func (gateway *SNatGateway) GetStatus() string {
return NatResouceStatusTransfer(gateway.Status)
}
func (self *SNatGateway) Delete() error {
return self.region.DeleteNatGateway(self.ID)
}
func (self *SNatGateway) Refresh() error {
nat, err := self.region.GetNatGateway(self.ID)
if err != nil {
return errors.Wrapf(err, "GetNatGateway(%s)", self.ID)
}
return jsonutils.Update(self, nat)
}
func (self *SNatGateway) GetINetworkId() string {
return self.InternalNetworkId
}
func (gateway *SNatGateway) GetNatSpec() string {
switch gateway.Spec {
case "1":
return api.NAT_SPEC_SMALL
case "2":
return api.NAT_SPEC_MIDDLE
case "3":
return api.NAT_SPEC_LARGE
case "4":
return api.NAT_SPEC_XLARGE
}
return gateway.Spec
}
func (gateway *SNatGateway) GetDescription() string {
return gateway.Description
}
func (gateway *SNatGateway) GetBillingType() string {
// Up to 2019.07.17, only support post pay
return billing_api.BILLING_TYPE_POSTPAID
}
func (gateway *SNatGateway) GetCreatedAt() time.Time {
t, _ := time.Parse("2006-01-02 15:04:05.000000", gateway.CreatedTime)
return t
}
func (gateway *SNatGateway) GetExpiredAt() time.Time {
// no support for expired time
return time.Time{}
}
func (gateway *SNatGateway) GetIEips() ([]cloudprovider.ICloudEIP, error) {
IEips, err := gateway.region.GetIEips()
if err != nil {
return nil, errors.Wrapf(err, `get all Eips of region %q error`, gateway.region.GetId())
}
dNatTables, err := gateway.GetINatDTable()
if err != nil {
return nil, errors.Wrapf(err, `get all DNatTable of gateway %q error`, gateway.GetId())
}
sNatTables, err := gateway.GetINatSTable()
if err != nil {
return nil, errors.Wrapf(err, `get all SNatTable of gateway %q error`, gateway.GetId())
}
// Get natIPSet of nat rules
natIPSet := make(map[string]struct{})
for _, snat := range sNatTables {
natIPSet[snat.GetIP()] = struct{}{}
}
for _, dnat := range dNatTables {
natIPSet[dnat.GetExternalIp()] = struct{}{}
}
// Add Eip whose GetIpAddr() in natIPSet to ret
ret := make([]cloudprovider.ICloudEIP, 0, 2)
for i := range IEips {
if _, ok := natIPSet[IEips[i].GetIpAddr()]; ok {
ret = append(ret, IEips[i])
}
}
return ret, nil
}
func (gateway *SNatGateway) GetINatDTable() ([]cloudprovider.ICloudNatDEntry, error) {
dNatTable, err := gateway.getNatDTable()
if err != nil {
return nil, errors.Wrapf(err, `get dnat table of nat gateway %q`, gateway.GetId())
}
ret := make([]cloudprovider.ICloudNatDEntry, len(dNatTable))
for i := range dNatTable {
ret[i] = &dNatTable[i]
}
return ret, nil
}
func (gateway *SNatGateway) GetINatSTable() ([]cloudprovider.ICloudNatSEntry, error) {
sNatTable, err := gateway.getNatSTable()
if err != nil {
return nil, errors.Wrapf(err, `get dnat table of nat gateway %q`, gateway.GetId())
}
ret := make([]cloudprovider.ICloudNatSEntry, len(sNatTable))
for i := range sNatTable {
ret[i] = &sNatTable[i]
}
return ret, nil
}
func (gateway *SNatGateway) CreateINatDEntry(rule cloudprovider.SNatDRule) (cloudprovider.ICloudNatDEntry, error) {
dnat, err := gateway.region.CreateNatDEntry(rule, gateway.GetId())
if err != nil {
return nil, err
}
dnat.gateway = gateway
return &dnat, nil
}
func (gateway *SNatGateway) CreateINatSEntry(rule cloudprovider.SNatSRule) (cloudprovider.ICloudNatSEntry, error) {
snat, err := gateway.region.CreateNatSEntry(rule, gateway.GetId())
if err != nil {
return nil, err
}
snat.gateway = gateway
return &snat, nil
}
func (gateway *SNatGateway) GetINatDEntryByID(id string) (cloudprovider.ICloudNatDEntry, error) {
dnat, err := gateway.region.GetNatDEntryByID(id)
if err != nil {
return nil, err
}
dnat.gateway = gateway
return &dnat, nil
}
func (gateway *SNatGateway) GetINatSEntryByID(id string) (cloudprovider.ICloudNatSEntry, error) {
snat, err := gateway.region.GetNatSEntryByID(id)
if err != nil {
return nil, err
}
snat.gateway = gateway
return &snat, nil
}
func (region *SRegion) GetNatGateways(vpcID, natGatewayID string) ([]SNatGateway, error) {
queues := make(map[string]string)
if len(natGatewayID) != 0 {
queues["id"] = natGatewayID
}
if len(vpcID) != 0 {
queues["router_id"] = vpcID
}
natGateways := make([]SNatGateway, 0, 2)
err := doListAllWithMarker(region.ecsClient.NatGateways.List, queues, &natGateways)
if err != nil {
return nil, errors.Wrapf(err, "get nat gateways error by natgatewayid")
}
for i := range natGateways {
natGateways[i].region = region
}
return natGateways, nil
}
func (region *SRegion) CreateNatDEntry(rule cloudprovider.SNatDRule, gatewayID string) (SNatDEntry, error) {
params := make(map[string]interface{})
params["nat_gateway_id"] = gatewayID
params["private_ip"] = rule.InternalIP
params["internal_service_port"] = rule.InternalPort
params["floating_ip_id"] = rule.ExternalIPID
params["external_service_port"] = rule.ExternalPort
params["protocol"] = rule.Protocol
packParams := map[string]map[string]interface{}{
"dnat_rule": params,
}
ret := SNatDEntry{}
err := DoCreate(region.ecsClient.DNatRules.Create, jsonutils.Marshal(packParams), &ret)
if err != nil {
return SNatDEntry{}, errors.Wrapf(err, `create dnat rule of nat gateway %q failed`, gatewayID)
}
return ret, nil
}
func (region *SRegion) CreateNatSEntry(rule cloudprovider.SNatSRule, gatewayID string) (SNatSEntry, error) {
params := make(map[string]interface{})
params["nat_gateway_id"] = gatewayID
if len(rule.NetworkID) != 0 {
params["network_id"] = rule.NetworkID
}
if len(rule.SourceCIDR) != 0 {
params["cidr"] = rule.SourceCIDR
}
params["floating_ip_id"] = rule.ExternalIPID
packParams := map[string]map[string]interface{}{
"snat_rule": params,
}
ret := SNatSEntry{}
err := DoCreate(region.ecsClient.SNatRules.Create, jsonutils.Marshal(packParams), &ret)
if err != nil {
return SNatSEntry{}, errors.Wrapf(err, `create snat rule of nat gateway %q failed`, gatewayID)
}
return ret, nil
}
func (region *SRegion) GetNatDEntryByID(id string) (SNatDEntry, error) {
dnat := SNatDEntry{}
err := DoGet(region.ecsClient.DNatRules.Get, id, map[string]string{}, &dnat)
if err != nil {
return SNatDEntry{}, err
}
return dnat, nil
}
func (region *SRegion) GetNatSEntryByID(id string) (SNatSEntry, error) {
snat := SNatSEntry{}
err := DoGet(region.ecsClient.SNatRules.Get, id, map[string]string{}, &snat)
if err != nil {
return SNatSEntry{}, cloudprovider.ErrNotFound
}
return snat, nil
}
func NatResouceStatusTransfer(status string) string {
// In Huawei Cloud, there are isx resource status of Nat, "ACTIVE", "PENDING_CREATE",
// "PENDING_UPDATE", "PENDING_DELETE", "EIP_FREEZED", "INACTIVE".
switch status {
case "ACTIVE":
return api.NAT_STAUTS_AVAILABLE
case "PENDING_CREATE":
return api.NAT_STATUS_ALLOCATE
case "PENDING_UPDATE", "PENDING_DELETE":
return api.NAT_STATUS_DEPLOYING
default:
return api.NAT_STATUS_UNKNOWN
}
}
func (self *SRegion) GetNatGateway(id string) (*SNatGateway, error) {
resp, err := self.ecsClient.NatGateways.Get(id, nil)
if err != nil {
return nil, errors.Wrapf(err, "NatGateways.Get(%s)", id)
}
nat := &SNatGateway{region: self}
err = resp.Unmarshal(nat)
return nat, errors.Wrap(err, "resp.Unmarshal")
}
func (self *SVpc) CreateINatGateway(opts *cloudprovider.NatGatewayCreateOptions) (cloudprovider.ICloudNatGateway, error) {
nat, err := self.region.CreateNatGateway(opts)
if err != nil {
return nil, errors.Wrapf(err, "CreateNatGateway")
}
return nat, nil
}
func (self *SRegion) CreateNatGateway(opts *cloudprovider.NatGatewayCreateOptions) (*SNatGateway, error) {
spec := ""
switch strings.ToLower(opts.NatSpec) {
case api.NAT_SPEC_SMALL:
spec = "1"
case api.NAT_SPEC_MIDDLE:
spec = "2"
case api.NAT_SPEC_LARGE:
spec = "3"
case api.NAT_SPEC_XLARGE:
spec = "4"
}
params := jsonutils.Marshal(map[string]map[string]interface{}{
"nat_gateway": map[string]interface{}{
"name": opts.Name,
"description": opts.Desc,
"router_id": opts.VpcId,
"internal_network_id": opts.NetworkId,
"spec": spec,
},
})
resp, err := self.ecsClient.NatGateways.Create(params)
if err != nil {
return nil, errors.Wrap(err, "AsyncCreate")
}
nat := &SNatGateway{region: self}
err = resp.Unmarshal(nat)
if err != nil {
return nil, errors.Wrapf(err, "resp.Unmarshal")
}
return nat, nil
}
func (self *SRegion) DeleteNatGateway(id string) error {
_, err := self.ecsClient.NatGateways.Delete(id, nil)
return errors.Wrapf(err, "NatGateways.Delete(%s)", id)
}