forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan_builder.go
130 lines (108 loc) · 3.58 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
package plan_builder
import (
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/models"
)
type PlanBuilder interface {
AttachOrgsToPlans([]models.ServicePlanFields) ([]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 api.OrganizationRepository
}
func NewBuilder(plan api.ServicePlanRepository, vis api.ServicePlanVisibilityRepository, org api.OrganizationRepository) Builder {
return Builder{
servicePlanRepo: plan,
servicePlanVisibilityRepo: vis,
orgRepo: org,
}
}
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) GetPlansForService(serviceGuid string) ([]models.ServicePlanFields, error) {
plans, err := builder.servicePlanRepo.Search(map[string]string{"service_guid": serviceGuid})
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) 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)
builder.orgRepo.ListOrgs(func(org models.Organization) bool {
orgLookup[org.Guid] = org.Name
return true
})
visibilities, err := builder.servicePlanVisibilityRepo.List()
if err != nil {
return nil, err
}
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) buildOrgToPlansVisibilityMap(planToOrgsMap map[string][]string) map[string][]string {
// Since this map doesn't ever change, we memoize it for performance
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
}