forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan_builder.go
232 lines (192 loc) · 6.37 KB
/
plan_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
package planbuilder
import (
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/api/organizations"
"code.cloudfoundry.org/cli/cf/models"
)
//go:generate counterfeiter . PlanBuilder
type PlanBuilder interface {
AttachOrgsToPlans([]models.ServicePlanFields) ([]models.ServicePlanFields, error)
AttachOrgToPlans([]models.ServicePlanFields, string) ([]models.ServicePlanFields, error)
GetPlansForServiceForOrg(string, string) ([]models.ServicePlanFields, error)
GetPlansForServiceWithOrgs(string) ([]models.ServicePlanFields, error)
GetPlansForManyServicesWithOrgs([]string) ([]models.ServicePlanFields, error)
GetPlansForService(string) ([]models.ServicePlanFields, error)
GetPlansVisibleToOrg(string) ([]models.ServicePlanFields, error)
}
var (
OrgToPlansVisibilityMap *map[string][]string
PlanToOrgsVisibilityMap *map[string][]string
)
type Builder struct {
servicePlanRepo api.ServicePlanRepository
servicePlanVisibilityRepo api.ServicePlanVisibilityRepository
orgRepo organizations.OrganizationRepository
}
func NewBuilder(plan api.ServicePlanRepository, vis api.ServicePlanVisibilityRepository, org organizations.OrganizationRepository) Builder {
return Builder{
servicePlanRepo: plan,
servicePlanVisibilityRepo: vis,
orgRepo: org,
}
}
func (builder Builder) AttachOrgToPlans(plans []models.ServicePlanFields, orgName string) ([]models.ServicePlanFields, error) {
visMap, err := builder.buildPlanToOrgVisibilityMap(orgName)
if err != nil {
return nil, err
}
for planIndex := range plans {
plan := &plans[planIndex]
plan.OrgNames = visMap[plan.GUID]
}
return plans, nil
}
func (builder Builder) AttachOrgsToPlans(plans []models.ServicePlanFields) ([]models.ServicePlanFields, error) {
visMap, err := builder.buildPlanToOrgsVisibilityMap()
if err != nil {
return nil, err
}
for planIndex := range plans {
plan := &plans[planIndex]
plan.OrgNames = visMap[plan.GUID]
}
return plans, nil
}
func (builder Builder) GetPlansForServiceForOrg(serviceGUID string, orgName string) ([]models.ServicePlanFields, error) {
plans, err := builder.servicePlanRepo.Search(map[string]string{"service_guid": serviceGUID})
if err != nil {
return nil, err
}
plans, err = builder.AttachOrgToPlans(plans, orgName)
if err != nil {
return nil, err
}
return plans, nil
}
func (builder Builder) GetPlansForService(serviceGUID string) ([]models.ServicePlanFields, error) {
plans, err := builder.servicePlanRepo.Search(map[string]string{"service_guid": serviceGUID})
if err != nil {
return nil, err
}
return plans, nil
}
func (builder Builder) GetPlansForServiceWithOrgs(serviceGUID string) ([]models.ServicePlanFields, error) {
plans, err := builder.GetPlansForService(serviceGUID)
if err != nil {
return nil, err
}
plans, err = builder.AttachOrgsToPlans(plans)
if err != nil {
return nil, err
}
return plans, nil
}
func (builder Builder) GetPlansForManyServicesWithOrgs(serviceGUIDs []string) ([]models.ServicePlanFields, error) {
plans, err := builder.servicePlanRepo.ListPlansFromManyServices(serviceGUIDs)
if err != nil {
return nil, err
}
plans, err = builder.AttachOrgsToPlans(plans)
if err != nil {
return nil, err
}
return plans, nil
}
func (builder Builder) GetPlansVisibleToOrg(orgName string) ([]models.ServicePlanFields, error) {
var plansToReturn []models.ServicePlanFields
allPlans, err := builder.servicePlanRepo.Search(nil)
planToOrgsVisMap, err := builder.buildPlanToOrgsVisibilityMap()
if err != nil {
return nil, err
}
orgToPlansVisMap := builder.buildOrgToPlansVisibilityMap(planToOrgsVisMap)
filterOrgPlans := orgToPlansVisMap[orgName]
for _, plan := range allPlans {
if builder.containsGUID(filterOrgPlans, plan.GUID) {
plan.OrgNames = planToOrgsVisMap[plan.GUID]
plansToReturn = append(plansToReturn, plan)
} else if plan.Public {
plansToReturn = append(plansToReturn, plan)
}
}
return plansToReturn, nil
}
func (builder Builder) containsGUID(guidSlice []string, guid string) bool {
for _, g := range guidSlice {
if g == guid {
return true
}
}
return false
}
func (builder Builder) buildPlanToOrgVisibilityMap(orgName string) (map[string][]string, error) {
// Since this map doesn't ever change, we memoize it for performance
orgLookup := make(map[string]string)
org, err := builder.orgRepo.FindByName(orgName)
if err != nil {
return nil, err
}
orgLookup[org.GUID] = org.Name
visibilities, err := builder.servicePlanVisibilityRepo.List()
if err != nil {
return nil, err
}
visMap := make(map[string][]string)
for _, vis := range visibilities {
if _, exists := orgLookup[vis.OrganizationGUID]; exists {
visMap[vis.ServicePlanGUID] = append(visMap[vis.ServicePlanGUID], orgLookup[vis.OrganizationGUID])
}
}
return visMap, nil
}
func (builder Builder) buildPlanToOrgsVisibilityMap() (map[string][]string, error) {
// Since this map doesn't ever change, we memoize it for performance
if PlanToOrgsVisibilityMap == nil {
orgLookup := make(map[string]string)
visibilities, err := builder.servicePlanVisibilityRepo.List()
if err != nil {
return nil, err
}
orgGUIDs := builder.getUniqueOrgGUIDsFromVisibilities(visibilities)
orgs, err := builder.orgRepo.GetManyOrgsByGUID(orgGUIDs)
if err != nil {
return nil, err
}
for _, org := range orgs {
orgLookup[org.GUID] = org.Name
}
visMap := make(map[string][]string)
for _, vis := range visibilities {
visMap[vis.ServicePlanGUID] = append(visMap[vis.ServicePlanGUID], orgLookup[vis.OrganizationGUID])
}
PlanToOrgsVisibilityMap = &visMap
}
return *PlanToOrgsVisibilityMap, nil
}
func (builder Builder) getUniqueOrgGUIDsFromVisibilities(visibilities []models.ServicePlanVisibilityFields) (orgGUIDs []string) {
for _, visibility := range visibilities {
found := false
for _, orgGUID := range orgGUIDs {
if orgGUID == visibility.OrganizationGUID {
found = true
break
}
}
if !found {
orgGUIDs = append(orgGUIDs, visibility.OrganizationGUID)
}
}
return
}
func (builder Builder) buildOrgToPlansVisibilityMap(planToOrgsMap map[string][]string) map[string][]string {
if OrgToPlansVisibilityMap == nil {
visMap := make(map[string][]string)
for planGUID, orgNames := range planToOrgsMap {
for _, orgName := range orgNames {
visMap[orgName] = append(visMap[orgName], planGUID)
}
}
OrgToPlansVisibilityMap = &visMap
}
return *OrgToPlansVisibilityMap
}