From 638e75fb2e20d36a29d5e4e056ff0573d7c0161a Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Thu, 26 Dec 2019 00:58:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20SyncEip=E6=97=B6=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=BA=94=E8=AF=A5=E5=9C=A8=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=B8=BB=E6=9C=BA=E4=BF=A1=E6=81=AF=E4=B9=8B=E5=89=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/elasticips.go | 28 +++++++++++++++++++++------- pkg/compute/models/guests.go | 4 ++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 77ce8b22cb..800584f49c 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -423,11 +423,13 @@ func (manager *SElasticipManager) newFromCloudEip(ctx context.Context, userCred log.Errorf("newFromCloudEip fail %s", err) return nil, err } + + SyncCloudProject(userCred, &eip, syncOwnerId, extEip, eip.ManagerId) + err = eip.SyncInstanceWithCloudEip(ctx, userCred, extEip) if err != nil { return nil, errors.Wrap(err, "fail to sync associated instance of EIP") } - SyncCloudProject(userCred, &eip, syncOwnerId, extEip, eip.ManagerId) db.OpsLog.LogEvent(&eip, db.ACT_CREATE, eip.GetShortDesc(ctx), userCred) @@ -587,8 +589,12 @@ func (self *SElasticip) AssociateLoadbalancer(ctx context.Context, userCred mccl if lb.PendingDeleted { return fmt.Errorf("loadbalancer is deleted") } - if len(self.AssociateType) > 0 { - return fmt.Errorf("EIP has been associated!!") + if len(self.AssociateType) > 0 && len(self.AssociateId) > 0 { + if self.AssociateType == api.EIP_ASSOCIATE_TYPE_LOADBALANCER && self.AssociateId == lb.Id { + return nil + } else { + return fmt.Errorf("EIP has been associated!!") + } } _, err := db.Update(self, func() error { self.AssociateType = api.EIP_ASSOCIATE_TYPE_LOADBALANCER @@ -610,8 +616,12 @@ func (self *SElasticip) AssociateVM(ctx context.Context, userCred mcclient.Token if vm.PendingDeleted || vm.Deleted { return fmt.Errorf("vm is deleted") } - if len(self.AssociateType) > 0 { - return fmt.Errorf("EIP has been associated!!") + if len(self.AssociateType) > 0 && len(self.AssociateId) > 0 { + if self.AssociateType == api.EIP_ASSOCIATE_TYPE_SERVER && self.AssociateId == vm.Id { + return nil + } else { + return fmt.Errorf("EIP has been associated!!") + } } _, err := db.Update(self, func() error { self.AssociateType = api.EIP_ASSOCIATE_TYPE_SERVER @@ -633,8 +643,12 @@ func (self *SElasticip) AssociateNatGateway(ctx context.Context, userCred mcclie if nat.Deleted { return fmt.Errorf("nat gateway is deleted") } - if len(self.AssociateType) > 0 { - return fmt.Errorf("Eip has been associated!!") + if len(self.AssociateType) > 0 && len(self.AssociateId) > 0 { + if self.AssociateType == api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY && self.AssociateId == nat.Id { + return nil + } else { + return fmt.Errorf("Eip has been associated!!") + } } _, err := db.Update(self, func() error { self.AssociateType = api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index cdff29c226..1c14eb1f0b 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -4279,11 +4279,11 @@ func (manager *SGuestManager) DeleteExpiredPostpaidServers(ctx context.Context, } func (self *SGuest) GetEip() (*SElasticip, error) { - return ElasticipManager.getEipForInstance("server", self.Id) + return ElasticipManager.getEipForInstance(api.EIP_ASSOCIATE_TYPE_SERVER, self.Id) } func (self *SGuest) GetPublicIp() (*SElasticip, error) { - return ElasticipManager.getEip("server", self.Id, api.EIP_MODE_INSTANCE_PUBLICIP) + return ElasticipManager.getEip(api.EIP_ASSOCIATE_TYPE_SERVER, self.Id, api.EIP_MODE_INSTANCE_PUBLICIP) } func (self *SGuest) SyncVMEip(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, extEip cloudprovider.ICloudEIP, syncOwnerId mcclient.IIdentityProvider) compare.SyncResult {