fix(region): guest associate eip checking fix

This commit is contained in:
tangbin
2021-04-06 15:29:11 +08:00
parent e4a47a1e61
commit cd9a97aef5
2 changed files with 25 additions and 6 deletions
+3 -6
View File
@@ -2942,14 +2942,11 @@ func (self *SGuest) PerformAssociateEip(ctx context.Context, userCred mcclient.T
return nil, err
}
eip, err := self.GetEipOrPublicIp()
err = self.IsEipAssociable()
if err != nil {
log.Errorf("Fail to get Eip %s", err)
return nil, httperrors.NewGeneralError(err)
}
if eip != nil {
return nil, httperrors.NewInvalidStatusError("already associate with eip")
}
eipStr := input.EipId
if len(eipStr) == 0 {
return nil, httperrors.NewMissingParameterError("eip_id")
@@ -2963,7 +2960,7 @@ func (self *SGuest) PerformAssociateEip(ctx context.Context, userCred mcclient.T
}
}
eip = eipObj.(*SElasticip)
eip := eipObj.(*SElasticip)
eipRegion, err := eip.GetRegion()
if err != nil {
return nil, httperrors.NewGeneralError(errors.Wrapf(err, "eip.GetRegion"))
+22
View File
@@ -4882,6 +4882,28 @@ func (self *SGuest) isInReconcile(userCred mcclient.TokenCredential) bool {
return false
}
func (self *SGuest) IsEipAssociable() error {
var eip *SElasticip
var err error
switch self.Hypervisor {
case api.HYPERVISOR_AWS:
eip, err = self.GetElasticIp()
default:
eip, err = self.GetEipOrPublicIp()
}
if err != nil {
log.Errorf("Fail to get Eip %s", err)
return errors.Wrap(err, "IsEipAssociable")
}
if eip != nil {
return httperrors.NewInvalidStatusError("already associate with eip")
}
return nil
}
func (self *SGuest) GetEipOrPublicIp() (*SElasticip, error) {
return ElasticipManager.getEip(api.EIP_ASSOCIATE_TYPE_SERVER, self.Id, "")
}