dbinstance_delete_task.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 dbinstance
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type DBInstanceDeleteTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(DBInstanceDeleteTask{})
  33. }
  34. func (self *DBInstanceDeleteTask) taskFailed(ctx context.Context, rds *models.SDBInstance, err error) {
  35. rds.SetStatus(ctx, self.UserCred, api.DBINSTANCE_DELETE_FAILED, err.Error())
  36. db.OpsLog.LogEvent(rds, db.ACT_DELETE, err, self.GetUserCred())
  37. logclient.AddActionLogWithStartable(self, rds, logclient.ACT_DELETE, err, self.UserCred, false)
  38. notifyclient.EventNotify(ctx, self.GetUserCred(), notifyclient.SEventNotifyParam{
  39. Obj: rds,
  40. Action: notifyclient.ActionDelete,
  41. IsFail: true,
  42. })
  43. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  44. }
  45. func (self *DBInstanceDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  46. rds := obj.(*models.SDBInstance)
  47. self.DeleteDBInstance(ctx, rds)
  48. }
  49. func (self *DBInstanceDeleteTask) DeleteDBInstance(ctx context.Context, rds *models.SDBInstance) {
  50. irds, err := rds.GetIDBInstance(ctx)
  51. if err != nil {
  52. if errors.Cause(err) == cloudprovider.ErrNotFound {
  53. self.DeleteDBInstanceComplete(ctx, rds)
  54. return
  55. }
  56. self.taskFailed(ctx, rds, err)
  57. return
  58. }
  59. if !jsonutils.QueryBoolean(self.Params, "purge", false) {
  60. err = irds.Delete()
  61. if err != nil {
  62. self.taskFailed(ctx, rds, err)
  63. return
  64. }
  65. err = cloudprovider.WaitDeleted(irds, time.Second*10, time.Minute*50)
  66. if err != nil {
  67. self.taskFailed(ctx, rds, errors.Wrapf(err, "WaitDeleted"))
  68. }
  69. }
  70. self.DeleteDBInstanceComplete(ctx, rds)
  71. }
  72. func (self *DBInstanceDeleteTask) DeleteDBInstanceComplete(ctx context.Context, rds *models.SDBInstance) {
  73. region, err := rds.GetRegion()
  74. if err != nil {
  75. self.taskFailed(ctx, rds, errors.Wrapf(err, "GetRegion"))
  76. return
  77. }
  78. if !region.GetDriver().IsSupportKeepDBInstanceManualBackup() || jsonutils.QueryBoolean(self.Params, "purge", false) {
  79. err = rds.RealDelete(ctx, self.UserCred)
  80. if err != nil {
  81. self.taskFailed(ctx, rds, errors.Wrap(err, "Delete"))
  82. return
  83. }
  84. //notifyclient.NotifyWebhook(ctx, self.UserCred, rds, notifyclient.ActionDelete)
  85. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  86. Obj: rds,
  87. Action: notifyclient.ActionDelete,
  88. })
  89. self.SetStageComplete(ctx, nil)
  90. return
  91. }
  92. self.DeleteBackups(ctx, rds, nil)
  93. //notifyclient.NotifyWebhook(ctx, self.UserCred, rds, notifyclient.ActionDelete)
  94. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  95. Obj: rds,
  96. Action: notifyclient.ActionDelete,
  97. })
  98. }
  99. func (self *DBInstanceDeleteTask) DeleteBackups(ctx context.Context, instance *models.SDBInstance, data jsonutils.JSONObject) {
  100. if !jsonutils.QueryBoolean(self.Params, "keep_backup", false) {
  101. backups, _ := instance.GetDBInstanceBackupByMode(api.BACKUP_MODE_MANUAL)
  102. for i := range backups {
  103. backups[i].StartDBInstanceBackupDeleteTask(ctx, self.UserCred, "")
  104. }
  105. }
  106. err := instance.RealDelete(ctx, self.UserCred)
  107. if err != nil {
  108. self.taskFailed(ctx, instance, errors.Wrap(err, "instance.Purge"))
  109. return
  110. }
  111. self.SetStageComplete(ctx, nil)
  112. }