filesystem_delete_task.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 filesystem
  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 FileSystemDeleteTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(FileSystemDeleteTask{})
  33. }
  34. func (self *FileSystemDeleteTask) taskFailed(ctx context.Context, fs *models.SFileSystem, err error) {
  35. fs.SetStatus(ctx, self.UserCred, api.NAS_STATUS_DELETE_FAILED, err.Error())
  36. logclient.AddActionLogWithStartable(self, fs, logclient.ACT_DELOCATE, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *FileSystemDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  40. fs := obj.(*models.SFileSystem)
  41. if len(fs.ExternalId) == 0 {
  42. self.taskComplete(ctx, fs)
  43. return
  44. }
  45. iFs, err := fs.GetICloudFileSystem(ctx)
  46. if err != nil {
  47. if errors.Cause(err) == cloudprovider.ErrNotFound {
  48. self.taskComplete(ctx, fs)
  49. return
  50. }
  51. self.taskFailed(ctx, fs, errors.Wrapf(err, "fs.GetICloudFileSystem"))
  52. return
  53. }
  54. err = func() error {
  55. mts, err := iFs.GetMountTargets()
  56. if err != nil {
  57. return errors.Wrapf(err, "iFs.GetMountTargets")
  58. }
  59. for i := range mts {
  60. err = mts[i].Delete()
  61. if err != nil {
  62. return errors.Wrapf(err, "Delete MountTarget")
  63. }
  64. }
  65. return nil
  66. }()
  67. if err != nil {
  68. self.taskFailed(ctx, fs, errors.Wrapf(err, "Delete MountTarget"))
  69. return
  70. }
  71. err = iFs.Delete()
  72. if err != nil {
  73. self.taskFailed(ctx, fs, errors.Wrapf(err, "iFs.Delete"))
  74. return
  75. }
  76. cloudprovider.WaitDeleted(iFs, time.Second*10, time.Minute*5)
  77. self.taskComplete(ctx, fs)
  78. }
  79. func (self *FileSystemDeleteTask) taskComplete(ctx context.Context, fs *models.SFileSystem) {
  80. fs.RealDelete(ctx, self.GetUserCred())
  81. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  82. Obj: self,
  83. Action: notifyclient.ActionDelete,
  84. })
  85. self.SetStageComplete(ctx, nil)
  86. }