cdn_domain_delete_task.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 cdn
  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 CDNDomainDeleteTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(CDNDomainDeleteTask{})
  33. }
  34. func (self *CDNDomainDeleteTask) taskFailed(ctx context.Context, domain *models.SCDNDomain, err error) {
  35. domain.SetStatus(ctx, self.UserCred, api.CDN_DOMAIN_STATUS_DELETE_FAILED, err.Error())
  36. db.OpsLog.LogEvent(domain, db.ACT_DELOCATE_FAIL, err.Error(), self.UserCred)
  37. logclient.AddActionLogWithStartable(self, domain, logclient.ACT_DELETE, err.Error(), self.UserCred, false)
  38. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. }
  40. func (self *CDNDomainDeleteTask) taskComplete(ctx context.Context, domain *models.SCDNDomain) {
  41. domain.RealDelete(ctx, self.GetUserCred())
  42. self.SetStageComplete(ctx, nil)
  43. }
  44. func (self *CDNDomainDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  45. domain := obj.(*models.SCDNDomain)
  46. iDomain, err := domain.GetICloudCDNDomain(ctx)
  47. if err != nil {
  48. if errors.Cause(err) == cloudprovider.ErrNotFound || errors.Cause(err) == sql.ErrNoRows {
  49. self.taskComplete(ctx, domain)
  50. return
  51. }
  52. self.taskFailed(ctx, domain, errors.Wrapf(err, "GetICloudCDNDomain"))
  53. return
  54. }
  55. err = iDomain.Delete()
  56. if err != nil {
  57. self.taskFailed(ctx, domain, errors.Wrapf(err, "Delete"))
  58. return
  59. }
  60. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  61. Obj: domain,
  62. Action: notifyclient.ActionDelete,
  63. })
  64. self.taskComplete(ctx, domain)
  65. }