From cd9a97aef53b47d092fbd56e66e28da75c6137a2 Mon Sep 17 00:00:00 2001 From: tangbin Date: Tue, 6 Apr 2021 15:29:11 +0800 Subject: [PATCH] fix(region): guest associate eip checking fix --- pkg/compute/models/guest_actions.go | 9 +++------ pkg/compute/models/guests.go | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index b91bde09f1..f4cb60a2a4 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -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")) diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index d9faef256b..813fa82521 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -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, "") }