guest_publicip_to_eip_task.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 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/compute/models"
  22. "yunion.io/x/onecloud/pkg/util/logclient"
  23. )
  24. type GuestPublicipToEipTask struct {
  25. taskman.STask
  26. }
  27. func init() {
  28. taskman.RegisterTask(GuestPublicipToEipTask{})
  29. }
  30. func (self *GuestPublicipToEipTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  31. guest := obj.(*models.SGuest)
  32. self.SetStage("OnEipConvertComplete", nil)
  33. drv, err := guest.GetDriver()
  34. if err != nil {
  35. db.OpsLog.LogEvent(guest, db.ACT_EIP_CONVERT_FAIL, err, self.UserCred)
  36. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_EIP_CONVERT, err, self.UserCred, false)
  37. guest.SetStatus(ctx, self.GetUserCred(), api.VM_EIP_CONVERT_FAILED, err.Error())
  38. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. return
  40. }
  41. err = drv.RequestConvertPublicipToEip(ctx, self.GetUserCred(), guest, self)
  42. if err != nil {
  43. db.OpsLog.LogEvent(guest, db.ACT_EIP_CONVERT_FAIL, err, self.UserCred)
  44. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_EIP_CONVERT, err, self.UserCred, false)
  45. guest.SetStatus(ctx, self.GetUserCred(), api.VM_EIP_CONVERT_FAILED, err.Error())
  46. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  47. return
  48. }
  49. }
  50. func (self *GuestPublicipToEipTask) OnEipConvertComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  51. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_EIP_CONVERT, nil, self.UserCred, true)
  52. self.SetStage("OnGuestSyncstatusComplete", nil)
  53. guest.StartSyncstatus(ctx, self.UserCred, self.GetTaskId())
  54. }
  55. func (self *GuestPublicipToEipTask) OnEipConvertCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  56. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_EIP_CONVERT, data, self.UserCred, false)
  57. guest.SetStatus(ctx, self.UserCred, api.VM_EIP_CONVERT_FAILED, data.String())
  58. self.SetStageFailed(ctx, data)
  59. }
  60. func (self *GuestPublicipToEipTask) OnGuestSyncstatusComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  61. if guest.Status == api.VM_READY && jsonutils.QueryBoolean(self.Params, "auto_start", false) {
  62. self.SetStage("OnGuestStartSucc", nil)
  63. guest.StartGueststartTask(ctx, self.UserCred, nil, self.GetId())
  64. return
  65. }
  66. self.SetStageComplete(ctx, nil)
  67. }
  68. func (self *GuestPublicipToEipTask) OnGuestSyncstatusCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  69. self.SetStageFailed(ctx, data)
  70. }
  71. func (self *GuestPublicipToEipTask) OnGuestStartSucc(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  72. self.SetStageComplete(ctx, nil)
  73. }
  74. func (self *GuestPublicipToEipTask) OnGuestStartSuccFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  75. self.SetStageFailed(ctx, data)
  76. }