Skip to content

Commit 7b0c9ce

Browse files
authored
Merge pull request #1435 from ioito/hotfix/qx-eip-relationship
fix(aliyun): eip relationship
2 parents 5660a2f + 8b2c083 commit 7b0c9ce

File tree

10 files changed

+104
-81
lines changed

10 files changed

+104
-81
lines changed

pkg/multicloud/aliyun/alb.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,26 @@ func (alb *SAlb) GetEgressMbps() int {
247247
}
248248

249249
func (alb *SAlb) GetIEIPs() ([]cloudprovider.ICloudEIP, error) {
250-
return []cloudprovider.ICloudEIP{}, nil
250+
ret := []cloudprovider.ICloudEIP{}
251+
info, err := alb.region.GetAlbDetail(alb.LoadBalancerId)
252+
if err != nil {
253+
return nil, err
254+
}
255+
for _, zone := range info.ZoneMappings {
256+
for _, addr := range zone.LoadBalancerAddresses {
257+
if len(addr.Address) > 0 {
258+
eips, err := alb.region.GetEips("", "", addr.Address)
259+
if err != nil {
260+
return nil, err
261+
}
262+
for i := range eips {
263+
eips[i].region = alb.region
264+
ret = append(ret, &eips[i])
265+
}
266+
}
267+
}
268+
}
269+
return ret, nil
251270
}
252271

253272
func (alb *SAlb) Start() error {

pkg/multicloud/aliyun/eip.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,10 @@ func (self *SEipAddress) ChangeBandwidth(bw int) error {
240240
return self.region.UpdateEipBandwidth(self.AllocationId, bw)
241241
}
242242

243-
func (region *SRegion) GetEips(eipId string, associatedId, addr string, offset int, limit int) ([]SEipAddress, int, error) {
244-
if limit > 50 || limit <= 0 {
245-
limit = 50
246-
}
247-
243+
func (region *SRegion) GetEips(eipId string, associatedId, addr string) ([]SEipAddress, error) {
248244
params := make(map[string]string)
249245
params["RegionId"] = region.RegionId
250-
params["PageSize"] = fmt.Sprintf("%d", limit)
251-
params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
246+
params["PageSize"] = "100"
252247
if len(addr) > 0 {
253248
params["EipAddress"] = addr
254249
}
@@ -266,27 +261,36 @@ func (region *SRegion) GetEips(eipId string, associatedId, addr string, offset i
266261
}
267262
}
268263

269-
body, err := region.vpcRequest("DescribeEipAddresses", params)
270-
if err != nil {
271-
log.Errorf("DescribeEipAddresses fail %s", err)
272-
return nil, 0, err
273-
}
274-
275-
eips := make([]SEipAddress, 0)
276-
err = body.Unmarshal(&eips, "EipAddresses", "EipAddress")
277-
if err != nil {
278-
log.Errorf("Unmarshal EipAddress details fail %s", err)
279-
return nil, 0, err
280-
}
281-
total, _ := body.Int("TotalCount")
282-
for i := 0; i < len(eips); i += 1 {
283-
eips[i].region = region
264+
pageNumber := 1
265+
ret := []SEipAddress{}
266+
for {
267+
params["PageNumber"] = fmt.Sprintf("%d", pageNumber)
268+
body, err := region.vpcRequest("DescribeEipAddresses", params)
269+
if err != nil {
270+
log.Errorf("DescribeEipAddresses fail %s", err)
271+
return nil, err
272+
}
273+
part := struct {
274+
EipAddresses struct {
275+
EipAddress []SEipAddress
276+
} `json:"EipAddresses"`
277+
TotalCount int `json:"TotalCount"`
278+
}{}
279+
err = body.Unmarshal(&part)
280+
if err != nil {
281+
return nil, errors.Wrapf(err, "Unmarshal EipAddress details")
282+
}
283+
ret = append(ret, part.EipAddresses.EipAddress...)
284+
if len(ret) >= part.TotalCount {
285+
break
286+
}
287+
pageNumber++
284288
}
285-
return eips, int(total), nil
289+
return ret, nil
286290
}
287291

288292
func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
289-
eips, _, err := region.GetEips(eipId, "", "", 0, 1)
293+
eips, err := region.GetEips(eipId, "", "")
290294
if err != nil {
291295
return nil, err
292296
}

pkg/multicloud/aliyun/loadbalancer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func (lb *SLoadbalancer) GetIEIPs() ([]cloudprovider.ICloudEIP, error) {
317317
}
318318
return []cloudprovider.ICloudEIP{&eip}, nil
319319
}
320-
eips, _, err := lb.region.GetEips("", lb.LoadBalancerId, "", 0, 1)
320+
eips, err := lb.region.GetEips("", lb.LoadBalancerId, "")
321321
if err != nil {
322322
return nil, errors.Wrapf(err, "lb.region.GetEips(%s)", lb.LoadBalancerId)
323323
}

pkg/multicloud/aliyun/natgateway.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,16 @@ func (nat *SNatGateway) GetExpiredAt() time.Time {
155155
}
156156

157157
func (nat *SNatGateway) GetIEips() ([]cloudprovider.ICloudEIP, error) {
158-
eips := []SEipAddress{}
159-
for {
160-
parts, total, err := nat.vpc.region.GetEips("", nat.NatGatewayId, "", len(eips), 50)
161-
if err != nil {
162-
return nil, err
163-
}
164-
eips = append(eips, parts...)
165-
if len(eips) >= total {
166-
break
167-
}
158+
eips, err := nat.vpc.region.GetEips("", nat.NatGatewayId, "")
159+
if err != nil {
160+
return nil, err
168161
}
169-
ieips := []cloudprovider.ICloudEIP{}
170-
for i := 0; i < len(eips); i++ {
162+
ret := []cloudprovider.ICloudEIP{}
163+
for i := range eips {
171164
eips[i].region = nat.vpc.region
172-
ieips = append(ieips, &eips[i])
165+
ret = append(ret, &eips[i])
173166
}
174-
return ieips, nil
167+
return ret, nil
175168
}
176169

177170
func (nat *SNatGateway) GetINatDTable() ([]cloudprovider.ICloudNatDEntry, error) {

pkg/multicloud/aliyun/nlb.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package aliyun
1616

1717
import (
1818
"context"
19+
"strings"
1920
"time"
2021

2122
"yunion.io/x/jsonutils"
@@ -251,7 +252,20 @@ func (nlb *SNlb) GetEgressMbps() int {
251252
}
252253

253254
func (nlb *SNlb) GetIEIPs() ([]cloudprovider.ICloudEIP, error) {
254-
return []cloudprovider.ICloudEIP{}, nil
255+
ret := []cloudprovider.ICloudEIP{}
256+
for _, zone := range nlb.ZoneMappings {
257+
for _, addr := range zone.LoadBalancerAddresses {
258+
if strings.HasPrefix(addr.AllocationId, "eip-") {
259+
eip, err := nlb.region.GetEip(addr.AllocationId)
260+
if err != nil {
261+
return nil, err
262+
}
263+
eip.region = nlb.region
264+
ret = append(ret, eip)
265+
}
266+
}
267+
}
268+
return ret, nil
255269
}
256270

257271
func (nlb *SNlb) Start() error {

pkg/multicloud/aliyun/region.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -871,37 +871,24 @@ func (self *SRegion) UpdateInstancePassword(instId string, passwd string) error
871871
}
872872

873873
func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
874-
eips, total, err := self.GetEips("", "", "", 0, 50)
874+
eips, err := self.GetEips("", "", "")
875875
if err != nil {
876876
return nil, err
877877
}
878-
for len(eips) < total {
879-
var parts []SEipAddress
880-
parts, total, err = self.GetEips("", "", "", len(eips), 50)
881-
if err != nil {
882-
return nil, err
883-
}
884-
eips = append(eips, parts...)
885-
}
886878
ret := make([]cloudprovider.ICloudEIP, len(eips))
887-
for i := 0; i < len(eips); i += 1 {
879+
for i := range eips {
880+
eips[i].region = self
888881
ret[i] = &eips[i]
889882
}
890883
return ret, nil
891884
}
892885

893886
func (self *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) {
894-
eips, total, err := self.GetEips(eipId, "", "", 0, 1)
887+
eip, err := self.GetEip(eipId)
895888
if err != nil {
896889
return nil, err
897890
}
898-
if total == 0 {
899-
return nil, cloudprovider.ErrNotFound
900-
}
901-
if total > 1 {
902-
return nil, cloudprovider.ErrDuplicateId
903-
}
904-
return &eips[0], nil
891+
return eip, nil
905892
}
906893

907894
func (region *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
@@ -955,6 +942,11 @@ func (region *SRegion) GetILoadBalancers() ([]cloudprovider.ICloudLoadbalancer,
955942
}
956943

957944
func (region *SRegion) GetILoadBalancerById(loadbalancerId string) (cloudprovider.ICloudLoadbalancer, error) {
945+
if strings.HasPrefix(loadbalancerId, "alb-") {
946+
return region.GetAlbDetail(loadbalancerId)
947+
} else if strings.HasPrefix(loadbalancerId, "nlb-") {
948+
return region.GetNlbDetail(loadbalancerId)
949+
}
958950
return region.GetLoadbalancerDetail(loadbalancerId)
959951
}
960952

pkg/multicloud/aliyun/shell/eip.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ func init() {
2626
Id string `help:"Eip id"`
2727
AssociateId string `help:"Id of associate resource"`
2828
Addr string `help:"Eip "`
29-
Offset int `help:"List offset"`
30-
Limit int `help:"List limit"`
3129
}
3230
shellutils.R(&EipListOptions{}, "eip-list", "List eips", func(cli *aliyun.SRegion, args *EipListOptions) error {
33-
eips, total, e := cli.GetEips(args.Id, args.AssociateId, args.Addr, args.Offset, args.Limit)
31+
eips, e := cli.GetEips(args.Id, args.AssociateId, args.Addr)
3432
if e != nil {
3533
return e
3634
}
37-
printList(eips, total, args.Offset, args.Limit, []string{})
35+
printList(eips, 0, 0, 0, []string{})
3836
return nil
3937
})
4038

pkg/multicloud/aws/natgateway.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,15 @@ func (self *SNatGateway) Refresh() error {
113113
}
114114

115115
func (self *SNatGateway) GetIEips() ([]cloudprovider.ICloudEIP, error) {
116-
eips, err := self.region.GetEips("", "", self.NatGatewayId)
117-
if err != nil {
118-
return nil, errors.Wrapf(err, "GetEIPs")
119-
}
120116
ret := []cloudprovider.ICloudEIP{}
121-
for i := range eips {
122-
eips[i].region = self.region
123-
ret = append(ret, &eips[i])
117+
for _, addr := range self.NatGatewayAddresses {
118+
if len(addr.PublicIp) > 0 {
119+
eip, err := self.region.GetEipByIpAddress(addr.PublicIp)
120+
if err != nil {
121+
return nil, errors.Wrapf(err, "GetEipByIpAddress")
122+
}
123+
ret = append(ret, eip)
124+
}
124125
}
125126
return ret, nil
126127
}

pkg/multicloud/google/eip.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ func (addr *SAddress) GetAssociationType() string {
135135
if strings.Contains(user, "/forwardingRules/") {
136136
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
137137
}
138+
if strings.Contains(user, "/routers/") {
139+
return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
140+
}
138141
}
139142
return ""
140143
}

pkg/multicloud/qcloud/loadbalancer.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,16 @@ func (self *SLoadbalancer) GetILoadBalancerBackendGroups() ([]cloudprovider.IClo
322322
}
323323

324324
func (self *SLoadbalancer) GetIEIPs() ([]cloudprovider.ICloudEIP, error) {
325-
if self.LoadBalancerType == "OPEN" && len(self.LoadBalancerVips) > 0 {
326-
return []cloudprovider.ICloudEIP{&SEipAddress{
327-
region: self.region,
328-
AddressId: self.LoadBalancerId,
329-
AddressIp: self.LoadBalancerVips[0],
330-
AddressType: EIP_STATUS_BIND,
331-
InstanceId: self.LoadBalancerId,
332-
CreatedTime: self.CreateTime,
333-
}}, nil
334-
}
335-
return nil, nil
325+
eips, _, err := self.region.GetEips("", self.LoadBalancerId, 0, 50)
326+
if err != nil {
327+
return nil, errors.Wrapf(err, "GetEips")
328+
}
329+
ret := []cloudprovider.ICloudEIP{}
330+
for i := range eips {
331+
eips[i].region = self.region
332+
ret = append(ret, &eips[i])
333+
}
334+
return ret, nil
336335
}
337336

338337
func (self *SRegion) GetLoadbalancers(ids []string, limit, offset int) ([]SLoadbalancer, int, error) {

0 commit comments

Comments
 (0)