forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceinstance.go
71 lines (58 loc) · 1.91 KB
/
serviceinstance.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
60
61
62
63
64
65
66
67
68
69
70
71
package listener
import (
"fmt"
"github.com/golang/glog"
api "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/kyma-project/kyma/components/console-backend-service/internal/gqlschema"
"github.com/pkg/errors"
)
//go:generate mockery -name=gqlInstanceConverter -output=automock -outpkg=automock -case=underscore
type gqlInstanceConverter interface {
ToGQL(in *api.ServiceInstance) (*gqlschema.ServiceInstance, error)
}
type Instance struct {
channel chan<- gqlschema.ServiceInstanceEvent
filter func(instance *api.ServiceInstance) bool
converter gqlInstanceConverter
}
func NewInstance(channel chan<- gqlschema.ServiceInstanceEvent, filter func(instance *api.ServiceInstance) bool, converter gqlInstanceConverter) *Instance {
return &Instance{
channel: channel,
filter: filter,
converter: converter,
}
}
func (l *Instance) OnAdd(object interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeAdd, object)
}
func (l *Instance) OnUpdate(oldObject, newObject interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeUpdate, newObject)
}
func (l *Instance) OnDelete(object interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeDelete, object)
}
func (l *Instance) onEvent(eventType gqlschema.SubscriptionEventType, object interface{}) {
instance, ok := object.(*api.ServiceInstance)
if !ok {
glog.Error(fmt.Errorf("incorrect object type: %T, should be: *ServiceInstance", object))
return
}
if l.filter(instance) {
l.notify(eventType, instance)
}
}
func (l *Instance) notify(eventType gqlschema.SubscriptionEventType, instance *api.ServiceInstance) {
gqlInstance, err := l.converter.ToGQL(instance)
if err != nil {
glog.Error(errors.Wrapf(err, "while converting *ServiceInstance"))
return
}
if gqlInstance == nil {
return
}
event := gqlschema.ServiceInstanceEvent{
Type: eventType,
ServiceInstance: *gqlInstance,
}
l.channel <- event
}