notify_hook.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 db
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. )
  20. var (
  21. updateNotifyHook updateNotifyHookFunc
  22. statusChangedNotifyHook statusChangedNotifyHookFunc
  23. customizeNotifyHook customizeNotifyHookFunc
  24. )
  25. type updateNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, obj IModel)
  26. type statusChangedNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, oldStatus, status string, obj IModel)
  27. type customizeNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, action string, obj IModel, moreDetails jsonutils.JSONObject)
  28. func SetUpdateNotifyHook(f updateNotifyHookFunc) {
  29. if updateNotifyHook != nil {
  30. panic("updateNotifyHook already set")
  31. }
  32. updateNotifyHook = f
  33. }
  34. func CallUpdateNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) {
  35. if updateNotifyHook == nil {
  36. return
  37. }
  38. updateNotifyHook(ctx, userCred, obj)
  39. }
  40. func SetCustomizeNotifyHook(f customizeNotifyHookFunc) {
  41. if customizeNotifyHook != nil {
  42. panic("updateNotifyHook already set")
  43. }
  44. customizeNotifyHook = f
  45. }
  46. func CallCustomizeNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, action string, obj IModel, customizeDetails jsonutils.JSONObject) {
  47. if customizeNotifyHook == nil {
  48. return
  49. }
  50. customizeNotifyHook(ctx, userCred, action, obj, customizeDetails)
  51. }
  52. func SetStatusChangedNotifyHook(f statusChangedNotifyHookFunc) {
  53. if statusChangedNotifyHook != nil {
  54. panic("updateNotifyHook already set")
  55. }
  56. statusChangedNotifyHook = f
  57. }
  58. func CallStatusChanegdNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, oldStatis, newStatus string, obj IModel) {
  59. if statusChangedNotifyHook == nil {
  60. return
  61. }
  62. statusChangedNotifyHook(ctx, userCred, oldStatis, newStatus, obj)
  63. }