forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_builder.go
314 lines (255 loc) · 9.26 KB
/
service_builder.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package servicebuilder
import (
"errors"
"code.cloudfoundry.org/cli/cf/actors/planbuilder"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/models"
)
//go:generate counterfeiter . ServiceBuilder
type ServiceBuilder interface {
GetAllServices() ([]models.ServiceOffering, error)
GetAllServicesWithPlans() ([]models.ServiceOffering, error)
GetServiceByNameWithPlans(string) (models.ServiceOffering, error)
GetServiceByNameWithPlansWithOrgNames(string) (models.ServiceOffering, error)
GetServiceByNameForSpace(string, string) (models.ServiceOffering, error)
GetServiceByNameForSpaceWithPlans(string, string) (models.ServiceOffering, error)
GetServicesByNameForSpaceWithPlans(string, string) (models.ServiceOfferings, error)
GetServiceByNameForOrg(string, string) (models.ServiceOffering, error)
GetServicesForManyBrokers([]string) ([]models.ServiceOffering, error)
GetServicesForBroker(string) ([]models.ServiceOffering, error)
GetServicesForSpace(string) ([]models.ServiceOffering, error)
GetServicesForSpaceWithPlans(string) ([]models.ServiceOffering, error)
GetServiceVisibleToOrg(string, string) (models.ServiceOffering, error)
GetServicesVisibleToOrg(string) ([]models.ServiceOffering, error)
}
type Builder struct {
serviceRepo api.ServiceRepository
planBuilder planbuilder.PlanBuilder
}
func NewBuilder(service api.ServiceRepository, planBuilder planbuilder.PlanBuilder) Builder {
return Builder{
serviceRepo: service,
planBuilder: planBuilder,
}
}
func (builder Builder) GetAllServices() ([]models.ServiceOffering, error) {
return builder.serviceRepo.GetAllServiceOfferings()
}
func (builder Builder) GetAllServicesWithPlans() ([]models.ServiceOffering, error) {
services, err := builder.GetAllServices()
if err != nil {
return []models.ServiceOffering{}, err
}
var plans []models.ServicePlanFields
for index, service := range services {
plans, err = builder.planBuilder.GetPlansForService(service.GUID)
if err != nil {
return []models.ServiceOffering{}, err
}
services[index].Plans = plans
}
return services, err
}
func (builder Builder) GetServicesForSpace(spaceGUID string) ([]models.ServiceOffering, error) {
return builder.serviceRepo.GetServiceOfferingsForSpace(spaceGUID)
}
func (builder Builder) GetServicesForSpaceWithPlans(spaceGUID string) ([]models.ServiceOffering, error) {
services, err := builder.GetServicesForSpace(spaceGUID)
if err != nil {
return []models.ServiceOffering{}, err
}
for index, service := range services {
services[index].Plans, err = builder.planBuilder.GetPlansForService(service.GUID)
if err != nil {
return []models.ServiceOffering{}, err
}
}
return services, nil
}
func (builder Builder) GetServiceByNameWithPlans(serviceLabel string) (models.ServiceOffering, error) {
services, err := builder.serviceRepo.FindServiceOfferingsByLabel(serviceLabel)
if err != nil {
return models.ServiceOffering{}, err
}
service := returnV2Service(services)
service.Plans, err = builder.planBuilder.GetPlansForService(service.GUID)
if err != nil {
return models.ServiceOffering{}, err
}
return service, nil
}
func (builder Builder) GetServiceByNameForOrg(serviceLabel, orgName string) (models.ServiceOffering, error) {
services, err := builder.serviceRepo.FindServiceOfferingsByLabel(serviceLabel)
if err != nil {
return models.ServiceOffering{}, err
}
service, err := builder.attachPlansToServiceForOrg(services[0], orgName)
if err != nil {
return models.ServiceOffering{}, err
}
return service, nil
}
func (builder Builder) GetServiceByNameForSpace(serviceLabel, spaceGUID string) (models.ServiceOffering, error) {
offerings, err := builder.serviceRepo.FindServiceOfferingsForSpaceByLabel(spaceGUID, serviceLabel)
if err != nil {
return models.ServiceOffering{}, err
}
for _, offering := range offerings {
if offering.Provider == "" {
return offering, nil
}
}
return models.ServiceOffering{}, errors.New("Could not find service")
}
func (builder Builder) GetServiceByNameForSpaceWithPlans(serviceLabel, spaceGUID string) (models.ServiceOffering, error) {
offering, err := builder.GetServiceByNameForSpace(serviceLabel, spaceGUID)
if err != nil {
return models.ServiceOffering{}, err
}
offering.Plans, err = builder.planBuilder.GetPlansForService(offering.GUID)
if err != nil {
return models.ServiceOffering{}, err
}
return offering, nil
}
func (builder Builder) GetServicesByNameForSpaceWithPlans(serviceLabel, spaceGUID string) (models.ServiceOfferings, error) {
offerings, err := builder.serviceRepo.FindServiceOfferingsForSpaceByLabel(serviceLabel, spaceGUID)
if err != nil {
return models.ServiceOfferings{}, err
}
for index, offering := range offerings {
offerings[index].Plans, err = builder.planBuilder.GetPlansForService(offering.GUID)
if err != nil {
return models.ServiceOfferings{}, err
}
}
return offerings, nil
}
func (builder Builder) GetServiceByNameWithPlansWithOrgNames(serviceLabel string) (models.ServiceOffering, error) {
services, err := builder.serviceRepo.FindServiceOfferingsByLabel(serviceLabel)
if err != nil {
return models.ServiceOffering{}, err
}
service, err := builder.attachPlansToService(services[0])
if err != nil {
return models.ServiceOffering{}, err
}
return service, nil
}
func (builder Builder) GetServicesForManyBrokers(brokerGUIDs []string) ([]models.ServiceOffering, error) {
services, err := builder.serviceRepo.ListServicesFromManyBrokers(brokerGUIDs)
if err != nil {
return nil, err
}
return builder.populateServicesWithPlansAndOrgs(services)
}
func (builder Builder) GetServicesForBroker(brokerGUID string) ([]models.ServiceOffering, error) {
services, err := builder.serviceRepo.ListServicesFromBroker(brokerGUID)
if err != nil {
return nil, err
}
return builder.populateServicesWithPlansAndOrgs(services)
}
func (builder Builder) populateServicesWithPlansAndOrgs(services []models.ServiceOffering) ([]models.ServiceOffering, error) {
serviceGUIDs := []string{}
for _, service := range services {
serviceGUIDs = append(serviceGUIDs, service.GUID)
}
plans, err := builder.planBuilder.GetPlansForManyServicesWithOrgs(serviceGUIDs)
if err != nil {
return nil, err
}
return builder.attachPlansToManyServices(services, plans)
}
func (builder Builder) GetServiceVisibleToOrg(serviceName string, orgName string) (models.ServiceOffering, error) {
visiblePlans, err := builder.planBuilder.GetPlansVisibleToOrg(orgName)
if err != nil {
return models.ServiceOffering{}, err
}
if len(visiblePlans) == 0 {
return models.ServiceOffering{}, nil
}
return builder.attachSpecificServiceToPlans(serviceName, visiblePlans)
}
func (builder Builder) GetServicesVisibleToOrg(orgName string) ([]models.ServiceOffering, error) {
visiblePlans, err := builder.planBuilder.GetPlansVisibleToOrg(orgName)
if err != nil {
return nil, err
}
if len(visiblePlans) == 0 {
return nil, nil
}
return builder.attachServicesToPlans(visiblePlans)
}
func (builder Builder) attachPlansToServiceForOrg(service models.ServiceOffering, orgName string) (models.ServiceOffering, error) {
plans, err := builder.planBuilder.GetPlansForServiceForOrg(service.GUID, orgName)
if err != nil {
return models.ServiceOffering{}, err
}
service.Plans = plans
return service, nil
}
func (builder Builder) attachPlansToManyServices(services []models.ServiceOffering, plans []models.ServicePlanFields) ([]models.ServiceOffering, error) {
for _, plan := range plans {
for index, service := range services {
if service.GUID == plan.ServiceOfferingGUID {
services[index].Plans = append(service.Plans, plan)
break
}
}
}
return services, nil
}
func (builder Builder) attachPlansToService(service models.ServiceOffering) (models.ServiceOffering, error) {
plans, err := builder.planBuilder.GetPlansForServiceWithOrgs(service.GUID)
if err != nil {
return models.ServiceOffering{}, err
}
service.Plans = plans
return service, nil
}
func (builder Builder) attachServicesToPlans(plans []models.ServicePlanFields) ([]models.ServiceOffering, error) {
var services []models.ServiceOffering
servicesMap := make(map[string]models.ServiceOffering)
for _, plan := range plans {
if plan.ServiceOfferingGUID == "" {
continue
}
if service, ok := servicesMap[plan.ServiceOfferingGUID]; ok {
service.Plans = append(service.Plans, plan)
servicesMap[service.GUID] = service
} else {
service, err := builder.serviceRepo.GetServiceOfferingByGUID(plan.ServiceOfferingGUID)
if err != nil {
return nil, err
}
service.Plans = append(service.Plans, plan)
servicesMap[service.GUID] = service
}
}
for _, service := range servicesMap {
services = append(services, service)
}
return services, nil
}
func (builder Builder) attachSpecificServiceToPlans(serviceName string, plans []models.ServicePlanFields) (models.ServiceOffering, error) {
services, err := builder.serviceRepo.FindServiceOfferingsByLabel(serviceName)
if err != nil {
return models.ServiceOffering{}, err
}
service := services[0]
for _, plan := range plans {
if plan.ServiceOfferingGUID == service.GUID {
service.Plans = append(service.Plans, plan)
}
}
return service, nil
}
func returnV2Service(services models.ServiceOfferings) models.ServiceOffering {
for _, service := range services {
if service.Provider == "" {
return service
}
}
return models.ServiceOffering{}
}