informer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 informer
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. )
  21. const (
  22. ErrBackendNotInit = errors.Error("InformerBackend not init")
  23. )
  24. var (
  25. defaultBackend IInformerBackend
  26. )
  27. type IInformerBackend interface {
  28. GetType() string
  29. Create(ctx context.Context, obj *ModelObject) error
  30. Update(ctx context.Context, obj *ModelObject, oldObj *jsonutils.JSONDict) error
  31. Delete(ctx context.Context, obj *ModelObject) error
  32. }
  33. func Init(be IInformerBackend) {
  34. if defaultBackend != nil {
  35. log.Fatalf("informer backend %q already init", be.GetType())
  36. }
  37. defaultBackend = be
  38. }
  39. func Set(be IInformerBackend) {
  40. defaultBackend = be
  41. }
  42. func GetDefaultBackend() IInformerBackend {
  43. if defaultBackend == nil {
  44. log.V(10).Warningf("default informer backend is not init")
  45. }
  46. return defaultBackend
  47. }
  48. func IsInit() bool {
  49. return defaultBackend != nil
  50. }
  51. type ModelObject struct {
  52. Object *jsonutils.JSONDict
  53. KeywordPlural string
  54. Id string
  55. IsJoint bool
  56. MasterId string
  57. SlaveId string
  58. }
  59. func NewModel(obj interface{}, keywordPlural, id string) *ModelObject {
  60. return &ModelObject{
  61. Object: jsonutils.Marshal(obj).(*jsonutils.JSONDict),
  62. KeywordPlural: keywordPlural,
  63. Id: id,
  64. }
  65. }
  66. func NewJointModel(obj interface{}, keywordPlural, masterId, slaveId string) *ModelObject {
  67. model := NewModel(obj, keywordPlural, "")
  68. model.IsJoint = true
  69. model.MasterId = masterId
  70. model.SlaveId = slaveId
  71. return model
  72. }
  73. func isResourceWatched(keywordPlural string) bool {
  74. return GetWatchResources().Has(keywordPlural)
  75. }
  76. func Create(ctx context.Context, obj *ModelObject) error {
  77. if !isResourceWatched(obj.KeywordPlural) {
  78. return nil
  79. }
  80. return run(ctx, func(ctx context.Context, be IInformerBackend) error {
  81. return be.Create(ctx, obj)
  82. })
  83. }
  84. func Update(ctx context.Context, obj *ModelObject, oldObj *jsonutils.JSONDict) error {
  85. if !isResourceWatched(obj.KeywordPlural) {
  86. return nil
  87. }
  88. return run(ctx, func(ctx context.Context, be IInformerBackend) error {
  89. return be.Update(ctx, obj, oldObj)
  90. })
  91. }
  92. func Delete(ctx context.Context, obj *ModelObject) error {
  93. if !isResourceWatched(obj.KeywordPlural) {
  94. return nil
  95. }
  96. return run(ctx, func(ctx context.Context, be IInformerBackend) error {
  97. return be.Delete(ctx, obj)
  98. })
  99. }