loadbalancer_health_check_delete_task.go 2.4 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 loadbalancer
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type LoadbalancerHealthCheckDeleteTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(LoadbalancerHealthCheckDeleteTask{})
  30. }
  31. func (self *LoadbalancerHealthCheckDeleteTask) taskFail(ctx context.Context, hc *models.SLoadbalancerHealthCheck, err error) {
  32. hc.SetStatus(ctx, self.GetUserCred(), api.LB_STATUS_DELETE_FAILED, err.Error())
  33. db.OpsLog.LogEvent(hc, db.ACT_DELOCATE_FAIL, err.Error(), self.UserCred)
  34. logclient.AddActionLogWithStartable(self, hc, logclient.ACT_DELOCATE, err.Error(), self.UserCred, false)
  35. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  36. }
  37. func (self *LoadbalancerHealthCheckDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  38. hc := obj.(*models.SLoadbalancerHealthCheck)
  39. if len(hc.ExternalId) == 0 {
  40. self.taskComplete(ctx, hc)
  41. return
  42. }
  43. iRegion, err := hc.GetIRegion(ctx)
  44. if err != nil {
  45. self.taskFail(ctx, hc, errors.Wrapf(err, "GetIRegion"))
  46. return
  47. }
  48. hcs, err := iRegion.GetILoadBalancerHealthChecks()
  49. if err != nil {
  50. self.taskFail(ctx, hc, errors.Wrapf(err, "GetLoadbalancerHealthChecks"))
  51. return
  52. }
  53. for i := range hcs {
  54. if hcs[i].GetGlobalId() == hc.ExternalId {
  55. err = hcs[i].Delete()
  56. if err != nil {
  57. self.taskFail(ctx, hc, errors.Wrapf(err, "Delete"))
  58. return
  59. }
  60. self.taskComplete(ctx, hc)
  61. return
  62. }
  63. }
  64. self.taskComplete(ctx, hc)
  65. }
  66. func (self *LoadbalancerHealthCheckDeleteTask) taskComplete(ctx context.Context, hc *models.SLoadbalancerHealthCheck) {
  67. hc.RealDelete(ctx, self.GetUserCred())
  68. self.SetStageComplete(ctx, nil)
  69. }