disk_backup_delete_task.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 backup
  15. import (
  16. "context"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type DiskBackupDeleteTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(DiskBackupDeleteTask{})
  31. }
  32. func (self *DiskBackupDeleteTask) taskFailed(ctx context.Context, backup *models.SDiskBackup, reason jsonutils.JSONObject) {
  33. reasonStr, _ := reason.GetString()
  34. backup.SetStatus(ctx, self.UserCred, api.BACKUP_STATUS_DELETE_FAILED, reasonStr)
  35. logclient.AddActionLogWithStartable(self, backup, logclient.ACT_DELETE, reason, self.UserCred, false)
  36. self.SetStageFailed(ctx, reason)
  37. }
  38. func (self *DiskBackupDeleteTask) taskSuccess(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
  39. backup.RealDelete(ctx, self.UserCred)
  40. self.SetStageComplete(ctx, nil)
  41. }
  42. func (self *DiskBackupDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  43. backup := obj.(*models.SDiskBackup)
  44. self.SetStage("OnDelete", nil)
  45. rd, err := backup.GetRegionDriver()
  46. if err != nil {
  47. self.taskFailed(ctx, backup, jsonutils.NewString(err.Error()))
  48. return
  49. }
  50. if err := rd.RequestDeleteBackup(ctx, backup, self); err != nil {
  51. self.taskFailed(ctx, backup, jsonutils.NewString(err.Error()))
  52. }
  53. }
  54. func (self *DiskBackupDeleteTask) OnDelete(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
  55. self.taskSuccess(ctx, backup, nil)
  56. }
  57. func (self *DiskBackupDeleteTask) OnDeleteFailed(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
  58. log.Infof("params: %s", self.Params)
  59. log.Infof("OnDeleteFailed data: %s", data)
  60. if forceDelete := jsonutils.QueryBoolean(self.Params, "force_delete", false); !forceDelete {
  61. self.taskFailed(ctx, backup, data)
  62. }
  63. reason, _ := data.GetString("__reason__")
  64. if !strings.Contains(reason, api.BackupStorageOffline) {
  65. self.taskFailed(ctx, backup, data)
  66. }
  67. log.Infof("delete backup %s failed, force delete", backup.GetId())
  68. self.taskSuccess(ctx, backup, nil)
  69. }