-
Notifications
You must be signed in to change notification settings - Fork 929
/
services_plans.go
251 lines (212 loc) · 7.03 KB
/
services_plans.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
package actors
import (
"fmt"
"code.cloudfoundry.org/cli/cf/api/organizations"
"code.cloudfoundry.org/cli/cf/actors/planbuilder"
"code.cloudfoundry.org/cli/cf/actors/servicebuilder"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/models"
)
//go:generate counterfeiter . ServicePlanActor
type ServicePlanActor interface {
FindServiceAccess(string, string) (ServiceAccess, error)
UpdateAllPlansForService(string, bool) error
UpdateOrgForService(string, string, bool) error
UpdateSinglePlanForService(string, string, bool) error
UpdatePlanAndOrgForService(string, string, string, bool) error
}
type ServiceAccess int
const (
ServiceAccessError ServiceAccess = iota
AllPlansArePublic
AllPlansArePrivate
AllPlansAreLimited
SomePlansArePublicSomeAreLimited
SomePlansArePublicSomeArePrivate
SomePlansAreLimitedSomeArePrivate
SomePlansArePublicSomeAreLimitedSomeArePrivate
)
type ServicePlanHandler struct {
servicePlanRepo api.ServicePlanRepository
servicePlanVisibilityRepo api.ServicePlanVisibilityRepository
orgRepo organizations.OrganizationRepository
serviceBuilder servicebuilder.ServiceBuilder
planBuilder planbuilder.PlanBuilder
}
func NewServicePlanHandler(plan api.ServicePlanRepository, vis api.ServicePlanVisibilityRepository, org organizations.OrganizationRepository, planBuilder planbuilder.PlanBuilder, serviceBuilder servicebuilder.ServiceBuilder) ServicePlanHandler {
return ServicePlanHandler{
servicePlanRepo: plan,
servicePlanVisibilityRepo: vis,
orgRepo: org,
serviceBuilder: serviceBuilder,
planBuilder: planBuilder,
}
}
func (actor ServicePlanHandler) UpdateAllPlansForService(serviceName string, setPlanVisibility bool) error {
service, err := actor.serviceBuilder.GetServiceByNameWithPlans(serviceName)
if err != nil {
return err
}
for _, plan := range service.Plans {
err = actor.updateSinglePlan(service, plan.Name, setPlanVisibility)
if err != nil {
return err
}
}
return nil
}
func (actor ServicePlanHandler) UpdateOrgForService(serviceName string, orgName string, setPlanVisibility bool) error {
service, err := actor.serviceBuilder.GetServiceByNameWithPlans(serviceName)
if err != nil {
return err
}
org, err := actor.orgRepo.FindByName(orgName)
if err != nil {
return err
}
for _, plan := range service.Plans {
switch {
case plan.Public:
continue
case setPlanVisibility:
err = actor.servicePlanVisibilityRepo.Create(plan.GUID, org.GUID)
if err != nil {
return err
}
case !setPlanVisibility:
err = actor.deleteServicePlanVisibilities(map[string]string{"organization_guid": org.GUID, "service_plan_guid": plan.GUID})
if err != nil {
return err
}
}
}
return nil
}
func (actor ServicePlanHandler) UpdatePlanAndOrgForService(serviceName, planName, orgName string, setPlanVisibility bool) error {
service, err := actor.serviceBuilder.GetServiceByNameWithPlans(serviceName)
if err != nil {
return err
}
org, err := actor.orgRepo.FindByName(orgName)
if err != nil {
return err
}
found := false
var servicePlan models.ServicePlanFields
for i, val := range service.Plans {
if val.Name == planName {
found = true
servicePlan = service.Plans[i]
}
}
if !found {
return fmt.Errorf("Service plan %s not found", planName)
}
switch {
case servicePlan.Public:
return nil
case setPlanVisibility:
// Enable service access
err = actor.servicePlanVisibilityRepo.Create(servicePlan.GUID, org.GUID)
case !setPlanVisibility:
// Disable service access
err = actor.deleteServicePlanVisibilities(map[string]string{"organization_guid": org.GUID, "service_plan_guid": servicePlan.GUID})
}
return err
}
func (actor ServicePlanHandler) UpdateSinglePlanForService(serviceName string, planName string, setPlanVisibility bool) error {
serviceOffering, err := actor.serviceBuilder.GetServiceByNameWithPlans(serviceName)
if err != nil {
return err
}
return actor.updateSinglePlan(serviceOffering, planName, setPlanVisibility)
}
func (actor ServicePlanHandler) updateSinglePlan(serviceOffering models.ServiceOffering, planName string, setPlanVisibility bool) error {
var planToUpdate *models.ServicePlanFields
for _, servicePlan := range serviceOffering.Plans {
if servicePlan.Name == planName {
planToUpdate = &servicePlan
break
}
}
if planToUpdate == nil {
return fmt.Errorf("The plan %s could not be found for service %s", planName, serviceOffering.Label)
}
return actor.updateServicePlanAvailability(serviceOffering.GUID, *planToUpdate, setPlanVisibility)
}
func (actor ServicePlanHandler) deleteServicePlanVisibilities(queryParams map[string]string) error {
visibilities, err := actor.servicePlanVisibilityRepo.Search(queryParams)
if err != nil {
return err
}
for _, visibility := range visibilities {
err = actor.servicePlanVisibilityRepo.Delete(visibility.GUID)
if err != nil {
return err
}
}
return nil
}
func (actor ServicePlanHandler) updateServicePlanAvailability(serviceGUID string, servicePlan models.ServicePlanFields, setPlanVisibility bool) error {
// We delete all service plan visibilities for the given Plan since the attribute public should function as a giant on/off
// switch for all orgs. Thus we need to clean up any visibilities laying around so that they don't carry over.
err := actor.deleteServicePlanVisibilities(map[string]string{"service_plan_guid": servicePlan.GUID})
if err != nil {
return err
}
if servicePlan.Public == setPlanVisibility {
return nil
}
return actor.servicePlanRepo.Update(servicePlan, serviceGUID, setPlanVisibility)
}
func (actor ServicePlanHandler) FindServiceAccess(serviceName string, orgName string) (ServiceAccess, error) {
service, err := actor.serviceBuilder.GetServiceByNameForOrg(serviceName, orgName)
if err != nil {
return ServiceAccessError, err
}
publicBucket, limitedBucket, privateBucket := 0, 0, 0
for _, plan := range service.Plans {
if plan.Public {
publicBucket++
} else if len(plan.OrgNames) > 0 {
limitedBucket++
} else {
privateBucket++
}
}
if publicBucket > 0 && limitedBucket == 0 && privateBucket == 0 {
return AllPlansArePublic, nil
}
if publicBucket > 0 && limitedBucket > 0 && privateBucket == 0 {
return SomePlansArePublicSomeAreLimited, nil
}
if publicBucket > 0 && privateBucket > 0 && limitedBucket == 0 {
return SomePlansArePublicSomeArePrivate, nil
}
if limitedBucket > 0 && publicBucket == 0 && privateBucket == 0 {
return AllPlansAreLimited, nil
}
if privateBucket > 0 && publicBucket == 0 && privateBucket == 0 {
return AllPlansArePrivate, nil
}
if limitedBucket > 0 && privateBucket > 0 && publicBucket == 0 {
return SomePlansAreLimitedSomeArePrivate, nil
}
return SomePlansArePublicSomeAreLimitedSomeArePrivate, nil
}
type PlanAccess int
const (
PlanAccessError PlanAccess = iota
All
Limited
None
)
func (actor ServicePlanHandler) findPlanAccess(plan models.ServicePlanFields) PlanAccess {
if plan.Public {
return All
} else if len(plan.OrgNames) > 0 {
return Limited
} else {
return None
}
}