network_delete_task.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 network
  15. import (
  16. "context"
  17. "database/sql"
  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 NetworkDeleteTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(NetworkDeleteTask{})
  33. }
  34. func (self *NetworkDeleteTask) taskFailed(ctx context.Context, network *models.SNetwork, err error) {
  35. network.SetStatus(ctx, self.UserCred, api.NETWORK_STATUS_DELETE_FAILED, err.Error())
  36. db.OpsLog.LogEvent(network, db.ACT_ALLOCATE_FAIL, err, self.UserCred)
  37. logclient.AddActionLogWithStartable(self, network, logclient.ACT_DELETE, err, self.UserCred, false)
  38. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. }
  40. func (self *NetworkDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  41. network := obj.(*models.SNetwork)
  42. network.SetStatus(ctx, self.UserCred, api.NETWORK_STATUS_DELETING, "")
  43. db.OpsLog.LogEvent(network, db.ACT_DELOCATING, network.GetShortDesc(ctx), self.UserCred)
  44. inet, err := network.GetINetwork(ctx)
  45. if err != nil {
  46. if errors.Cause(err) == cloudprovider.ErrNotFound || errors.Cause(err) == sql.ErrNoRows {
  47. self.taskComplete(ctx, network)
  48. return
  49. }
  50. self.taskFailed(ctx, network, errors.Wrapf(err, "GetINetwork"))
  51. return
  52. }
  53. err = inet.Delete()
  54. if err != nil {
  55. self.taskFailed(ctx, network, errors.Wrapf(err, "inet.Delete"))
  56. return
  57. }
  58. self.taskComplete(ctx, network)
  59. }
  60. func (self *NetworkDeleteTask) taskComplete(ctx context.Context, network *models.SNetwork) {
  61. network.RealDelete(ctx, self.UserCred)
  62. logclient.AddActionLogWithStartable(self, network, logclient.ACT_DELETE, "", self.UserCred, true)
  63. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  64. Obj: network,
  65. Action: notifyclient.ActionDelete,
  66. })
  67. self.SetStageComplete(ctx, nil)
  68. }