project.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/mcclient/auth"
  24. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  25. "yunion.io/x/onecloud/pkg/util/ctx"
  26. )
  27. type tenantCacheSyncWorker struct {
  28. ids []string
  29. }
  30. func (w *tenantCacheSyncWorker) Run() {
  31. log.Debugf("[tenantCacheSyncWorker] Run project cache sync worker ...")
  32. err := syncProjects(ctx.CtxWithTime(), w.ids)
  33. if err != nil {
  34. log.Errorf("fail to syncProjects %s", err)
  35. }
  36. }
  37. func (w *tenantCacheSyncWorker) Dump() string {
  38. return "tenantCacheSyncWorker"
  39. }
  40. func syncProjects(ctx context.Context, ids []string) error {
  41. s := auth.GetAdminSession(ctx, consts.GetRegion())
  42. query := jsonutils.NewDict()
  43. query.Add(jsonutils.NewInt(1024), "limit")
  44. query.Add(jsonutils.NewString(string(rbacscope.ScopeSystem)), "scope")
  45. query.Add(jsonutils.JSONTrue, "details")
  46. query.Add(jsonutils.NewString("all"), "pending_delete")
  47. query.Add(jsonutils.NewString("all"), "delete")
  48. if len(ids) > 0 {
  49. log.Debugf("to syncProjects for %s", jsonutils.Marshal(ids))
  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.Projects.List(s, query)
  57. if err != nil {
  58. return errors.Wrap(err, "Projects.List")
  59. }
  60. total = results.Total
  61. for i := range results.Data {
  62. // update project 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. db.TenantCacheManager.Save(ctx, item, true)
  68. } else if deleted {
  69. tenantObj, _ := db.TenantCacheManager.FetchById(item.Id)
  70. if tenantObj != nil {
  71. tenantObj.Delete(ctx, nil)
  72. }
  73. }
  74. offset++
  75. }
  76. }
  77. return nil
  78. }