loadbalancer_health_check_create_task.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/cloudcommon/notifyclient"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type LoadbalancerHealthCheckCreateTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(LoadbalancerHealthCheckCreateTask{})
  33. }
  34. func (self *LoadbalancerHealthCheckCreateTask) taskFail(ctx context.Context, hc *models.SLoadbalancerHealthCheck, err error) {
  35. hc.SetStatus(ctx, self.GetUserCred(), api.LB_CREATE_FAILED, err.Error())
  36. db.OpsLog.LogEvent(hc, db.ACT_ALLOCATE_FAIL, err, self.UserCred)
  37. logclient.AddActionLogWithStartable(self, hc, logclient.ACT_CREATE, err, self.UserCred, false)
  38. notifyclient.NotifySystemErrorWithCtx(ctx, hc.Id, hc.Name, api.LB_CREATE_FAILED, err.Error())
  39. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  40. }
  41. func (self *LoadbalancerHealthCheckCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  42. hc := obj.(*models.SLoadbalancerHealthCheck)
  43. iRegion, err := hc.GetIRegion(ctx)
  44. if err != nil {
  45. self.taskFail(ctx, hc, errors.Wrapf(err, "GetIRegion"))
  46. return
  47. }
  48. opts := &cloudprovider.SLoadbalancerHealthCheck{
  49. Name: hc.Name,
  50. HealthCheckType: hc.HealthCheckType,
  51. HealthCheckDomain: hc.HealthCheckDomain,
  52. HealthCheckURI: hc.HealthCheckURI,
  53. HealthCheckHttpCode: hc.HealthCheckHttpCode,
  54. HealthCheckMethod: hc.HealthCheckMethod,
  55. HealthCheckPort: hc.HealthCheckPort,
  56. HealthCheckTimeout: hc.HealthCheckTimeout,
  57. HealthCheckInterval: hc.HealthCheckInterval,
  58. HealthCheckRise: hc.HealthCheckRise,
  59. HealthCheckReq: hc.HealthCheckReq,
  60. HealthCheckExp: hc.HealthCheckExp,
  61. }
  62. iHc, err := iRegion.CreateILoadBalancerHealthCheck(opts)
  63. if err != nil {
  64. self.taskFail(ctx, hc, errors.Wrapf(err, "CreateILoadBalancerHealthCheck"))
  65. return
  66. }
  67. _, err = db.Update(hc, func() error {
  68. hc.ExternalId = iHc.GetGlobalId()
  69. hc.Status = apis.STATUS_AVAILABLE
  70. return nil
  71. })
  72. if err != nil {
  73. self.taskFail(ctx, hc, errors.Wrapf(err, "Update"))
  74. }
  75. self.SetStageComplete(ctx, nil)
  76. }