stores.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/rbacscope"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. )
  22. const (
  23. METADATA_KEY = "quota"
  24. )
  25. type SDBQuotaStore struct {
  26. }
  27. func newDBQuotaStore() *SDBQuotaStore {
  28. return &SDBQuotaStore{}
  29. }
  30. func (store *SDBQuotaStore) GetQuota(ctx context.Context, scope rbacscope.TRbacScope, ownerId mcclient.IIdentityProvider, quota IQuota) error {
  31. var tenant *db.STenant
  32. var err error
  33. switch scope {
  34. case rbacscope.ScopeDomain:
  35. tenant, err = db.TenantCacheManager.FetchDomainById(ctx, ownerId.GetProjectDomainId())
  36. default:
  37. tenant, err = db.TenantCacheManager.FetchTenantById(ctx, ownerId.GetProjectId())
  38. }
  39. if err != nil {
  40. return err
  41. }
  42. quotaStr := tenant.GetMetadata(ctx, METADATA_KEY, nil)
  43. quotaJson, _ := jsonutils.ParseString(quotaStr)
  44. if quotaJson != nil {
  45. return quotaJson.Unmarshal(quota)
  46. }
  47. return nil
  48. }