eip_deallocate_task.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/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 EipDeallocateTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(EipDeallocateTask{})
  33. }
  34. func (self *EipDeallocateTask) taskFail(ctx context.Context, eip *models.SElasticip, msg jsonutils.JSONObject) {
  35. eip.SetStatus(ctx, self.UserCred, api.EIP_STATUS_DEALLOCATE_FAIL, msg.String())
  36. db.OpsLog.LogEvent(eip, db.ACT_DELOCATE, msg, self.GetUserCred())
  37. logclient.AddActionLogWithStartable(self, eip, logclient.ACT_DELETE, msg, self.UserCred, false)
  38. notifyclient.EventNotify(ctx, self.GetUserCred(), notifyclient.SEventNotifyParam{
  39. Obj: eip,
  40. Action: notifyclient.ActionDelete,
  41. IsFail: true,
  42. })
  43. self.SetStageFailed(ctx, msg)
  44. return
  45. }
  46. func (self *EipDeallocateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  47. eip := obj.(*models.SElasticip)
  48. if eip.ExternalId != "" {
  49. expEip, err := eip.GetIEip(ctx)
  50. if err != nil {
  51. if errors.Cause(err) != cloudprovider.ErrNotFound && errors.Cause(err) != cloudprovider.ErrInvalidProvider {
  52. msg := fmt.Sprintf("fail to find iEIP for eip %s", err)
  53. self.taskFail(ctx, eip, jsonutils.NewString(msg))
  54. return
  55. }
  56. } else {
  57. err = expEip.Delete()
  58. if err != nil {
  59. msg := fmt.Sprintf("fail to delete iEIP %s", err)
  60. self.taskFail(ctx, eip, jsonutils.NewString(msg))
  61. return
  62. }
  63. }
  64. }
  65. if eip.IsManaged() {
  66. // TODO clear out guestnics
  67. }
  68. err := eip.RealDelete(ctx, self.UserCred)
  69. if err != nil {
  70. msg := fmt.Sprintf("fail to delete EIP %s", err)
  71. self.taskFail(ctx, eip, jsonutils.NewString(msg))
  72. return
  73. }
  74. logclient.AddActionLogWithStartable(self, eip, logclient.ACT_DELETE, nil, self.UserCred, true)
  75. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  76. Obj: eip,
  77. Action: notifyclient.ActionDelete,
  78. })
  79. self.SetStageComplete(ctx, nil)
  80. }