loadbalancer_health_check_syncstatus_task.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/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/compute/models"
  24. )
  25. type LoadbalancerHealthCheckSyncstatusTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(LoadbalancerHealthCheckSyncstatusTask{})
  30. }
  31. func (self *LoadbalancerHealthCheckSyncstatusTask) taskFail(ctx context.Context, hc *models.SLoadbalancerHealthCheck, err error) {
  32. hc.SetStatus(ctx, self.GetUserCred(), apis.STATUS_UNKNOWN, err.Error())
  33. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  34. }
  35. func (self *LoadbalancerHealthCheckSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  36. hc := obj.(*models.SLoadbalancerHealthCheck)
  37. iRegion, err := hc.GetIRegion(ctx)
  38. if err != nil {
  39. self.taskFail(ctx, hc, errors.Wrapf(err, "GetIRegion"))
  40. return
  41. }
  42. hcs, err := iRegion.GetILoadBalancerHealthChecks()
  43. if err != nil {
  44. self.taskFail(ctx, hc, errors.Wrapf(err, "GetLoadbalancerHealthChecks"))
  45. return
  46. }
  47. for i := range hcs {
  48. if hcs[i].GetGlobalId() == hc.ExternalId {
  49. err := hc.SyncWithCloudLoadbalancerHealthCheck(ctx, self.GetUserCred(), hcs[i], hc.GetCloudprovider())
  50. if err != nil {
  51. self.taskFail(ctx, hc, errors.Wrapf(err, "SyncWithCloudLoadbalancerHealthCheck"))
  52. return
  53. }
  54. self.taskComplete(ctx, hc)
  55. return
  56. }
  57. }
  58. self.taskFail(ctx, hc, errors.Wrapf(cloudprovider.ErrNotFound, "LoadbalancerHealthCheck not found"))
  59. }
  60. func (self *LoadbalancerHealthCheckSyncstatusTask) taskComplete(ctx context.Context, hc *models.SLoadbalancerHealthCheck) {
  61. hc.SetStatus(ctx, self.GetUserCred(), apis.STATUS_AVAILABLE, "")
  62. self.SetStageComplete(ctx, nil)
  63. }