nat_delete_task.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type NatGatewayDeleteTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(NatGatewayDeleteTask{})
  33. }
  34. func (self *NatGatewayDeleteTask) taskFailed(ctx context.Context, nat *models.SNatGateway, err error) {
  35. nat.SetStatus(ctx, self.UserCred, api.NAT_STATUS_DELETE_FAILED, err.Error())
  36. logclient.AddActionLogWithStartable(self, nat, logclient.ACT_DELOCATE, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *NatGatewayDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  40. nat := obj.(*models.SNatGateway)
  41. iNat, err := nat.GetINatGateway(ctx)
  42. if err != nil {
  43. if errors.Cause(err) == cloudprovider.ErrNotFound {
  44. self.OnEipDissociateComplete(ctx, nat, nil)
  45. return
  46. }
  47. self.taskFailed(ctx, nat, errors.Wrapf(err, "nat.GetINatGateway"))
  48. return
  49. }
  50. dnat, err := iNat.GetINatDTable()
  51. if err != nil {
  52. self.taskFailed(ctx, nat, errors.Wrapf(err, "iNat.GetINatDTable"))
  53. return
  54. }
  55. for i := range dnat {
  56. err = dnat[i].Delete()
  57. if err != nil {
  58. self.taskFailed(ctx, nat, errors.Wrapf(err, "delete d entry %v", dnat[i]))
  59. return
  60. }
  61. cloudprovider.WaitDeleted(dnat[i], time.Second*5, time.Minute)
  62. }
  63. snat, err := iNat.GetINatSTable()
  64. if err != nil {
  65. self.taskFailed(ctx, nat, errors.Wrapf(err, "GetINatSTable"))
  66. return
  67. }
  68. for i := range snat {
  69. err = snat[i].Delete()
  70. if err != nil {
  71. self.taskFailed(ctx, nat, errors.Wrapf(err, "delete s entry %v", snat[i]))
  72. return
  73. }
  74. cloudprovider.WaitDeleted(snat[i], time.Second*5, time.Minute)
  75. }
  76. self.SetStage("OnEipDissociateComplete", nil)
  77. self.OnEipDissociateComplete(ctx, nat, nil)
  78. }
  79. func (self *NatGatewayDeleteTask) OnEipDissociateComplete(ctx context.Context, nat *models.SNatGateway, data jsonutils.JSONObject) {
  80. eips, err := nat.GetEips()
  81. if err != nil {
  82. self.taskFailed(ctx, nat, errors.Wrapf(err, "nat.GetEips"))
  83. return
  84. }
  85. if len(eips) > 0 {
  86. eips[0].StartEipDissociateTask(ctx, self.GetUserCred(), false, self.GetTaskId())
  87. return
  88. }
  89. self.doDeleteNatGateway(ctx, nat)
  90. }
  91. func (self *NatGatewayDeleteTask) OnEipDissociateCompleteFailed(ctx context.Context, nat *models.SNatGateway, data jsonutils.JSONObject) {
  92. self.SetStageFailed(ctx, nil)
  93. }
  94. func (self *NatGatewayDeleteTask) doDeleteNatGateway(ctx context.Context, nat *models.SNatGateway) {
  95. iNat, err := nat.GetINatGateway(ctx)
  96. if err != nil {
  97. if errors.Cause(err) == cloudprovider.ErrNotFound {
  98. self.taskComplete(ctx, nat)
  99. return
  100. }
  101. self.taskFailed(ctx, nat, errors.Wrapf(err, "nat.GetINatGateway"))
  102. return
  103. }
  104. err = iNat.Delete()
  105. if err != nil {
  106. self.taskFailed(ctx, nat, errors.Wrapf(err, "iNat.Delete"))
  107. return
  108. }
  109. err = cloudprovider.WaitDeleted(iNat, time.Second*5, time.Minute*3)
  110. if err != nil {
  111. self.taskFailed(ctx, nat, errors.Wrapf(err, "cloudprovider.WaitDeleted"))
  112. return
  113. }
  114. self.taskComplete(ctx, nat)
  115. }
  116. func (self *NatGatewayDeleteTask) taskComplete(ctx context.Context, nat *models.SNatGateway) {
  117. nat.RealDelete(ctx, self.GetUserCred())
  118. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  119. Obj: nat,
  120. Action: notifyclient.ActionDelete,
  121. })
  122. self.SetStageComplete(ctx, nil)
  123. }