domain.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 cachesync
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  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. "yunion.io/x/onecloud/pkg/util/ctx"
  27. )
  28. type domainCacheSyncWorker struct {
  29. ids []string
  30. }
  31. func (w *domainCacheSyncWorker) Run() {
  32. log.Debugf("[domainCacheSyncWorker] Run domain cache sync worker ...")
  33. err := syncDomains(ctx.CtxWithTime(), w.ids)
  34. if err != nil {
  35. log.Errorf("fail to syncDomains %s", err)
  36. }
  37. }
  38. func (w *domainCacheSyncWorker) Dump() string {
  39. return "domainCacheSyncWorker"
  40. }
  41. func syncDomains(ctx context.Context, ids []string) error {
  42. s := auth.GetAdminSession(ctx, consts.GetRegion())
  43. query := jsonutils.NewDict()
  44. query.Add(jsonutils.NewInt(1024), "limit")
  45. query.Add(jsonutils.NewString(string(rbacscope.ScopeSystem)), "scope")
  46. query.Add(jsonutils.JSONTrue, "details")
  47. query.Add(jsonutils.NewString("all"), "pending_delete")
  48. query.Add(jsonutils.NewString("all"), "delete")
  49. if len(ids) > 0 {
  50. query.Add(jsonutils.NewStringArray(ids), "id")
  51. }
  52. total := -1
  53. offset := 0
  54. for total < 0 || offset < total {
  55. query.Set("offset", jsonutils.NewInt(int64(offset)))
  56. results, err := modules.Domains.List(s, query)
  57. if err != nil {
  58. return errors.Wrap(err, "Domains.List")
  59. }
  60. total = results.Total
  61. for i := range results.Data {
  62. // update domain cache
  63. item := db.SCachedTenant{}
  64. deleted := jsonutils.QueryBoolean(results.Data[i], "deleted", false)
  65. err := results.Data[i].Unmarshal(&item)
  66. if err == nil && !deleted {
  67. item.ProjectDomain = identityapi.KeystoneDomainRoot
  68. item.DomainId = identityapi.KeystoneDomainRoot
  69. db.TenantCacheManager.Save(ctx, item, true)
  70. } else if deleted {
  71. tenantObj, _ := db.TenantCacheManager.FetchById(item.Id)
  72. if tenantObj != nil {
  73. tenantObj.Delete(ctx, nil)
  74. }
  75. }
  76. offset++
  77. }
  78. }
  79. return nil
  80. }