sync.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/log"
  18. api "yunion.io/x/onecloud/pkg/apis/identity"
  19. "yunion.io/x/onecloud/pkg/keystone/driver"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. )
  22. func AutoSyncIdentityProviderTask(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
  23. idps, err := IdentityProviderManager.FetchEnabledProviders("")
  24. if err != nil {
  25. log.Errorf("FetchEnabledProviders fail %s", err)
  26. return
  27. }
  28. if isStart {
  29. for i := range idps {
  30. idps[i].SetSyncStatus(ctx, userCred, api.IdentitySyncStatusIdle)
  31. }
  32. }
  33. for i := range idps {
  34. err = syncIdentityProvider(ctx, userCred, &idps[i])
  35. if err != nil {
  36. log.Errorf("Fail to sync identityprovider %s: %s", idps[i].Name, err)
  37. }
  38. }
  39. }
  40. func syncIdentityProvider(ctx context.Context, userCred mcclient.TokenCredential, idp *SIdentityProvider) error {
  41. if idp.SyncStatus != api.IdentitySyncStatusIdle {
  42. log.Debugf("IDP %s cannot sync in non-idle status", idp.Name)
  43. return nil
  44. }
  45. if !idp.CanSync() {
  46. log.Debugf("IDP %s cannot sync", idp.Name)
  47. return nil
  48. }
  49. if !idp.NeedSync() {
  50. // log.Debugf("IDP %s no need to sync", idp.Name)
  51. return nil
  52. }
  53. drvCls := driver.GetDriverClass(idp.Driver)
  54. if drvCls.SyncMethod() == api.IdentityProviderSyncLocal {
  55. // skip, no need to sync
  56. log.Debugf("IDP %s is local, no need to sync", idp.Name)
  57. return nil
  58. }
  59. if drvCls.SyncMethod() == api.IdentityProviderSyncOnAuth {
  60. log.Debugf("IDP %s sync on auth, no need to sync", idp.Name)
  61. return nil
  62. }
  63. submitIdpSyncTask(ctx, userCred, idp)
  64. return nil
  65. }