forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
113 lines (98 loc) · 3.3 KB
/
services.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
package actors
import (
"github.com/cloudfoundry/cli/cf/actors/broker_builder"
"github.com/cloudfoundry/cli/cf/actors/service_builder"
"github.com/cloudfoundry/cli/cf/api/organizations"
"github.com/cloudfoundry/cli/cf/models"
)
type ServiceActor interface {
FilterBrokers(brokerFlag string, serviceFlag string, orgFlag string) ([]models.ServiceBroker, error)
}
type ServiceHandler struct {
orgRepo organizations.OrganizationRepository
brokerBuilder broker_builder.BrokerBuilder
serviceBuilder service_builder.ServiceBuilder
}
func NewServiceHandler(org organizations.OrganizationRepository, brokerBuilder broker_builder.BrokerBuilder, serviceBuilder service_builder.ServiceBuilder) ServiceHandler {
return ServiceHandler{
orgRepo: org,
brokerBuilder: brokerBuilder,
serviceBuilder: serviceBuilder,
}
}
func (actor ServiceHandler) FilterBrokers(brokerFlag string, serviceFlag string, orgFlag string) ([]models.ServiceBroker, error) {
if orgFlag == "" {
return actor.getServiceBrokers(brokerFlag, serviceFlag)
} else {
err := actor.checkForOrgExistence(orgFlag)
if err != nil {
return nil, err
}
return actor.buildBrokersVisibleFromOrg(brokerFlag, serviceFlag, orgFlag)
}
}
func (actor ServiceHandler) checkForOrgExistence(orgName string) error {
_, err := actor.orgRepo.FindByName(orgName)
return err
}
func (actor ServiceHandler) getServiceBrokers(brokerName string, serviceName string) ([]models.ServiceBroker, error) {
if serviceName != "" {
broker, err := actor.brokerBuilder.GetBrokerWithSpecifiedService(serviceName)
if err != nil {
return nil, err
}
if brokerName != "" {
if broker.Name != brokerName {
return nil, nil
}
}
return []models.ServiceBroker{broker}, nil
}
if brokerName != "" && serviceName == "" {
broker, err := actor.brokerBuilder.GetBrokerWithAllServices(brokerName)
if err != nil {
return nil, err
}
return []models.ServiceBroker{broker}, nil
}
return actor.brokerBuilder.GetAllServiceBrokers()
}
func (actor ServiceHandler) buildBrokersVisibleFromOrg(brokerFlag string, serviceFlag string, orgFlag string) ([]models.ServiceBroker, error) {
if serviceFlag != "" && brokerFlag != "" {
service, err := actor.serviceBuilder.GetServiceVisibleToOrg(serviceFlag, orgFlag)
if err != nil {
return nil, err
}
broker, err := actor.brokerBuilder.AttachSpecificBrokerToServices(brokerFlag, []models.ServiceOffering{service})
if err != nil {
return nil, err
}
return []models.ServiceBroker{broker}, nil
}
if serviceFlag != "" && brokerFlag == "" {
service, err := actor.serviceBuilder.GetServiceVisibleToOrg(serviceFlag, orgFlag)
if err != nil {
return nil, err
}
return actor.brokerBuilder.AttachBrokersToServices([]models.ServiceOffering{service})
}
if serviceFlag == "" && brokerFlag != "" {
services, err := actor.serviceBuilder.GetServicesVisibleToOrg(orgFlag)
if err != nil {
return nil, err
}
broker, err := actor.brokerBuilder.AttachSpecificBrokerToServices(brokerFlag, services)
if err != nil {
return nil, err
}
return []models.ServiceBroker{broker}, nil
}
if serviceFlag == "" && brokerFlag == "" {
services, err := actor.serviceBuilder.GetServicesVisibleToOrg(orgFlag)
if err != nil {
return nil, err
}
return actor.brokerBuilder.AttachBrokersToServices(services)
}
return nil, nil
}