From 1287af044f67cb93b6b67072eba0fa6eea31e005 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Wed, 16 Jan 2019 00:48:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=9A=E5=BD=93eip?= =?UTF-8?q?=E5=85=B3=E8=81=94=E4=B8=BB=E6=9C=BA=E6=97=A0=E6=95=88=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=85=81=E8=AE=B8=E5=88=A0=E9=99=A4=E8=AF=A5eip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/elasticips.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 183b96b240..0dddf07237 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/tristate" @@ -351,6 +352,16 @@ func (manager *SElasticipManager) getEipForInstance(instanceType string, instanc return &eip, nil } +func (self *SElasticip) IsAssociated() bool { + if len(self.AssociateId) == 0 { + return false + } + if self.GetAssociateVM() != nil { + return true + } + return false +} + func (self *SElasticip) GetAssociateVM() *SGuest { if self.AssociateType == "server" && len(self.AssociateId) > 0 { return GuestManager.FetchGuestById(self.AssociateId) @@ -386,6 +397,9 @@ func (self *SElasticip) Dissociate(ctx context.Context, userCred mcclient.TokenC } func (self *SElasticip) AssociateVM(ctx context.Context, userCred mcclient.TokenCredential, vm *SGuest) error { + if vm.PendingDeleted || vm.Deleted { + return fmt.Errorf("vm is deleted") + } if len(self.AssociateType) > 0 { return fmt.Errorf("EIP has been associated!!") } @@ -505,7 +519,7 @@ func (self *SElasticip) CustomizeDelete(ctx context.Context, userCred mcclient.T } func (self *SElasticip) ValidateDeleteCondition(ctx context.Context) error { - if len(self.AssociateId) > 0 { + if self.IsAssociated() { return fmt.Errorf("eip is associated with instance") } return self.SVirtualResourceBase.ValidateDeleteCondition(ctx) @@ -527,7 +541,7 @@ func (self *SElasticip) AllowPerformAssociate(ctx context.Context, userCred mccl } func (self *SElasticip) PerformAssociate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - if len(self.AssociateId) > 0 { + if self.IsAssociated() { return nil, httperrors.NewConflictError("eip has been associated with instance") } @@ -627,7 +641,7 @@ func (self *SElasticip) AllowPerformDissociate(ctx context.Context, userCred mcc } func (self *SElasticip) PerformDissociate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - if len(self.AssociateId) == 0 { + if !self.IsAssociated() { return nil, httperrors.NewConflictError("eip is not associated with instance") }