yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/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 huawei 16 17 import ( 18 "yunion.io/x/jsonutils" 19 "yunion.io/x/pkg/errors" 20 21 "yunion.io/x/cloudmux/pkg/multicloud" 22 ) 23 24 type SNatDEntry struct { 25 multicloud.SResourceBase 26 HuaweiTags 27 gateway *SNatGateway 28 29 ID string `json:"id"` 30 NatGatewayID string `json:"nat_gateway_id"` 31 Protocol string `json:"protocol"` 32 Status string `json:"status"` 33 ExternalIP string `json:"floating_ip_address"` 34 ExternalPort int `json:"external_service_port"` 35 InternalIP string `json:"private_ip"` 36 InternalPort int `json:"internal_service_port"` 37 PortID string `json:"port_id"` 38 AdminStateUp bool `json:"admin_state_up"` 39 } 40 41 func (nat *SNatDEntry) GetId() string { 42 return nat.ID 43 } 44 45 func (nat *SNatDEntry) GetName() string { 46 // No name so return id 47 return nat.GetId() 48 } 49 50 func (nat *SNatDEntry) GetGlobalId() string { 51 return nat.GetId() 52 } 53 54 func (nat *SNatDEntry) GetStatus() string { 55 return NatResouceStatusTransfer(nat.Status) 56 } 57 58 func (nat *SNatDEntry) GetIpProtocol() string { 59 return nat.Protocol 60 } 61 62 func (nat *SNatDEntry) GetExternalIp() string { 63 return nat.ExternalIP 64 } 65 66 func (nat *SNatDEntry) GetExternalPort() int { 67 return nat.ExternalPort 68 } 69 70 func (nat *SNatDEntry) GetInternalIp() string { 71 return nat.InternalIP 72 } 73 74 func (nat *SNatDEntry) GetInternalPort() int { 75 return nat.InternalPort 76 } 77 78 func (nat *SNatDEntry) Delete() error { 79 return nat.gateway.region.DeleteNatDEntry(nat.GetId()) 80 } 81 82 // getNatSTable return all snat rules of gateway 83 func (gateway *SNatGateway) getNatDTable() ([]SNatDEntry, error) { 84 ret, err := gateway.region.GetNatDTable(gateway.GetId()) 85 if err != nil { 86 return nil, err 87 } 88 for i := range ret { 89 ret[i].gateway = gateway 90 } 91 return ret, nil 92 } 93 94 func (region *SRegion) GetNatDTable(natGatewayID string) ([]SNatDEntry, error) { 95 queuies := map[string]string{ 96 "nat_gateway_id": natGatewayID, 97 } 98 dNatSTableEntries := make([]SNatDEntry, 0, 2) 99 // can't make true that restapi support marker para in Huawei Cloud 100 err := doListAllWithMarker(region.ecsClient.DNatRules.List, queuies, &dNatSTableEntries) 101 if err != nil { 102 return nil, errors.Wrapf(err, `get dnat rule of gateway %q`, natGatewayID) 103 } 104 for i := range dNatSTableEntries { 105 nat := &dNatSTableEntries[i] 106 if len(nat.InternalIP) == 0 { 107 port, err := region.GetPort(nat.PortID) 108 if err != nil { 109 return nil, errors.Wrapf(err, `get port info for transfer to ip of port_id %q error`, nat.PortID) 110 } 111 nat.InternalIP = port.FixedIps[0].IpAddress 112 } 113 } 114 return dNatSTableEntries, nil 115 } 116 117 func (region *SRegion) DeleteNatDEntry(entryID string) error { 118 _, err := region.ecsClient.DNatRules.Delete(entryID, nil) 119 if err != nil { 120 return errors.Wrapf(err, `delete dnat rule %q failed`, entryID) 121 } 122 return nil 123 } 124 125 func (nat *SNatDEntry) Refresh() error { 126 new, err := nat.gateway.region.GetNatDEntryByID(nat.ID) 127 if err != nil { 128 return err 129 } 130 return jsonutils.Update(nat, new) 131 }