dnszone_delete_task.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 dnszone
  15. import (
  16. "context"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  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/cloudcommon/notifyclient"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type DnsZoneDeleteTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(DnsZoneDeleteTask{})
  32. }
  33. func (self *DnsZoneDeleteTask) taskFailed(ctx context.Context, dnsZone *models.SDnsZone, err error) {
  34. dnsZone.SetStatus(ctx, self.GetUserCred(), api.DNS_ZONE_STATUS_DELETE_FAILED, err.Error())
  35. db.OpsLog.LogEvent(dnsZone, db.ACT_DELETE, dnsZone.GetShortDesc(ctx), self.GetUserCred())
  36. logclient.AddActionLogWithContext(ctx, dnsZone, logclient.ACT_DELETE, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *DnsZoneDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  40. zone := obj.(*models.SDnsZone)
  41. iZone, err := zone.GetICloudDnsZone(ctx)
  42. if err != nil {
  43. if errors.Cause(err) == cloudprovider.ErrNotFound {
  44. self.taskComplete(ctx, zone)
  45. return
  46. }
  47. self.taskFailed(ctx, zone, errors.Wrapf(err, "GetICloudDnsZone"))
  48. return
  49. }
  50. err = iZone.Delete()
  51. if err != nil {
  52. self.taskFailed(ctx, zone, errors.Wrapf(err, "Delete"))
  53. return
  54. }
  55. self.taskComplete(ctx, zone)
  56. }
  57. func (self *DnsZoneDeleteTask) taskComplete(ctx context.Context, zone *models.SDnsZone) {
  58. zone.RealDelete(ctx, self.GetUserCred())
  59. logclient.AddActionLogWithContext(ctx, zone, logclient.ACT_DELETE, nil, self.UserCred, true)
  60. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  61. Obj: zone,
  62. Action: notifyclient.ActionDelete,
  63. })
  64. self.SetStageComplete(ctx, nil)
  65. }