loadbalancer_start_task.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. api "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type LoadbalancerStartTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(LoadbalancerStartTask{})
  30. }
  31. func (self *LoadbalancerStartTask) taskFail(ctx context.Context, lb *models.SLoadbalancer, reason jsonutils.JSONObject) {
  32. lb.SetStatus(ctx, self.GetUserCred(), api.LB_STATUS_DISABLED, reason.String())
  33. db.OpsLog.LogEvent(lb, db.ACT_ENABLE, reason, self.UserCred)
  34. logclient.AddActionLogWithStartable(self, lb, logclient.ACT_ENABLE, reason, self.UserCred, false)
  35. notifyclient.NotifySystemErrorWithCtx(ctx, lb.Id, lb.Name, api.LB_STATUS_DISABLED, reason.String())
  36. self.SetStageFailed(ctx, reason)
  37. }
  38. func (self *LoadbalancerStartTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  39. lb := obj.(*models.SLoadbalancer)
  40. region, err := lb.GetRegion()
  41. if err != nil {
  42. self.taskFail(ctx, lb, jsonutils.NewString(err.Error()))
  43. return
  44. }
  45. self.SetStage("OnLoadbalancerStartComplete", nil)
  46. if err := region.GetDriver().RequestStartLoadbalancer(ctx, self.GetUserCred(), lb, self); err != nil {
  47. self.taskFail(ctx, lb, jsonutils.NewString(err.Error()))
  48. }
  49. }
  50. func (self *LoadbalancerStartTask) OnLoadbalancerStartComplete(ctx context.Context, lb *models.SLoadbalancer, data jsonutils.JSONObject) {
  51. lb.SetStatus(ctx, self.GetUserCred(), api.LB_STATUS_ENABLED, "")
  52. db.OpsLog.LogEvent(lb, db.ACT_ENABLE, lb.GetShortDesc(ctx), self.UserCred)
  53. logclient.AddActionLogWithStartable(self, lb, logclient.ACT_ENABLE, nil, self.UserCred, true)
  54. self.SetStageComplete(ctx, nil)
  55. }
  56. func (self *LoadbalancerStartTask) OnLoadbalancerStartCompleteFailed(ctx context.Context, lb *models.SLoadbalancer, reason jsonutils.JSONObject) {
  57. self.taskFail(ctx, lb, reason)
  58. }