-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicebinding_usage.go
75 lines (63 loc) · 2.3 KB
/
servicebinding_usage.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
72
73
74
75
package listener
import (
"github.com/golang/glog"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/servicecatalogaddons/extractor"
"github.com/kyma-project/kyma/components/console-backend-service/internal/gqlschema"
api "github.com/kyma-project/kyma/components/service-binding-usage-controller/pkg/apis/servicecatalog/v1alpha1"
"github.com/pkg/errors"
)
//go:generate mockery -name=gqlBindingUsageConverter -output=automock -outpkg=automock -case=underscore
type gqlBindingUsageConverter interface {
ToGQL(in *api.ServiceBindingUsage) (*gqlschema.ServiceBindingUsage, error)
}
type BindingUsage struct {
channel chan<- gqlschema.ServiceBindingUsageEvent
filter func(bindingUsage *api.ServiceBindingUsage) bool
converter gqlBindingUsageConverter
extractor extractor.BindingUsageUnstructuredExtractor
}
func NewBindingUsage(channel chan<- gqlschema.ServiceBindingUsageEvent, filter func(bindingUsage *api.ServiceBindingUsage) bool, converter gqlBindingUsageConverter) *BindingUsage {
return &BindingUsage{
channel: channel,
filter: filter,
converter: converter,
extractor: extractor.BindingUsageUnstructuredExtractor{},
}
}
func (l *BindingUsage) OnAdd(object interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeAdd, object)
}
func (l *BindingUsage) OnUpdate(oldObject, newObject interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeUpdate, newObject)
}
func (l *BindingUsage) OnDelete(object interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeDelete, object)
}
func (l *BindingUsage) onEvent(eventType gqlschema.SubscriptionEventType, object interface{}) {
bindingUsage, err := l.extractor.Do(object)
if err != nil {
glog.Error(errors.New("cannot extract *ServiceBindingUsage"))
return
}
if bindingUsage == nil {
return
}
if l.filter(bindingUsage) {
l.notify(eventType, bindingUsage)
}
}
func (l *BindingUsage) notify(eventType gqlschema.SubscriptionEventType, bindingUsage *api.ServiceBindingUsage) {
gqlBindingUsage, err := l.converter.ToGQL(bindingUsage)
if err != nil {
glog.Error(errors.Wrapf(err, "while converting *ServiceBindingUsage"))
return
}
if gqlBindingUsage == nil {
return
}
event := gqlschema.ServiceBindingUsageEvent{
Type: eventType,
ServiceBindingUsage: *gqlBindingUsage,
}
l.channel <- event
}