register.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 quotas
  15. import (
  16. "context"
  17. "reflect"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. var (
  26. quotaManagerTable map[reflect.Type]IQuotaManager
  27. )
  28. func init() {
  29. quotaManagerTable = make(map[reflect.Type]IQuotaManager)
  30. db.AddUsages = AddUsages
  31. db.CancelUsages = CancelUsages
  32. db.CancelPendingUsagesInContext = cancelPendingUsagesInContext
  33. db.InitPendingUsagesInContext = initPendingUsagesInContext
  34. }
  35. func Register(manager IQuotaManager) {
  36. obj, _ := db.NewModelObject(manager)
  37. ele := reflect.Indirect(reflect.ValueOf(obj))
  38. quotaManagerTable[ele.Type()] = manager
  39. manager.SetVirtualObject(manager)
  40. }
  41. func getQuotaManager(quota IQuota) IQuotaManager {
  42. quotaType := reflect.Indirect(reflect.ValueOf(quota)).Type()
  43. if m, ok := quotaManagerTable[quotaType]; ok {
  44. return m
  45. } else {
  46. log.Fatalf("No manager for quota %s", quotaType.Name())
  47. return nil
  48. }
  49. }
  50. func CancelPendingUsage(ctx context.Context, userCred mcclient.TokenCredential, localUsage IQuota, cancelUsage IQuota, save bool) error {
  51. if !consts.EnableQuotaCheck() {
  52. return nil
  53. }
  54. if localUsage == nil {
  55. return nil
  56. }
  57. manager := getQuotaManager(cancelUsage)
  58. return manager.cancelPendingUsage(ctx, userCred, localUsage, cancelUsage, save)
  59. }
  60. func CheckSetPendingQuota(ctx context.Context, userCred mcclient.TokenCredential, quota IQuota) error {
  61. if !consts.EnableQuotaCheck() {
  62. return nil
  63. }
  64. manager := getQuotaManager(quota)
  65. err := manager.checkSetPendingQuota(ctx, userCred, quota)
  66. if err != nil {
  67. return errors.Wrap(err, "manager.checkSetPendingQuota")
  68. }
  69. savePendingUsagesInContext(ctx, quota)
  70. return nil
  71. }
  72. func CancelUsages(ctx context.Context, userCred mcclient.TokenCredential, usages []db.IUsage) {
  73. if !consts.EnableQuotaCheck() {
  74. return
  75. }
  76. for _, usage := range usages {
  77. cancelUsage(ctx, userCred, usage.(IQuota))
  78. }
  79. }
  80. func cancelUsage(ctx context.Context, userCred mcclient.TokenCredential, usage IQuota) {
  81. manager := getQuotaManager(usage)
  82. err := manager.cancelUsage(ctx, userCred, usage)
  83. if err != nil {
  84. log.Errorf("cancelUsage %s fail: %s", jsonutils.Marshal(usage), err)
  85. }
  86. }
  87. func AddUsages(ctx context.Context, userCred mcclient.TokenCredential, usages []db.IUsage) {
  88. if !consts.EnableQuotaCheck() {
  89. return
  90. }
  91. for _, usage := range usages {
  92. addUsage(ctx, userCred, usage.(IQuota))
  93. }
  94. }
  95. func addUsage(ctx context.Context, userCred mcclient.TokenCredential, usage IQuota) {
  96. manager := getQuotaManager(usage)
  97. err := manager.addUsage(ctx, userCred, usage)
  98. if err != nil {
  99. log.Errorf("cancelUsage %s fail: %s", jsonutils.Marshal(usage), err)
  100. }
  101. }
  102. func GetQuotaCount(ctx context.Context, request IQuota, pendingKeys IQuotaKeys) (int, error) {
  103. manager := getQuotaManager(request)
  104. return manager.getQuotaCount(ctx, request, pendingKeys)
  105. }