From c3f5e8289d028361e0577138bd34aeead25f8e46 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Tue, 4 Sep 2018 23:29:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=9A1.=20=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E7=BB=91=E5=AE=9A=E7=9A=84EIP=E7=A6=81=E6=AD=A2?= =?UTF-8?q?=E5=88=A0=E9=99=A4=202.=20=E5=88=A0=E9=99=A4server=E6=97=B6?= =?UTF-8?q?=E5=80=99=E6=B8=85=E9=99=A4public=20IP=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/elasticips.go | 7 +++++++ pkg/compute/models/guests.go | 17 +++++++++++++++++ pkg/compute/tasks/guest_delete_task.go | 2 ++ 3 files changed, 26 insertions(+) diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 887a2f68b3..cb60e4ca08 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -432,6 +432,13 @@ func (self *SElasticip) CustomizeDelete(ctx context.Context, userCred mcclient.T return self.StartEipDeallocateTask(ctx, userCred, "") } +func (self *SElasticip) ValidateDeleteCondition(ctx context.Context) error { + if len(self.AssociateId) > 0 { + return fmt.Errorf("eip is associated with instance") + } + return self.SVirtualResourceBase.ValidateDeleteCondition(ctx) +} + func (self *SElasticip) StartEipDeallocateTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error { task, err := taskman.TaskManager.NewTask(ctx, "EipDeallocateTask", self, userCred, nil, parentTaskId, "", nil) if err != nil { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 560c5e72eb..db3c42d039 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -3670,4 +3670,21 @@ func (self *SGuest) PerformCreateEip(ctx context.Context, userCred mcclient.Toke return nil, httperrors.NewGeneralError(err) } return nil, nil +} + +func (self *SGuest) DeleteEip(ctx context.Context, userCred mcclient.TokenCredential) error { + eip, err := self.GetEip() + if err != nil { + log.Errorf("Delete eip fail for get Eip %s", err) + return err + } + if eip == nil { + return nil + } + err = eip.Delete(ctx, userCred) + if err != nil { + log.Errorf("Delete eip fail %s", err) + return err + } + return nil } \ No newline at end of file diff --git a/pkg/compute/tasks/guest_delete_task.go b/pkg/compute/tasks/guest_delete_task.go index c702c1d7bc..bccc6bcca1 100644 --- a/pkg/compute/tasks/guest_delete_task.go +++ b/pkg/compute/tasks/guest_delete_task.go @@ -33,6 +33,7 @@ func (self *GuestDeleteTask) OnGuestStopComplete(ctx context.Context, obj db.ISt eip, _ := guest.GetEip() if eip != nil && eip.Mode != models.EIP_MODE_INSTANCE_PUBLICIP { + // detach floating EIP only self.SetStage("on_eip_dissociate_complete", nil) eip.StartEipDissociateTask(ctx, self.UserCred, self.GetTaskId()) } else { @@ -113,6 +114,7 @@ func (self *GuestDeleteTask) OnGuestDeleteComplete(ctx context.Context, obj db.I guest.LeaveAllGroups(self.UserCred) guest.DetachAllNetworks(ctx, self.UserCred) guest.EjectIso(self.UserCred) + guest.DeleteEip(ctx, self.UserCred) guest.GetDriver().OnDeleteGuestFinalCleanup(ctx, guest, self.UserCred) self.DeleteGuest(ctx, guest) }