watch.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 shell
  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/sets"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/mcclient/informer"
  23. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  24. )
  25. type eventHandler struct {
  26. man informer.IResourceManager
  27. }
  28. func (e eventHandler) keyword() string {
  29. return e.man.GetKeyword()
  30. }
  31. func (e eventHandler) OnAdd(obj *jsonutils.JSONDict) {
  32. log.Infof("%s [CREATED]: \n%s", e.keyword(), obj.String())
  33. }
  34. func (e eventHandler) OnUpdate(oldObj, newObj *jsonutils.JSONDict) {
  35. log.Infof("%s [UPDATED]: \n[NEW]: %s\n[OLD]: %s", e.keyword(), newObj.String(), oldObj.String())
  36. }
  37. func (e eventHandler) OnDelete(obj *jsonutils.JSONDict) {
  38. log.Infof("%s [DELETED]: \n%s", e.keyword(), obj.String())
  39. }
  40. func init() {
  41. type WatchOptions struct {
  42. Resource []string `help:"Resource manager plural keyword, e.g.'servers, disks, guestdisks'" short-token:"s"`
  43. All bool `help:"Watch all resources"`
  44. }
  45. R(&WatchOptions{}, "watch", "Watch resources", func(s *mcclient.ClientSession, opts *WatchOptions) error {
  46. watchMan, err := informer.NewWatchManagerBySession(s)
  47. if err != nil {
  48. return err
  49. }
  50. resources := opts.Resource
  51. if opts.All {
  52. resSets := sets.NewString()
  53. mods, _ := modulebase.GetRegisterdModules()
  54. resSets.Insert(mods...)
  55. resources = resSets.List()
  56. }
  57. if len(resources) == 0 {
  58. return errors.Errorf("no watch resources specified")
  59. }
  60. for _, res := range resources {
  61. var resMan informer.IResourceManager
  62. if modMan, _ := modulebase.GetModule(s, res); modMan != nil {
  63. resMan = modMan
  64. }
  65. if resMan == nil {
  66. if jointModMan, _ := modulebase.GetJointModule(s, res); jointModMan != nil {
  67. resMan = jointModMan
  68. }
  69. }
  70. if resMan == nil {
  71. //return errors.Errorf("Not found %q resource manager", res)
  72. log.Warningf("Not found %q resource manager", res)
  73. continue
  74. }
  75. if err := watchMan.For(resMan).AddEventHandler(context.Background(), eventHandler{resMan}); err != nil {
  76. return errors.Wrapf(err, "watch resource %s", res)
  77. }
  78. }
  79. select {}
  80. })
  81. }