eip_associate_task.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 eip
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type EipAssociateTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(EipAssociateTask{})
  31. }
  32. func (self *EipAssociateTask) taskFail(ctx context.Context, eip *models.SElasticip, obj db.IStatusStandaloneModel, err error) {
  33. eip.SetStatus(ctx, self.UserCred, api.EIP_STATUS_ASSOCIATE_FAIL, err.Error())
  34. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  35. if obj != nil {
  36. db.StatusBaseSetStatus(ctx, obj, self.GetUserCred(), api.INSTANCE_ASSOCIATE_EIP_FAILED, err.Error())
  37. db.OpsLog.LogEvent(obj, db.ACT_EIP_ATTACH, err, self.GetUserCred())
  38. logclient.AddActionLogWithStartable(self, obj, logclient.ACT_EIP_ASSOCIATE, err, self.UserCred, false)
  39. }
  40. logclient.AddActionLogWithStartable(self, eip, logclient.ACT_VM_ASSOCIATE, err, self.UserCred, false)
  41. }
  42. func (self *EipAssociateTask) GetAssociateInput() (api.ElasticipAssociateInput, error) {
  43. input := api.ElasticipAssociateInput{}
  44. err := self.Params.Unmarshal(&input)
  45. if err != nil {
  46. return input, errors.Wrapf(err, "self.Params.Unmarshal")
  47. }
  48. return input, nil
  49. }
  50. func (self *EipAssociateTask) GetAssociateObj(ctx context.Context) (db.IStatusStandaloneModel, api.ElasticipAssociateInput, error) {
  51. input, err := self.GetAssociateInput()
  52. if err != nil {
  53. return nil, input, errors.Wrapf(err, "GetAssociateInput")
  54. }
  55. switch input.InstanceType {
  56. case api.EIP_ASSOCIATE_TYPE_SERVER:
  57. vmObj, err := db.FetchByIdOrName(ctx, models.GuestManager, self.UserCred, input.InstanceId)
  58. if err != nil {
  59. return nil, input, errors.Wrapf(err, "GuestManager.FetchByIdOrName(%q)", input.InstanceId)
  60. }
  61. vm := vmObj.(*models.SGuest)
  62. input.InstanceExternalId = vm.ExternalId
  63. return vm, input, nil
  64. case api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY:
  65. natObj, err := db.FetchByIdOrName(ctx, models.NatGatewayManager, self.UserCred, input.InstanceId)
  66. if err != nil {
  67. return nil, input, errors.Wrapf(err, "NatGatewayManager.FetchByIdOrName(%q)", input.InstanceId)
  68. }
  69. nat := natObj.(*models.SNatGateway)
  70. input.InstanceExternalId = nat.ExternalId
  71. return nat, input, nil
  72. case api.EIP_ASSOCIATE_TYPE_INSTANCE_GROUP:
  73. grpObj, err := models.GroupManager.FetchById(input.InstanceId)
  74. if err != nil {
  75. return nil, input, errors.Wrapf(err, "GroupManager.FetchById(%s)", input.InstanceId)
  76. }
  77. grp := grpObj.(*models.SGroup)
  78. return grp, input, nil
  79. case api.EIP_ASSOCIATE_TYPE_LOADBALANCER:
  80. obj, err := db.FetchByIdOrName(ctx, models.LoadbalancerManager, self.UserCred, input.InstanceId)
  81. if err != nil {
  82. return nil, input, errors.Wrapf(err, "LoadbalancerManager.FetchByIdOrName(%q)", input.InstanceId)
  83. }
  84. m := obj.(*models.SLoadbalancer)
  85. input.InstanceExternalId = m.ExternalId
  86. return m, input, nil
  87. default:
  88. return nil, input, fmt.Errorf("invalid instance type %s", input.InstanceType)
  89. }
  90. }
  91. func (self *EipAssociateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  92. eip := obj.(*models.SElasticip)
  93. region, err := eip.GetRegion()
  94. if err != nil {
  95. self.taskFail(ctx, eip, nil, errors.Wrapf(err, "eip.GetRegion"))
  96. return
  97. }
  98. ins, input, err := self.GetAssociateObj(ctx)
  99. if err != nil {
  100. self.taskFail(ctx, eip, nil, errors.Wrapf(err, "self.GetAssociateObj"))
  101. return
  102. }
  103. db.StatusBaseSetStatus(ctx, ins, self.GetUserCred(), api.INSTANCE_ASSOCIATE_EIP, "associate eip")
  104. self.SetStage("OnAssociateEipComplete", nil)
  105. err = region.GetDriver().RequestAssociateEip(ctx, self.UserCred, eip, input, ins, self)
  106. if err != nil {
  107. self.taskFail(ctx, eip, ins, errors.Wrapf(err, "RequestAssociateEip"))
  108. return
  109. }
  110. }
  111. func (self *EipAssociateTask) OnAssociateEipComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  112. eip := obj.(*models.SElasticip)
  113. ins, input, err := self.GetAssociateObj(ctx)
  114. if err == nil {
  115. switch input.InstanceType {
  116. case api.EIP_ASSOCIATE_TYPE_SERVER:
  117. server := ins.(*models.SGuest)
  118. server.StartSyncstatus(ctx, self.UserCred, "")
  119. logclient.AddActionLogWithStartable(self, eip, logclient.ACT_VM_ASSOCIATE, ins, self.UserCred, true)
  120. case api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY:
  121. nat := ins.(*models.SNatGateway)
  122. nat.StartSyncstatus(ctx, self.UserCred, "")
  123. logclient.AddActionLogWithStartable(self, eip, logclient.ACT_NATGATEWAY_ASSOCIATE, ins, self.UserCred, true)
  124. case api.EIP_ASSOCIATE_TYPE_INSTANCE_GROUP:
  125. grp := ins.(*models.SGroup)
  126. grp.SetStatus(ctx, self.UserCred, "init", "success")
  127. logclient.AddActionLogWithStartable(self, eip, logclient.ACT_NATGATEWAY_ASSOCIATE, ins, self.UserCred, true)
  128. case api.EIP_ASSOCIATE_TYPE_LOADBALANCER:
  129. lb := ins.(*models.SLoadbalancer)
  130. lb.StartSyncstatus(ctx, self.UserCred, "")
  131. logclient.AddActionLogWithStartable(self, eip, logclient.ACT_LOADBALANCER_ASSOCIATE, ins, self.UserCred, true)
  132. }
  133. logclient.AddActionLogWithStartable(self, ins, logclient.ACT_EIP_ASSOCIATE, nil, self.UserCred, true)
  134. }
  135. self.SetStageComplete(ctx, nil)
  136. }
  137. func (self *EipAssociateTask) OnAssociateEipCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  138. eip := obj.(*models.SElasticip)
  139. ins, _, _ := self.GetAssociateObj(ctx)
  140. self.taskFail(ctx, eip, ins, errors.Errorf("%s", data.String()))
  141. }