yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/natgateway.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 apsara 16 17 import ( 18 "fmt" 19 "time" 20 21 "yunion.io/x/log" 22 "yunion.io/x/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 ) 28 29 type SBandwidthPackageIds struct { 30 BandwidthPackageId []string 31 } 32 33 type SForwardTableIds struct { 34 ForwardTableId []string 35 } 36 37 type SSnatTableIds struct { 38 SnatTableId []string 39 } 40 41 type SNatGetway struct { 42 multicloud.SNatGatewayBase 43 ApsaraTags 44 45 vpc *SVpc 46 47 BandwidthPackageIds SBandwidthPackageIds 48 BusinessStatus string 49 CreationTime time.Time 50 ExpiredTime time.Time 51 Description string 52 ForwardTableIds SForwardTableIds 53 SnatTableIds SSnatTableIds 54 InstanceChargeType TChargeType 55 Name string 56 NatGatewayId string 57 RegionId string 58 Spec string 59 Status string 60 VpcId string 61 } 62 63 func (nat *SNatGetway) GetId() string { 64 return nat.NatGatewayId 65 } 66 67 func (nat *SNatGetway) GetGlobalId() string { 68 return nat.NatGatewayId 69 } 70 71 func (nat *SNatGetway) GetName() string { 72 if len(nat.Name) > 0 { 73 return nat.Name 74 } 75 return nat.NatGatewayId 76 } 77 78 func (self *SNatGetway) GetINetworkId() string { 79 return "" 80 } 81 82 func (nat *SNatGetway) GetStatus() string { 83 switch nat.Status { 84 case "Initiating": 85 return api.NAT_STATUS_ALLOCATE 86 case "Available": 87 return api.NAT_STAUTS_AVAILABLE 88 case "Pending": 89 return api.NAT_STATUS_DEPLOYING 90 default: 91 return api.NAT_STATUS_UNKNOWN 92 } 93 94 } 95 96 func (nat *SNatGetway) GetBillingType() string { 97 return convertChargeType(nat.InstanceChargeType) 98 } 99 100 func (nat *SNatGetway) GetNatSpec() string { 101 return nat.Spec 102 } 103 104 func (nat *SNatGetway) GetCreatedAt() time.Time { 105 return nat.CreationTime 106 } 107 108 func (nat *SNatGetway) GetExpiredAt() time.Time { 109 return nat.ExpiredTime 110 } 111 112 func (nat *SNatGetway) GetIEips() ([]cloudprovider.ICloudEIP, error) { 113 eips := []SEipAddress{} 114 for { 115 parts, total, err := nat.vpc.region.GetEips("", nat.NatGatewayId, len(eips), 50) 116 if err != nil { 117 return nil, err 118 } 119 eips = append(eips, parts...) 120 if len(eips) >= total { 121 break 122 } 123 } 124 ieips := []cloudprovider.ICloudEIP{} 125 for i := 0; i < len(eips); i++ { 126 eips[i].region = nat.vpc.region 127 ieips = append(ieips, &eips[i]) 128 } 129 return ieips, nil 130 } 131 132 func (nat *SNatGetway) GetINatDTable() ([]cloudprovider.ICloudNatDEntry, error) { 133 itables := []cloudprovider.ICloudNatDEntry{} 134 for _, dtableId := range nat.ForwardTableIds.ForwardTableId { 135 dtables, err := nat.vpc.region.GetAllDTables(dtableId) 136 if err != nil { 137 return nil, err 138 } 139 for i := 0; i < len(dtables); i++ { 140 dtables[i].nat = nat 141 itables = append(itables, &dtables[i]) 142 } 143 } 144 return itables, nil 145 } 146 147 func (nat *SNatGetway) GetINatSTable() ([]cloudprovider.ICloudNatSEntry, error) { 148 stables, err := nat.getSnatEntries() 149 if err != nil { 150 return nil, err 151 } 152 itables := []cloudprovider.ICloudNatSEntry{} 153 for i := 0; i < len(stables); i++ { 154 stables[i].nat = nat 155 itables = append(itables, &stables[i]) 156 } 157 return itables, nil 158 } 159 160 func (nat *SNatGetway) GetINatDEntryByID(id string) (cloudprovider.ICloudNatDEntry, error) { 161 dNATEntry, err := nat.vpc.region.GetForwardTableEntry(nat.ForwardTableIds.ForwardTableId[0], id) 162 if err != nil { 163 return nil, cloudprovider.ErrNotFound 164 } 165 dNATEntry.nat = nat 166 return &dNATEntry, nil 167 } 168 169 func (nat *SNatGetway) GetINatSEntryByID(id string) (cloudprovider.ICloudNatSEntry, error) { 170 sNATEntry, err := nat.vpc.region.GetSNATEntry(nat.SnatTableIds.SnatTableId[0], id) 171 if err != nil { 172 return nil, cloudprovider.ErrNotFound 173 } 174 sNATEntry.nat = nat 175 return &sNATEntry, nil 176 } 177 178 func (nat *SNatGetway) CreateINatDEntry(rule cloudprovider.SNatDRule) (cloudprovider.ICloudNatDEntry, error) { 179 entryID, err := nat.vpc.region.CreateForwardTableEntry(rule, nat.ForwardTableIds.ForwardTableId[0]) 180 if err != nil { 181 return nil, errors.Wrapf(err, `create dnat rule for nat gateway %q`, nat.GetId()) 182 } 183 return nat.GetINatDEntryByID(entryID) 184 } 185 186 func (nat *SNatGetway) CreateINatSEntry(rule cloudprovider.SNatSRule) (cloudprovider.ICloudNatSEntry, error) { 187 entryID, err := nat.vpc.region.CreateSNATTableEntry(rule, nat.SnatTableIds.SnatTableId[0]) 188 if err != nil { 189 return nil, errors.Wrapf(err, `create snat rule for nat gateway %q`, nat.GetId()) 190 } 191 return nat.GetINatSEntryByID(entryID) 192 } 193 194 func (self *SRegion) GetNatGateways(vpcId string, natGwId string, offset, limit int) ([]SNatGetway, int, error) { 195 if limit > 50 || limit <= 0 { 196 limit = 50 197 } 198 params := make(map[string]string) 199 params["RegionId"] = self.RegionId 200 params["PageSize"] = fmt.Sprintf("%d", limit) 201 params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1) 202 if len(vpcId) > 0 { 203 params["VpcId"] = vpcId 204 } 205 if len(natGwId) > 0 { 206 params["NatGatewayId"] = natGwId 207 } 208 209 body, err := self.vpcRequest("DescribeNatGateways", params) 210 if err != nil { 211 log.Errorf("GetVSwitches fail %s", err) 212 return nil, 0, err 213 } 214 215 if self.client.debug { 216 log.Debugf("%s", body.PrettyString()) 217 } 218 219 gateways := make([]SNatGetway, 0) 220 err = body.Unmarshal(&gateways, "NatGateways", "NatGateway") 221 if err != nil { 222 log.Errorf("Unmarshal gateways fail %s", err) 223 return nil, 0, err 224 } 225 total, _ := body.Int("TotalCount") 226 return gateways, int(total), nil 227 }