forked from yunionio/cloudmux
/
natstable.go
121 lines (104 loc) · 3.09 KB
/
natstable.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
// 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 huawei
import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/cloudmux/pkg/multicloud"
)
type SNatSEntry struct {
multicloud.SResourceBase
HuaweiTags
gateway *SNatGateway
ID string `json:"id"`
NatGatewayID string `json:"nat_gateway_id"`
NetworkID string `json:"network_id"`
SourceCIDR string `json:"cidr"`
Status string `json:"status"`
SNatIP string `json:"floating_ip_address"`
AdminStateUp bool `json:"admin_state_up"`
}
func (nat *SNatSEntry) GetId() string {
return nat.ID
}
func (nat *SNatSEntry) GetName() string {
// Snat rule has no name in Huawei Cloud, so return ID
return nat.GetId()
}
func (nat *SNatSEntry) GetGlobalId() string {
return nat.GetId()
}
func (nat *SNatSEntry) GetStatus() string {
return NatResouceStatusTransfer(nat.Status)
}
func (nat *SNatSEntry) GetIP() string {
return nat.SNatIP
}
func (nat *SNatSEntry) GetSourceCIDR() string {
return nat.SourceCIDR
}
func (nat *SNatSEntry) GetNetworkId() string {
return nat.NetworkID
}
func (nat *SNatSEntry) Delete() error {
return nat.gateway.region.DeleteNatSEntry(nat.GetId())
}
// getNatSTable return all snat rules of gateway
func (gateway *SNatGateway) getNatSTable() ([]SNatSEntry, error) {
ret, err := gateway.region.GetNatSTable(gateway.GetId())
if err != nil {
return nil, err
}
for i := range ret {
ret[i].gateway = gateway
}
return ret, nil
}
func (region *SRegion) GetNatSTable(natGatewayID string) ([]SNatSEntry, error) {
queuies := map[string]string{
"nat_gateway_id": natGatewayID,
}
sNatSTableEntris := make([]SNatSEntry, 0, 2)
err := doListAllWithMarker(region.ecsClient.SNatRules.List, queuies, &sNatSTableEntris)
if err != nil {
return nil, errors.Wrapf(err, `get snat rule of gateway %q`, natGatewayID)
}
for i := range sNatSTableEntris {
nat := &sNatSTableEntris[i]
if len(nat.SourceCIDR) != 0 {
continue
}
subnet := SNetwork{}
err := DoGet(region.ecsClient.Subnets.Get, nat.NetworkID, map[string]string{}, &subnet)
if err != nil {
return nil, errors.Wrapf(err, `get cidr of subnet %q`, nat.NetworkID)
}
nat.SourceCIDR = subnet.CIDR
}
return sNatSTableEntris, nil
}
func (region *SRegion) DeleteNatSEntry(entryID string) error {
_, err := region.ecsClient.SNatRules.Delete(entryID, nil)
if err != nil {
return errors.Wrapf(err, `delete snat rule %q failed`, entryID)
}
return nil
}
func (nat *SNatSEntry) Refresh() error {
new, err := nat.gateway.region.GetNatSEntryByID(nat.ID)
if err != nil {
return err
}
return jsonutils.Update(nat, new)
}