guest_set_auto_renew_task.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 guest
  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 GuestSetAutoRenewTask struct {
  26. SGuestBaseTask
  27. }
  28. func init() {
  29. taskman.RegisterTask(GuestSetAutoRenewTask{})
  30. }
  31. func (self *GuestSetAutoRenewTask) taskFailed(ctx context.Context, guest *models.SGuest, err error) {
  32. db.OpsLog.LogEvent(guest, db.ACT_SET_AUTO_RENEW_FAIL, err, self.UserCred)
  33. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_SET_AUTO_RENEW, err, self.UserCred, false)
  34. guest.SetStatus(ctx, self.GetUserCred(), api.VM_SET_AUTO_RENEW_FAILED, err.Error())
  35. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  36. }
  37. func (self *GuestSetAutoRenewTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  38. guest := obj.(*models.SGuest)
  39. self.SetStage("OnSetAutoRenewComplete", nil)
  40. input := api.GuestAutoRenewInput{}
  41. self.GetParams().Unmarshal(&input)
  42. drv, err := guest.GetDriver()
  43. if err != nil {
  44. self.taskFailed(ctx, guest, errors.Wrapf(err, "GetDriver"))
  45. return
  46. }
  47. err = drv.RequestSetAutoRenewInstance(ctx, self.UserCred, guest, input, self)
  48. if err != nil {
  49. self.taskFailed(ctx, guest, errors.Wrapf(err, "RequestSetAutoRenewInstance"))
  50. return
  51. }
  52. }
  53. func (self *GuestSetAutoRenewTask) OnSetAutoRenewComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  54. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_SET_AUTO_RENEW, nil, self.UserCred, true)
  55. self.SetStage("OnGuestSyncstatusComplete", nil)
  56. guest.StartSyncstatus(ctx, self.UserCred, "")
  57. }
  58. func (self *GuestSetAutoRenewTask) OnSetAutoRenewCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  59. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_SET_AUTO_RENEW, data, self.UserCred, false)
  60. guest.SetStatus(ctx, self.GetUserCred(), api.VM_SET_AUTO_RENEW_FAILED, data.String())
  61. self.SetStageFailed(ctx, data)
  62. }
  63. func (self *GuestSetAutoRenewTask) OnGuestSyncstatusComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  64. self.SetStageComplete(ctx, nil)
  65. }
  66. func (self *GuestSetAutoRenewTask) OnGuestSyncstatusCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  67. self.SetStageFailed(ctx, data)
  68. }