yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/natdtable.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package hcs 16 17 import ( 18 "net/url" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/pkg/errors" 22 23 "yunion.io/x/cloudmux/pkg/cloudprovider" 24 "yunion.io/x/cloudmux/pkg/multicloud" 25 ) 26 27 type SNatDEntry struct { 28 multicloud.SResourceBase 29 HcsTags 30 gateway *SNatGateway 31 32 Id string `json:"id"` 33 NatGatewayId string `json:"nat_gateway_id"` 34 Protocol string `json:"protocol"` 35 Status string `json:"status"` 36 ExternalIP string `json:"floating_ip_address"` 37 ExternalPort int `json:"external_service_port"` 38 InternalIP string `json:"private_ip"` 39 InternalPort int `json:"internal_service_port"` 40 PortId string `json:"port_id"` 41 AdminStateUp bool `json:"admin_state_up"` 42 } 43 44 func (nat *SNatDEntry) GetId() string { 45 return nat.Id 46 } 47 48 func (nat *SNatDEntry) GetName() string { 49 // No name so return id 50 return nat.GetId() 51 } 52 53 func (nat *SNatDEntry) GetGlobalId() string { 54 return nat.GetId() 55 } 56 57 func (nat *SNatDEntry) GetStatus() string { 58 return NatResouceStatusTransfer(nat.Status) 59 } 60 61 func (nat *SNatDEntry) GetIpProtocol() string { 62 return nat.Protocol 63 } 64 65 func (nat *SNatDEntry) GetExternalIp() string { 66 return nat.ExternalIP 67 } 68 69 func (nat *SNatDEntry) GetExternalPort() int { 70 return nat.ExternalPort 71 } 72 73 func (nat *SNatDEntry) GetInternalIp() string { 74 return nat.InternalIP 75 } 76 77 func (nat *SNatDEntry) GetInternalPort() int { 78 return nat.InternalPort 79 } 80 81 func (nat *SNatDEntry) Delete() error { 82 return nat.gateway.region.DeleteNatDEntry(nat.GetId()) 83 } 84 85 // getNatSTable return all snat rules of gateway 86 func (gateway *SNatGateway) getNatDTable() ([]SNatDEntry, error) { 87 ret, err := gateway.region.GetNatDTable(gateway.GetId()) 88 if err != nil { 89 return nil, err 90 } 91 for i := range ret { 92 ret[i].gateway = gateway 93 } 94 return ret, nil 95 } 96 97 func (self *SRegion) GetNatDTable(natGatewayId string) ([]SNatDEntry, error) { 98 query := url.Values{} 99 query.Set("nat_gateway_id", natGatewayId) 100 ret := []SNatDEntry{} 101 err := self.list("nat", "v2.0", "dnat_rules", query, &ret) 102 if err != nil { 103 return nil, err 104 } 105 for i := range ret { 106 nat := &ret[i] 107 if len(nat.InternalIP) == 0 { 108 port, err := self.GetPort(nat.PortId) 109 if err != nil { 110 return nil, errors.Wrapf(err, `get port info for transfer to ip of port_id %q error`, nat.PortId) 111 } 112 nat.InternalIP = port.FixedIps[0].IpAddress 113 } 114 } 115 return ret, nil 116 } 117 118 func (self *SRegion) DeleteNatDEntry(entryId string) error { 119 return self.delete("nat", "v2.0", "dnat_rules/"+entryId) 120 } 121 122 func (nat *SNatDEntry) Refresh() error { 123 ret, err := nat.gateway.region.GetNatDEntry(nat.Id) 124 if err != nil { 125 return err 126 } 127 return jsonutils.Update(nat, ret) 128 } 129 130 func (self *SRegion) CreateNatDEntry(rule cloudprovider.SNatDRule, gatewayId string) (*SNatDEntry, error) { 131 params := make(map[string]interface{}) 132 params["nat_gateway_id"] = gatewayId 133 params["private_ip"] = rule.InternalIP 134 params["internal_service_port"] = rule.InternalPort 135 params["floating_ip_id"] = rule.ExternalIPID 136 params["external_service_port"] = rule.ExternalPort 137 params["protocol"] = rule.Protocol 138 139 packParams := map[string]interface{}{ 140 "dnat_rule": params, 141 } 142 ret := &SNatDEntry{} 143 return ret, self.create("nat", "v2.0", "dnat_rules", packParams, ret) 144 } 145 146 func (self *SRegion) GetNatDEntry(id string) (*SNatDEntry, error) { 147 ret := &SNatDEntry{} 148 return ret, self.get("nat", "v2.0", "dnat_rules/"+id, ret) 149 }