diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index d608a3f5b8..d3151d153a 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -2974,16 +2974,7 @@ func (self *SGuest) SendMonitorCommand(ctx context.Context, userCred mcclient.To } func (self *SGuest) PerformAssociateEip(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ServerAssociateEipInput) (jsonutils.JSONObject, error) { - if !utils.IsInStringArray(self.Status, []string{api.VM_READY, api.VM_RUNNING}) { - return nil, httperrors.NewInvalidStatusError("cannot associate eip in status %s", self.Status) - } - - err := ValidateAssociateEip(self) - if err != nil { - return nil, err - } - - err = self.IsEipAssociable() + err := self.IsEipAssociable() if err != nil { return nil, httperrors.NewGeneralError(err) } @@ -3103,9 +3094,9 @@ func (self *SGuest) PerformCreateEip(ctx context.Context, userCred mcclient.Toke autoDellocate = (input.AutoDellocate != nil && *input.AutoDellocate) ) - err := ValidateAssociateEip(self) + err := self.IsEipAssociable() if err != nil { - return nil, err + return nil, httperrors.NewGeneralError(err) } if chargeType == "" { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index a8ce0dae7c..f8a4be24cf 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -5027,8 +5027,16 @@ func (self *SGuest) isInReconcile(userCred mcclient.TokenCredential) bool { } func (self *SGuest) IsEipAssociable() error { + if !utils.IsInStringArray(self.Status, []string{api.VM_READY, api.VM_RUNNING}) { + return errors.Wrapf(httperrors.ErrInvalidStatus, "cannot associate eip in status %s", self.Status) + } + + err := ValidateAssociateEip(self) + if err != nil { + return errors.Wrap(err, "ValidateAssociateEip") + } + var eip *SElasticip - var err error switch self.Hypervisor { case api.HYPERVISOR_AWS: eip, err = self.GetElasticIp()