cache.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 usages
  15. import (
  16. "time"
  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. "yunion.io/x/onecloud/pkg/util/hashcache"
  22. "yunion.io/x/onecloud/pkg/util/rbacutils"
  23. )
  24. var (
  25. usageCache = hashcache.NewCache(1024, time.Second*300) // 5 minutes, 1024 buckets cache
  26. )
  27. func getCacheKey(
  28. scope rbacscope.TRbacScope,
  29. userCred mcclient.IIdentityProvider,
  30. isOwner bool,
  31. rangeObjs []db.IStandaloneModel,
  32. hostTypes []string,
  33. providers []string,
  34. brands []string,
  35. cloudEnv string,
  36. includeSystem bool,
  37. policyResult rbacutils.SPolicyResult,
  38. ) string {
  39. type RangeObject struct {
  40. Resource string `json:"resource"`
  41. Id string `json:"id"`
  42. }
  43. type KeyStruct struct {
  44. Scope rbacscope.TRbacScope `json:"scope"`
  45. Domain string `json:"domain"`
  46. Project string `json:"project"`
  47. IsOwner bool `json:"is_owner"`
  48. Ranges []RangeObject `json:"ranges"`
  49. HostTypes []string `json:"host_types"`
  50. Providers []string `json:"providers"`
  51. Brands []string `json:"brands"`
  52. CloudEnv string `json:"cloud_env"`
  53. System bool `json:"system"`
  54. PolicyResult rbacutils.SPolicyResult `json:"policy_result"`
  55. }
  56. key := KeyStruct{}
  57. key.Scope = scope
  58. switch scope {
  59. case rbacscope.ScopeSystem:
  60. case rbacscope.ScopeDomain:
  61. key.Domain = userCred.GetProjectDomainId()
  62. case rbacscope.ScopeProject:
  63. key.Project = userCred.GetProjectId()
  64. }
  65. if isOwner {
  66. key.IsOwner = true
  67. }
  68. for _, obj := range rangeObjs {
  69. robj := RangeObject{
  70. Resource: obj.Keyword(),
  71. Id: obj.GetId(),
  72. }
  73. key.Ranges = append(key.Ranges, robj)
  74. }
  75. key.HostTypes = hostTypes
  76. key.Providers = providers
  77. key.Brands = brands
  78. key.CloudEnv = cloudEnv
  79. key.System = includeSystem
  80. key.PolicyResult = policyResult
  81. jsonObj := jsonutils.Marshal(key)
  82. return jsonObj.QueryString()
  83. }