This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
catalog.go
261 lines (226 loc) · 6.75 KB
/
catalog.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package service
import (
"encoding/json"
)
// Catalog is an interface to be implemented by types that represents the
// service/plans offered by a service module or by the entire broker.
type Catalog interface {
GetServices() []Service
GetService(serviceID string) (Service, bool)
}
type catalog struct {
services []Service
indexedServices map[string]Service
}
// ServiceProperties represent the properties of a Service that can be directly
// instantiated and passed to the NewService() constructor function which will
// carry out all necessary initialization.
type ServiceProperties struct { // nolint: golint
Name string `json:"name"`
ID string `json:"id"`
Description string `json:"description"`
Metadata ServiceMetadata `json:"metadata,omitempty"`
Tags []string `json:"tags"`
Bindable bool `json:"bindable"`
PlanUpdatable bool `json:"plan_updateable"` // Misspelling is
// deliberate to match the spec
ParentServiceID string `json:"-"`
ChildServiceID string `json:"-"`
Extended map[string]interface{} `json:"-"`
EndOfLife bool `json:"-"`
}
// ServiceMetadata contains metadata about the service classes
type ServiceMetadata struct { // nolint: golint
DisplayName string `json:"displayName,omitempty"`
ImageURL string `json:"imageUrl,omitempty"`
LongDescription string `json:"longDescription,omitempty"`
ProviderDisplayName string `json:"providerDisplayName,omitempty"`
DocumentationURL string `json:"documentationUrl,omitempty"`
SupportURL string `json:"supportUrl,omitempty"`
}
// Service is an interface to be implemented by types that represent a single
// type of service with one or more plans
type Service interface {
GetID() string
GetName() string
IsBindable() bool
GetServiceManager() ServiceManager
GetPlans() []Plan
GetPlan(planID string) (Plan, bool)
GetParentServiceID() string
GetChildServiceID() string
GetProperties() ServiceProperties
GetTags() []string
IsEndOfLife() bool
}
type service struct {
ServiceProperties
serviceManager ServiceManager
indexedPlans map[string]Plan
plans []Plan
}
// PlanProperties represent the properties of a Plan that can be directly
// instantiated and passed to the NewPlan() constructor function which will
// carry out all necessary initialization.
type PlanProperties struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Free bool `json:"free"`
Metadata ServicePlanMetadata `json:"metadata,omitempty"` // nolint: lll
Extended map[string]interface{} `json:"-"`
EndOfLife bool `json:"-"`
Schemas PlanSchemas `json:"schemas,omitempty"`
Stability Stability `json:"-"`
}
// ServicePlanMetadata contains metadata about the service plans
type ServicePlanMetadata struct { // nolint: golint
DisplayName string `json:"displayName,omitempty"`
Bullets []string `json:"bullets,omitempty"`
}
// Plan is an interface to be implemented by types that represent a single
// variant or "sku" of a service
type Plan interface {
GetID() string
GetName() string
GetProperties() PlanProperties
IsEndOfLife() bool
GetSchemas() PlanSchemas
GetStability() Stability
}
type plan struct {
PlanProperties
}
// NewCatalog initializes and returns a new Catalog
func NewCatalog(services []Service) Catalog {
c := &catalog{
services: services,
indexedServices: make(map[string]Service),
}
for _, service := range services {
c.indexedServices[service.GetID()] = service
}
return c
}
// ToJSON returns a []byte containing a JSON representation of the catalog
func (c *catalog) MarshalJSON() ([]byte, error) {
// filter out the EOL services. When MarshalJson is called
// they won't be present in the slice
nonEOLServices := []Service{}
for _, service := range c.services {
if !service.IsEndOfLife() {
nonEOLServices = append(nonEOLServices, service)
}
}
return json.Marshal(struct {
Services []Service `json:"services"`
}{
Services: nonEOLServices,
})
}
// GetServices returns all of the catalog's services
func (c *catalog) GetServices() []Service {
return c.services
}
// GetService finds a service by serviceID in a catalog
// TODO: Test this
func (c *catalog) GetService(serviceID string) (Service, bool) {
service, ok := c.indexedServices[serviceID]
return service, ok
}
// NewService initialized and returns a new Service
func NewService(
serviceProperties ServiceProperties,
serviceManager ServiceManager,
plans ...Plan,
) Service {
s := service{
ServiceProperties: serviceProperties,
serviceManager: serviceManager,
plans: plans,
indexedPlans: make(map[string]Plan),
}
for _, p := range s.plans {
s.indexedPlans[p.GetID()] = p
}
return s
}
func (s service) MarshalJSON() ([]byte, error) {
// filter out the EOL plans. When MarshalJson is called
// they won't be present in the slice
nonEOLPlans := []Plan{}
for _, plan := range s.plans {
if !plan.IsEndOfLife() {
nonEOLPlans = append(nonEOLPlans, plan)
}
}
return json.Marshal(struct {
ServiceProperties
Plans []Plan `json:"plans"`
}{
ServiceProperties: s.GetProperties(),
Plans: nonEOLPlans,
})
}
func (s service) GetID() string {
return s.ID
}
func (s service) GetName() string {
return s.Name
}
// IsBindable returns true if a service is bindable
func (s service) IsBindable() bool {
return s.Bindable
}
func (s service) GetServiceManager() ServiceManager {
return s.serviceManager
}
// GetPlans returns all of the service's plans
func (s service) GetPlans() []Plan {
return s.plans
}
// GetPlan finds a plan by planID in a service
// TODO: Test this
func (s service) GetPlan(planID string) (Plan, bool) {
plan, ok := s.indexedPlans[planID]
return plan, ok
}
func (s service) GetParentServiceID() string {
return s.ParentServiceID
}
func (s service) GetChildServiceID() string {
return s.ChildServiceID
}
func (s service) GetProperties() ServiceProperties {
return s.ServiceProperties
}
func (s service) GetTags() []string {
return s.Tags
}
func (s service) IsEndOfLife() bool {
return s.EndOfLife
}
// NewPlan initializes and returns a new Plan
func NewPlan(planProperties PlanProperties) Plan {
return plan{
PlanProperties: planProperties,
}
}
func (p plan) GetID() string {
return p.ID
}
func (p plan) GetName() string {
return p.Name
}
func (p plan) GetProperties() PlanProperties {
return p.PlanProperties
}
func (p plan) IsEndOfLife() bool {
return p.EndOfLife
}
func (p plan) GetSchemas() PlanSchemas {
return p.Schemas
}
func (p plan) GetStability() Stability {
return p.Stability
}