loadbalancer_health_check_update_task.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/cloudprovider"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/apis"
  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/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type LoadbalancerHealthCheckUpdateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(LoadbalancerHealthCheckUpdateTask{})
  32. }
  33. func (self *LoadbalancerHealthCheckUpdateTask) taskFail(ctx context.Context, hc *models.SLoadbalancerHealthCheck, err error) {
  34. hc.SetStatus(ctx, self.GetUserCred(), api.LB_SYNC_CONF_FAILED, err.Error())
  35. db.OpsLog.LogEvent(hc, db.ACT_SYNC_CONF, err.Error(), self.UserCred)
  36. logclient.AddActionLogWithStartable(self, hc, logclient.ACT_SYNC_CONF, err.Error(), self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *LoadbalancerHealthCheckUpdateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  40. hc := obj.(*models.SLoadbalancerHealthCheck)
  41. iRegion, err := hc.GetIRegion(ctx)
  42. if err != nil {
  43. self.taskFail(ctx, hc, errors.Wrapf(err, "GetIRegion"))
  44. return
  45. }
  46. hcs, err := iRegion.GetILoadBalancerHealthChecks()
  47. if err != nil {
  48. self.taskFail(ctx, hc, errors.Wrapf(err, "GetLoadbalancerHealthChecks"))
  49. return
  50. }
  51. for i := range hcs {
  52. if hcs[i].GetGlobalId() == hc.ExternalId {
  53. opts := &cloudprovider.SLoadbalancerHealthCheck{
  54. Name: hc.Name,
  55. HealthCheckType: hc.HealthCheckType,
  56. HealthCheckDomain: hc.HealthCheckDomain,
  57. HealthCheckURI: hc.HealthCheckURI,
  58. HealthCheckHttpCode: hc.HealthCheckHttpCode,
  59. HealthCheckMethod: hc.HealthCheckMethod,
  60. HealthCheckPort: hc.HealthCheckPort,
  61. HealthCheckTimeout: hc.HealthCheckTimeout,
  62. HealthCheckInterval: hc.HealthCheckInterval,
  63. HealthCheckRise: hc.HealthCheckRise,
  64. HealthCheckReq: hc.HealthCheckReq,
  65. HealthCheckExp: hc.HealthCheckExp,
  66. }
  67. err := hcs[i].Update(ctx, opts)
  68. if err != nil {
  69. self.taskFail(ctx, hc, errors.Wrapf(err, "Update"))
  70. return
  71. }
  72. self.taskComplete(ctx, hc)
  73. return
  74. }
  75. }
  76. self.taskFail(ctx, hc, errors.Wrapf(errors.ErrNotFound, "LoadbalancerHealthCheck %s not found", hc.ExternalId))
  77. }
  78. func (self *LoadbalancerHealthCheckUpdateTask) taskComplete(ctx context.Context, hc *models.SLoadbalancerHealthCheck) {
  79. hc.SetStatus(ctx, self.GetUserCred(), apis.STATUS_AVAILABLE, "")
  80. self.SetStageComplete(ctx, nil)
  81. }