nat_create_task.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 nat
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/billing"
  22. billing_api "yunion.io/x/onecloud/pkg/apis/billing"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  27. "yunion.io/x/onecloud/pkg/compute/models"
  28. "yunion.io/x/onecloud/pkg/util/logclient"
  29. )
  30. type NatGatewayCreateTask struct {
  31. taskman.STask
  32. }
  33. func init() {
  34. taskman.RegisterTask(NatGatewayCreateTask{})
  35. }
  36. func (self *NatGatewayCreateTask) taskFailed(ctx context.Context, nat *models.SNatGateway, err error) {
  37. nat.SetStatus(ctx, self.UserCred, api.NAT_STATUS_CREATE_FAILED, err.Error())
  38. logclient.AddActionLogWithStartable(self, nat, logclient.ACT_ALLOCATE, err, self.UserCred, false)
  39. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  40. }
  41. func (self *NatGatewayCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  42. nat := obj.(*models.SNatGateway)
  43. opts := cloudprovider.NatGatewayCreateOptions{
  44. Name: nat.Name,
  45. Desc: nat.Description,
  46. NatSpec: nat.NatSpec,
  47. }
  48. vpc, err := nat.GetVpc()
  49. if err != nil {
  50. self.taskFailed(ctx, nat, errors.Wrapf(err, "nat.GetVpc"))
  51. return
  52. }
  53. opts.VpcId = vpc.ExternalId
  54. if len(nat.NetworkId) > 0 {
  55. _network, err := models.NetworkManager.FetchById(nat.NetworkId)
  56. if err != nil {
  57. self.taskFailed(ctx, nat, errors.Wrapf(err, "NetworkManager.FetchById(%s)", nat.NetworkId))
  58. return
  59. }
  60. network := _network.(*models.SNetwork)
  61. opts.NetworkId = network.ExternalId
  62. }
  63. if nat.BillingType == billing_api.BILLING_TYPE_PREPAID {
  64. bc, err := billing.ParseBillingCycle(nat.BillingCycle)
  65. if err != nil {
  66. self.taskFailed(ctx, nat, errors.Wrapf(err, "ParseBillingCycle(%s)", nat.BillingCycle))
  67. return
  68. }
  69. bc.AutoRenew = nat.AutoRenew
  70. opts.BillingCycle = &bc
  71. }
  72. self.SetStage("OnCreateNatGatewayCreateComplete", nil)
  73. taskman.LocalTaskRun(self, func() (jsonutils.JSONObject, error) {
  74. iVpc, err := vpc.GetIVpc(ctx)
  75. if err != nil {
  76. return nil, errors.Wrapf(err, "vpc.GetIVpc")
  77. }
  78. iNat, err := iVpc.CreateINatGateway(&opts)
  79. if err != nil {
  80. return nil, errors.Wrapf(err, "iVpc.CreateINatGateway")
  81. }
  82. err = db.SetExternalId(nat, self.GetUserCred(), iNat.GetGlobalId())
  83. if err != nil {
  84. return nil, errors.Wrapf(err, "db.SetExternalId")
  85. }
  86. err = cloudprovider.WaitStatus(iNat, api.NAT_STAUTS_AVAILABLE, time.Second*5, time.Minute*10)
  87. if err != nil {
  88. return nil, errors.Wrapf(err, "cloudprovider.WaitStatus")
  89. }
  90. nat.SyncWithCloudNatGateway(ctx, self.GetUserCred(), nat.GetCloudprovider(), iNat)
  91. return nil, nil
  92. })
  93. }
  94. func (self *NatGatewayCreateTask) OnCreateNatGatewayCreateComplete(ctx context.Context, nat *models.SNatGateway, body jsonutils.JSONObject) {
  95. input := api.NatgatewayCreateInput{}
  96. self.GetParams().Unmarshal(&input)
  97. if len(input.Eip) > 0 || input.EipBw > 0 {
  98. self.SetStage("OnDeployEipComplete", nil)
  99. var eip *models.SElasticip = nil
  100. var err error
  101. if len(input.Eip) > 0 {
  102. eipObj, err := models.ElasticipManager.FetchById(input.Eip)
  103. if err != nil {
  104. self.OnDeployEipCompleteFailed(ctx, nat, jsonutils.NewString(errors.Wrapf(err, "ElasticipManager.FetchById(%s)", input.Eip).Error()))
  105. return
  106. }
  107. eip = eipObj.(*models.SElasticip)
  108. } else {
  109. pendingRegionUsage := models.SRegionQuota{}
  110. self.GetPendingUsage(&pendingRegionUsage, 1)
  111. self.SetPendingUsage(&pendingRegionUsage, 1)
  112. eip, err = models.ElasticipManager.NewEipForVMOnHost(ctx, self.UserCred, &models.NewEipForVMOnHostArgs{
  113. Bandwidth: input.EipBw,
  114. BgpType: input.EipBgpType,
  115. ChargeType: input.EipChargeType,
  116. AutoDellocate: input.EipAutoDellocate,
  117. Natgateway: nat,
  118. PendingUsage: &pendingRegionUsage,
  119. })
  120. self.SetPendingUsage(&pendingRegionUsage, 1)
  121. if err != nil {
  122. self.OnDeployEipCompleteFailed(ctx, nat, jsonutils.NewString(errors.Wrapf(err, "ElasticipManager.NewEipForVMOnHost").Error()))
  123. return
  124. }
  125. }
  126. opts := api.ElasticipAssociateInput{
  127. InstanceId: nat.Id,
  128. InstanceExternalId: nat.ExternalId,
  129. InstanceType: api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY,
  130. }
  131. if input.EipBw > 0 {
  132. // newly allocated eip, need allocation and associate
  133. err = eip.AllocateAndAssociateInstance(ctx, self.UserCred, nat, opts, self.GetId())
  134. err = errors.Wrap(err, "AllocateAndAssociateVM")
  135. } else {
  136. err = eip.StartEipAssociateInstanceTask(ctx, self.UserCred, opts, self.GetId())
  137. err = errors.Wrap(err, "StartEipAssociateInstanceTask")
  138. }
  139. if err != nil {
  140. self.OnDeployEipCompleteFailed(ctx, nat, jsonutils.NewString(err.Error()))
  141. return
  142. }
  143. return
  144. }
  145. self.OnDeployEipComplete(ctx, nat, nil)
  146. }
  147. func (self *NatGatewayCreateTask) OnCreateNatGatewayCreateCompleteFailed(ctx context.Context, nat *models.SNatGateway, body jsonutils.JSONObject) {
  148. self.taskFailed(ctx, nat, errors.Errorf("%s", body.String()))
  149. }
  150. func (self *NatGatewayCreateTask) OnDeployEipCompleteFailed(ctx context.Context, nat *models.SNatGateway, data jsonutils.JSONObject) {
  151. nat.SetStatus(ctx, self.UserCred, api.INSTANCE_ASSOCIATE_EIP_FAILED, data.String())
  152. db.OpsLog.LogEvent(nat, db.ACT_EIP_ATTACH, data, self.UserCred)
  153. logclient.AddActionLogWithStartable(self, nat, logclient.ACT_EIP_ASSOCIATE, data, self.UserCred, false)
  154. notifyclient.NotifySystemErrorWithCtx(ctx, nat.Id, nat.Name, api.INSTANCE_ASSOCIATE_EIP_FAILED, data.String())
  155. self.SetStageFailed(ctx, data)
  156. }
  157. func (self *NatGatewayCreateTask) OnDeployEipComplete(ctx context.Context, nat *models.SNatGateway, data jsonutils.JSONObject) {
  158. self.SetStage("OnSyncstatusComplete", nil)
  159. nat.StartSyncstatus(ctx, self.GetUserCred(), self.GetTaskId())
  160. }
  161. func (self *NatGatewayCreateTask) OnSyncstatusComplete(ctx context.Context, nat *models.SNatGateway, data jsonutils.JSONObject) {
  162. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  163. Obj: nat,
  164. Action: notifyclient.ActionCreate,
  165. })
  166. self.SetStageComplete(ctx, nil)
  167. }
  168. func (self *NatGatewayCreateTask) OnSyncstatusCompleteFailed(ctx context.Context, nat *models.SNatGateway, data jsonutils.JSONObject) {
  169. self.SetStageFailed(ctx, data)
  170. }