-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker_builder.go
139 lines (117 loc) · 3.95 KB
/
broker_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
package brokerbuilder
import (
"github.com/cloudfoundry/cli/cf/actors/servicebuilder"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/models"
)
//go:generate counterfeiter . BrokerBuilder
type BrokerBuilder interface {
AttachBrokersToServices([]models.ServiceOffering) ([]models.ServiceBroker, error)
AttachSpecificBrokerToServices(string, []models.ServiceOffering) (models.ServiceBroker, error)
GetAllServiceBrokers() ([]models.ServiceBroker, error)
GetBrokerWithAllServices(brokerName string) (models.ServiceBroker, error)
GetBrokerWithSpecifiedService(serviceName string) (models.ServiceBroker, error)
}
type Builder struct {
brokerRepo api.ServiceBrokerRepository
serviceBuilder servicebuilder.ServiceBuilder
}
func NewBuilder(broker api.ServiceBrokerRepository, serviceBuilder servicebuilder.ServiceBuilder) Builder {
return Builder{
brokerRepo: broker,
serviceBuilder: serviceBuilder,
}
}
func (builder Builder) AttachBrokersToServices(services []models.ServiceOffering) ([]models.ServiceBroker, error) {
var brokers []models.ServiceBroker
brokersMap := make(map[string]models.ServiceBroker)
for _, service := range services {
if service.BrokerGUID == "" {
continue
}
if broker, ok := brokersMap[service.BrokerGUID]; ok {
broker.Services = append(broker.Services, service)
brokersMap[broker.GUID] = broker
} else {
broker, err := builder.brokerRepo.FindByGUID(service.BrokerGUID)
if err != nil {
return nil, err
}
broker.Services = append(broker.Services, service)
brokersMap[service.BrokerGUID] = broker
}
}
for _, broker := range brokersMap {
brokers = append(brokers, broker)
}
return brokers, nil
}
func (builder Builder) AttachSpecificBrokerToServices(brokerName string, services []models.ServiceOffering) (models.ServiceBroker, error) {
broker, err := builder.brokerRepo.FindByName(brokerName)
if err != nil {
return models.ServiceBroker{}, err
}
for _, service := range services {
if service.BrokerGUID == broker.GUID {
broker.Services = append(broker.Services, service)
}
}
return broker, nil
}
func (builder Builder) GetAllServiceBrokers() ([]models.ServiceBroker, error) {
brokers := []models.ServiceBroker{}
brokerGUIDs := []string{}
var err error
var services models.ServiceOfferings
err = builder.brokerRepo.ListServiceBrokers(func(broker models.ServiceBroker) bool {
brokers = append(brokers, broker)
brokerGUIDs = append(brokerGUIDs, broker.GUID)
return true
})
if err != nil {
return nil, err
}
services, err = builder.serviceBuilder.GetServicesForManyBrokers(brokerGUIDs)
if err != nil {
return nil, err
}
brokers, err = builder.attachServiceOfferingsToBrokers(services, brokers)
if err != nil {
return nil, err
}
return brokers, err
}
func (builder Builder) attachServiceOfferingsToBrokers(services models.ServiceOfferings, brokers []models.ServiceBroker) ([]models.ServiceBroker, error) {
for _, service := range services {
for index, broker := range brokers {
if broker.GUID == service.BrokerGUID {
brokers[index].Services = append(brokers[index].Services, service)
break
}
}
}
return brokers, nil
}
func (builder Builder) GetBrokerWithAllServices(brokerName string) (models.ServiceBroker, error) {
broker, err := builder.brokerRepo.FindByName(brokerName)
if err != nil {
return models.ServiceBroker{}, err
}
services, err := builder.serviceBuilder.GetServicesForBroker(broker.GUID)
if err != nil {
return models.ServiceBroker{}, err
}
broker.Services = services
return broker, nil
}
func (builder Builder) GetBrokerWithSpecifiedService(serviceName string) (models.ServiceBroker, error) {
service, err := builder.serviceBuilder.GetServiceByNameWithPlansWithOrgNames(serviceName)
if err != nil {
return models.ServiceBroker{}, err
}
brokers, err := builder.AttachBrokersToServices([]models.ServiceOffering{service})
if err != nil || len(brokers) == 0 {
return models.ServiceBroker{}, err
}
return brokers[0], err
}