forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_listener.go
59 lines (48 loc) · 1.61 KB
/
instance_listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package servicecatalog
import (
"fmt"
"github.com/golang/glog"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/gqlschema"
)
// TODO: Move to subpackage
type instanceListener struct {
channel chan<- gqlschema.ServiceInstanceEvent
filter func(object interface{}) bool
instanceConverter gqlInstanceConverter
}
func newInstanceListener(channel chan<- gqlschema.ServiceInstanceEvent, filter func(object interface{}) bool, instanceConverter gqlInstanceConverter) *instanceListener {
return &instanceListener{
channel: channel,
filter: filter,
instanceConverter: instanceConverter,
}
}
func (il *instanceListener) OnAdd(object interface{}) {
if il.filter(object) {
il.notify(gqlschema.ServiceInstanceEventTypeAdd, object)
}
}
func (il *instanceListener) OnUpdate(oldObject, newObject interface{}) {
if il.filter(newObject) {
il.notify(gqlschema.ServiceInstanceEventTypeUpdate, newObject)
}
}
func (il *instanceListener) OnDelete(object interface{}) {
if il.filter(object) {
il.notify(gqlschema.ServiceInstanceEventTypeDelete, object)
}
}
func (il *instanceListener) notify(eventType gqlschema.ServiceInstanceEventType, object interface{}) {
instance, ok := object.(*v1beta1.ServiceInstance)
if !ok {
glog.Error(fmt.Errorf("Incorrect object type: %T, should be: *ServiceInstance", object))
return
}
gqlInstance := il.instanceConverter.ToGQL(instance)
event := gqlschema.ServiceInstanceEvent{
Type: eventType,
Instance: *gqlInstance,
}
il.channel <- event
}