-
Notifications
You must be signed in to change notification settings - Fork 28
/
broker_notifications_interceptor.go
184 lines (156 loc) · 6.1 KB
/
broker_notifications_interceptor.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package interceptors
import (
"context"
"fmt"
"github.com/Peripli/service-manager/pkg/log"
"github.com/Peripli/service-manager/pkg/query"
"github.com/Peripli/service-manager/pkg/types"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/storage"
"github.com/Peripli/service-manager/storage/catalog"
"github.com/Peripli/service-manager/storage/service_plans"
"time"
)
func NewBrokerNotificationsInterceptor(tenantKey string, notificationsKeepFor time.Duration) *NotificationsInterceptor {
return &NotificationsInterceptor{
PlatformIDsProviderFunc: func(ctx context.Context, obj types.Object, repository storage.Repository) ([]string, error) {
broker := obj.(*types.ServiceBroker)
var err error
plans := make([]*types.ServicePlan, 0)
if len(broker.Services) == 0 { // broker create/update might be triggered inside an existing transaction, which will result in not loading the broker catalog
plans, err = fetchBrokerPlans(ctx, broker.ID, repository)
if err != nil {
return nil, err
}
} else {
for _, svc := range broker.Services {
plans = append(plans, svc.Plans...)
}
}
var supportedPlatforms map[string]*types.Platform
if tenantIDValues, found := broker.Labels[tenantKey]; found && len(tenantIDValues) > 0 {
// tenant-scoped broker
supportedPlatforms, err = service_plans.ResolveSupportedPlatformsForTenant(ctx, plans, repository, tenantKey, tenantIDValues[0])
} else {
// global broker
supportedPlatforms, err = service_plans.ResolveSupportedPlatformsForPlans(ctx, plans, repository)
}
if err != nil {
return nil, err
}
supportedPlatformIDs := make([]string, 0)
for id, platform := range supportedPlatforms {
if !platform.Technical && (platform.Active || platform.LastActive.After(time.Now().Add(-notificationsKeepFor))) {
// only platforms that are active or were active in the time period we keep notifications for are notified
supportedPlatformIDs = append(supportedPlatformIDs, id)
}
}
return removeSMPlatform(supportedPlatformIDs), nil
},
AdditionalDetailsFunc: func(ctx context.Context, objects types.ObjectList, repository storage.Repository) (objectDetails, error) {
details := make(objectDetails, objects.Len())
for i := 0; i < objects.Len(); i++ {
broker := objects.ItemAt(i).(*types.ServiceBroker)
services := broker.Services
if len(services) == 0 {
var err error
serviceOfferings, err := catalog.Load(ctx, broker.ID, repository)
if err != nil {
return nil, err
}
services = serviceOfferings.ServiceOfferings
}
details[broker.ID] = &BrokerAdditional{
Services: services,
}
}
return details, nil
},
DeletePostConditionFunc: func(ctx context.Context, object types.Object, repository storage.Repository, platformID string) error {
criteria := []query.Criterion{
query.ByField(query.EqualsOperator, "broker_id", object.GetID()),
query.ByField(query.EqualsOperator, "platform_id", platformID),
}
log.C(ctx).Debugf("Proceeding with deletion of broker platform credentials for broker with id %s and platform with id %s", object.GetID(), platformID)
if err := repository.Delete(ctx, types.BrokerPlatformCredentialType, criteria...); err != nil {
if err != util.ErrNotFoundInStorage {
return err
}
}
return nil
},
}
}
func removeSMPlatform(platforms []string) []string {
for i := range platforms {
if platforms[i] == types.SMPlatform {
platforms[i] = platforms[len(platforms)-1]
return platforms[:len(platforms)-1]
}
}
return platforms
}
type BrokerAdditional struct {
Services []*types.ServiceOffering `json:"services,omitempty"`
}
func (ba BrokerAdditional) Validate() error {
if len(ba.Services) == 0 {
return fmt.Errorf("broker details services cannot be empty")
}
return nil
}
const (
BrokerCreateNotificationInterceptorName = "BrokerNotificationsCreateInterceptorProvider"
BrokerUpdateNotificationInterceptorName = "BrokerNotificationsUpdateInterceptorProvider"
BrokerDeleteNotificationInterceptorName = "BrokerNotificationsDeleteInterceptorProvider"
)
type BrokerNotificationsCreateInterceptorProvider struct {
NotificationsKeepFor time.Duration
TenantKey string
}
func (*BrokerNotificationsCreateInterceptorProvider) Name() string {
return BrokerCreateNotificationInterceptorName
}
func (b *BrokerNotificationsCreateInterceptorProvider) Provide() storage.CreateOnTxInterceptor {
return NewBrokerNotificationsInterceptor(b.TenantKey, b.NotificationsKeepFor)
}
type BrokerNotificationsUpdateInterceptorProvider struct {
NotificationsKeepFor time.Duration
TenantKey string
}
func (*BrokerNotificationsUpdateInterceptorProvider) Name() string {
return BrokerUpdateNotificationInterceptorName
}
func (b *BrokerNotificationsUpdateInterceptorProvider) Provide() storage.UpdateOnTxInterceptor {
return NewBrokerNotificationsInterceptor(b.TenantKey, b.NotificationsKeepFor)
}
type BrokerNotificationsDeleteInterceptorProvider struct {
NotificationsKeepFor time.Duration
TenantKey string
}
func (*BrokerNotificationsDeleteInterceptorProvider) Name() string {
return BrokerDeleteNotificationInterceptorName
}
func (b *BrokerNotificationsDeleteInterceptorProvider) Provide() storage.DeleteOnTxInterceptor {
return NewBrokerNotificationsInterceptor(b.TenantKey, b.NotificationsKeepFor)
}
func fetchBrokerPlans(ctx context.Context, brokerID string, repository storage.Repository) ([]*types.ServicePlan, error) {
byBrokerID := query.ByField(query.EqualsOperator, "broker_id", brokerID)
objList, err := repository.List(ctx, types.ServiceOfferingType, byBrokerID)
if err != nil {
return nil, err
}
if objList.Len() == 0 {
return nil, nil
}
serviceOfferingIDs := make([]string, 0)
for i := 0; i < objList.Len(); i++ {
serviceOfferingIDs = append(serviceOfferingIDs, objList.ItemAt(i).GetID())
}
byOfferingIDs := query.ByField(query.InOperator, "service_offering_id", serviceOfferingIDs...)
objList, err = repository.List(ctx, types.ServicePlanType, byOfferingIDs)
if err != nil {
return nil, err
}
return objList.(*types.ServicePlans).ServicePlans, nil
}