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 {