fix: SyncEip时同步项目应该在同步主机信息之前

This commit is contained in:
Qiu Jian
2019-12-26 00:58:50 +08:00
parent 4d73c7c675
commit 638e75fb2e
2 changed files with 23 additions and 9 deletions
+21 -7
View File
@@ -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
+2 -2
View File
@@ -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 {