pending_deleted.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 db
  15. import (
  16. "context"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/rbacscope"
  22. "yunion.io/x/pkg/util/timeutils"
  23. "yunion.io/x/pkg/utils"
  24. "yunion.io/x/sqlchemy"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  27. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  28. "yunion.io/x/onecloud/pkg/mcclient"
  29. "yunion.io/x/onecloud/pkg/util/logclient"
  30. )
  31. type SPendingDeletedBaseManager struct{}
  32. type SPendingDeletedBase struct {
  33. // 资源放入回收站时间
  34. PendingDeletedAt time.Time `json:"pending_deleted_at" list:"user" update:"admin"`
  35. // 资源是否处于回收站中
  36. PendingDeleted bool `nullable:"false" default:"false" index:"true" get:"user" list:"user" json:"pending_deleted"`
  37. }
  38. // GetPendingDeleted implements IPendingDeltable
  39. func (base *SPendingDeletedBase) GetPendingDeleted() bool {
  40. return base.PendingDeleted
  41. }
  42. func (base *SPendingDeletedBase) MarkPendingDeleted() {
  43. base.PendingDeleted = true
  44. base.PendingDeletedAt = timeutils.UtcNow()
  45. }
  46. func (base *SPendingDeletedBase) CancelPendingDeleted() {
  47. base.PendingDeleted = false
  48. base.PendingDeletedAt = time.Time{}
  49. }
  50. // GetPendingDeletedAt implements IPendingDeltable
  51. func (base *SPendingDeletedBase) GetPendingDeletedAt() time.Time {
  52. return base.PendingDeletedAt
  53. }
  54. func (base *SPendingDeletedBaseManager) FilterBySystemAttributes(manager IStandaloneModelManager, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject, scope rbacscope.TRbacScope) *sqlchemy.SQuery {
  55. var pendingDelete string
  56. if query != nil {
  57. pendingDelete, _ = query.GetString("pending_delete")
  58. }
  59. pendingDeleteLower := strings.ToLower(pendingDelete)
  60. if pendingDeleteLower == "all" || pendingDeleteLower == "any" || utils.ToBool(pendingDeleteLower) {
  61. var isAllow bool
  62. allowScope, result := policy.PolicyManager.AllowScope(userCred, consts.GetServiceType(), manager.KeywordPlural(), policy.PolicyActionList, "pending_delete")
  63. if result.Result.IsAllow() && !scope.HigherThan(allowScope) {
  64. isAllow = true
  65. }
  66. if !isAllow {
  67. pendingDeleteLower = ""
  68. }
  69. }
  70. if pendingDeleteLower == "all" || pendingDeleteLower == "any" {
  71. } else if utils.ToBool(pendingDeleteLower) {
  72. q = q.IsTrue("pending_deleted")
  73. } else {
  74. q = q.Filter(sqlchemy.OR(sqlchemy.IsNull(q.Field("pending_deleted")), sqlchemy.IsFalse(q.Field("pending_deleted"))))
  75. }
  76. return q
  77. }
  78. func (base *SPendingDeletedBase) MarkPendingDelete(model IStandaloneModel, ctx context.Context, userCred mcclient.TokenCredential, newName string) error {
  79. if !base.PendingDeleted {
  80. _, err := Update(model, func() error {
  81. if len(newName) > 0 {
  82. model.SetName(newName)
  83. }
  84. model.MarkPendingDeleted()
  85. return nil
  86. })
  87. if err != nil {
  88. return errors.Wrap(err, "MarkPendingDelete.Update")
  89. }
  90. OpsLog.LogEvent(model, ACT_PENDING_DELETE, model.GetShortDesc(ctx), userCred)
  91. logclient.AddSimpleActionLog(model, logclient.ACT_PENDING_DELETE, model.GetShortDesc(ctx), userCred, true)
  92. }
  93. return nil
  94. }
  95. func (base *SPendingDeletedBase) MarkCancelPendingDelete(model IStandaloneModel, ctx context.Context, userCred mcclient.TokenCredential) error {
  96. manager := model.GetModelManager()
  97. ownerId := model.GetOwnerId()
  98. lockman.LockRawObject(ctx, manager.Keyword(), "name")
  99. defer lockman.ReleaseRawObject(ctx, manager.Keyword(), "name")
  100. newName, err := GenerateName(ctx, manager, ownerId, model.GetName())
  101. if err != nil {
  102. return errors.Wrapf(err, "GenerateNam")
  103. }
  104. _, err = Update(model, func() error {
  105. model.SetName(newName)
  106. model.CancelPendingDeleted()
  107. return nil
  108. })
  109. if err != nil {
  110. return errors.Wrapf(err, "MarkCancelPendingDelete.Update")
  111. }
  112. OpsLog.LogEvent(model, ACT_CANCEL_DELETE, model.GetShortDesc(ctx), userCred)
  113. logclient.AddSimpleActionLog(model, logclient.ACT_CANCEL_DELETE, model.GetShortDesc(ctx), userCred, true)
  114. return nil
  115. }