cloudgroup_delete_task.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 tasks
  15. import (
  16. "context"
  17. "yunion.io/x/cloudmux/pkg/apis"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/cloudid/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type CloudgroupDeleteTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(CloudgroupDeleteTask{})
  31. }
  32. func (self *CloudgroupDeleteTask) taskFailed(ctx context.Context, group *models.SCloudgroup, err error) {
  33. group.SetStatus(ctx, self.GetUserCred(), apis.STATUS_DELETE_FAILED, err.Error())
  34. logclient.AddActionLogWithStartable(self, group, logclient.ACT_DELETE, err, self.UserCred, false)
  35. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  36. }
  37. func (self *CloudgroupDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  38. group := obj.(*models.SCloudgroup)
  39. iGroup, err := group.GetICloudgroup()
  40. if err != nil {
  41. if errors.Cause(err) == cloudprovider.ErrNotFound {
  42. self.taskComplete(ctx, group)
  43. return
  44. }
  45. self.taskFailed(ctx, group, errors.Wrap(err, "GetICloudgroup"))
  46. return
  47. }
  48. users, err := iGroup.GetICloudusers()
  49. if err != nil {
  50. self.taskFailed(ctx, group, errors.Wrap(err, "GetICloudusers"))
  51. return
  52. }
  53. for i := range users {
  54. err = iGroup.RemoveUser(users[i].GetName())
  55. if err != nil {
  56. self.taskFailed(ctx, group, errors.Wrapf(err, "RemoveUser(%s)", users[i].GetName()))
  57. return
  58. }
  59. }
  60. policies, err := iGroup.GetICloudpolicies()
  61. if err != nil {
  62. self.taskFailed(ctx, group, errors.Wrapf(err, "GetICloudpolicies"))
  63. return
  64. }
  65. for i := range policies {
  66. err = iGroup.DetachPolicy(policies[i].GetGlobalId(), policies[i].GetPolicyType())
  67. if err != nil {
  68. self.taskFailed(ctx, group, errors.Wrapf(err, "DetachPolicy(%s)", policies[i].GetName()))
  69. return
  70. }
  71. }
  72. err = iGroup.Delete()
  73. if err != nil {
  74. self.taskFailed(ctx, group, errors.Wrap(err, "iGroup.Delete"))
  75. return
  76. }
  77. self.taskComplete(ctx, group)
  78. }
  79. func (self *CloudgroupDeleteTask) taskComplete(ctx context.Context, group *models.SCloudgroup) {
  80. logclient.AddActionLogWithStartable(self, group, logclient.ACT_DELETE, nil, self.UserCred, true)
  81. group.RealDelete(ctx, self.GetUserCred())
  82. self.SetStageComplete(ctx, nil)
  83. }