fetchnames.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 utils
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/util/rbacscope"
  21. identityapi "yunion.io/x/onecloud/pkg/apis/identity"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  26. )
  27. func MapKeys(idMap map[string]string) []string {
  28. keys := make([]string, len(idMap))
  29. idx := 0
  30. for k := range idMap {
  31. keys[idx] = k
  32. idx += 1
  33. }
  34. return keys
  35. }
  36. func FetchDomainNames(ctx context.Context, domainMap map[string]string) (map[string]string, error) {
  37. s := auth.GetAdminSession(ctx, consts.GetRegion())
  38. query := jsonutils.NewDict()
  39. query.Add(jsonutils.NewString(fmt.Sprintf("id.equals(%s)", strings.Join(MapKeys(domainMap), ","))), "filter.0")
  40. query.Add(jsonutils.NewInt(int64(len(domainMap))), "limit")
  41. query.Add(jsonutils.NewString(string(rbacscope.ScopeSystem)), "scope")
  42. query.Add(jsonutils.JSONTrue, "details")
  43. results, err := modules.Domains.List(s, query)
  44. if err == nil {
  45. for i := range results.Data {
  46. // update domain cache
  47. item := db.SCachedTenant{}
  48. results.Data[i].Unmarshal(&item)
  49. item.ProjectDomain = identityapi.KeystoneDomainRoot
  50. item.DomainId = identityapi.KeystoneDomainRoot
  51. db.TenantCacheManager.Save(ctx, item, true)
  52. domainMap[item.Id] = item.Name
  53. }
  54. for k, v := range domainMap {
  55. if len(v) == 0 {
  56. db.TenantCacheManager.Delete(ctx, k)
  57. }
  58. }
  59. return domainMap, nil
  60. } else {
  61. return domainMap, err
  62. }
  63. }
  64. func FetchTenantNames(ctx context.Context, tenantMap map[string]string) (map[string]string, error) {
  65. s := auth.GetAdminSession(ctx, consts.GetRegion())
  66. query := jsonutils.NewDict()
  67. query.Add(jsonutils.NewString(fmt.Sprintf("id.equals(%s)", strings.Join(MapKeys(tenantMap), ","))), "filter.0")
  68. query.Add(jsonutils.NewInt(int64(len(tenantMap))), "limit")
  69. query.Add(jsonutils.NewString(string(rbacscope.ScopeSystem)), "scope")
  70. query.Add(jsonutils.JSONTrue, "details")
  71. results, err := modules.Projects.List(s, query)
  72. if err == nil {
  73. for i := range results.Data {
  74. // update project cache
  75. item := db.SCachedTenant{}
  76. results.Data[i].Unmarshal(&item)
  77. db.TenantCacheManager.Save(ctx, item, true)
  78. tenantMap[item.Id] = item.Name
  79. }
  80. for k, v := range tenantMap {
  81. if len(v) == 0 {
  82. db.TenantCacheManager.Delete(ctx, k)
  83. }
  84. }
  85. return tenantMap, nil
  86. } else {
  87. return tenantMap, err
  88. }
  89. }