instance_snapshot_delete_task.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package snapshot
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type InstanceSnapshotDeleteTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(InstanceSnapshotDeleteTask{})
  30. }
  31. func (self *InstanceSnapshotDeleteTask) taskFail(
  32. ctx context.Context, isp *models.SInstanceSnapshot, reason jsonutils.JSONObject) {
  33. isp.SetStatus(ctx, self.UserCred, compute.INSTANCE_SNAPSHOT_DELETE_FAILED, "on delete failed")
  34. db.OpsLog.LogEvent(isp, db.ACT_DELETE_FAIL, reason, self.UserCred)
  35. logclient.AddActionLogWithContext(ctx, isp, logclient.ACT_DELETE, reason, self.UserCred, false)
  36. self.SetStageFailed(ctx, reason)
  37. }
  38. func (self *InstanceSnapshotDeleteTask) taskComplete(
  39. ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  40. isp.RealDelete(ctx, self.UserCred)
  41. logclient.AddActionLogWithContext(ctx, isp, logclient.ACT_DELETE, nil, self.UserCred, true)
  42. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  43. Obj: isp,
  44. Action: notifyclient.ActionDelete,
  45. })
  46. self.SetStageComplete(ctx, nil)
  47. }
  48. func (self *InstanceSnapshotDeleteTask) OnInit(
  49. ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  50. isp := obj.(*models.SInstanceSnapshot)
  51. sps, err := isp.GetSnapshots()
  52. if err != nil {
  53. self.taskFail(ctx, isp, jsonutils.NewString(err.Error()))
  54. return
  55. }
  56. snapshotCnt := len(sps)
  57. self.Params.Set("snapshot_total_count", jsonutils.NewInt(int64(snapshotCnt)))
  58. self.SetStage("OnInstanceSnapshotDelete", nil)
  59. if err := isp.GetRegionDriver().RequestDeleteInstanceSnapshot(ctx, isp, self); err != nil {
  60. self.taskFail(ctx, isp, jsonutils.NewString(err.Error()))
  61. return
  62. }
  63. }
  64. func (self *InstanceSnapshotDeleteTask) OnKvmSnapshotDelete(
  65. ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  66. snapshotId, _ := self.Params.GetString("del_snapshot_id")
  67. // detach snapshot and instance
  68. isjp := new(models.SInstanceSnapshotJoint)
  69. err := models.InstanceSnapshotJointManager.Query().
  70. Equals("instance_snapshot_id", isp.Id).Equals("snapshot_id", snapshotId).First(isjp)
  71. if err != nil {
  72. self.taskFail(ctx, isp, jsonutils.NewString(err.Error()))
  73. return
  74. }
  75. isjp.SetModelManager(models.InstanceSnapshotJointManager, isjp)
  76. err = isjp.Delete(ctx, self.UserCred)
  77. if err != nil {
  78. self.taskFail(ctx, isp, jsonutils.NewString(err.Error()))
  79. return
  80. }
  81. if err := isp.GetRegionDriver().RequestDeleteInstanceSnapshot(ctx, isp, self); err != nil {
  82. self.taskFail(ctx, isp, jsonutils.NewString(err.Error()))
  83. return
  84. }
  85. }
  86. func (self *InstanceSnapshotDeleteTask) OnKvmSnapshotDeleteFailed(
  87. ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  88. self.taskFail(ctx, isp, data)
  89. }
  90. func (self *InstanceSnapshotDeleteTask) OnInstanceSnapshotDelete(ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  91. err := isp.Delete(ctx, self.UserCred)
  92. if err != nil {
  93. self.taskFail(ctx, isp, jsonutils.NewString(err.Error()))
  94. return
  95. }
  96. self.taskComplete(ctx, isp, data)
  97. }
  98. func (self *InstanceSnapshotDeleteTask) OnInstanceSnapshotDeleteFailed(ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  99. self.taskFail(ctx, isp, data)
  100. }