forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_access.go
349 lines (289 loc) · 9.8 KB
/
service_access.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package v7action
import (
"errors"
"fmt"
"sort"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
)
type ServicePlanWithSpaceAndOrganization ccv3.ServicePlanWithSpaceAndOrganization
type ServicePlanAccess struct {
BrokerName string
ServiceOfferingName string
ServicePlanName string
VisibilityType ccv3.VisibilityType
VisibilityDetails []string
}
type SkippedPlans []string
const (
visibleAdmin = ccv3.VisibilityType("admin")
visiblePublic = ccv3.VisibilityType("public")
visibleOrganization = ccv3.VisibilityType("organization")
visibleSpace = ccv3.VisibilityType("space")
)
type offeringDetails struct{ offeringName, brokerName string }
func (actor *Actor) GetServiceAccess(offeringName, brokerName, orgName string) ([]ServicePlanAccess, Warnings, error) {
var orgGUID string
var allWarnings Warnings
if orgName != "" {
org, orgWarnings, err := actor.GetOrganizationByName(orgName)
if err != nil {
return nil, orgWarnings, err
}
allWarnings = append(allWarnings, orgWarnings...)
orgGUID = org.GUID
}
plansQuery := buildPlansFilterForGet(offeringName, brokerName, orgGUID)
offerings, offeringsWarnings, err := actor.getServiceOfferings(offeringName, brokerName)
allWarnings = append(allWarnings, offeringsWarnings...)
if err != nil {
return nil, allWarnings, err
}
plans, plansWarnings, err := actor.CloudControllerClient.GetServicePlansWithSpaceAndOrganization(plansQuery...)
allWarnings = append(allWarnings, plansWarnings...)
if err != nil {
return nil, allWarnings, err
}
var result []ServicePlanAccess
for _, plan := range plans {
if offering, ok := offerings[plan.ServiceOfferingGUID]; ok {
visibilityDetails, warnings, err := actor.getServicePlanVisibilityDetails(ServicePlanWithSpaceAndOrganization(plan))
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
result = append(result, ServicePlanAccess{
ServicePlanName: plan.Name,
VisibilityType: plan.VisibilityType,
VisibilityDetails: visibilityDetails,
ServiceOfferingName: offering.offeringName,
BrokerName: offering.brokerName,
})
}
}
sort.Slice(result, func(i, j int) bool {
if result[i].BrokerName != result[j].BrokerName {
return result[i].BrokerName < result[j].BrokerName
}
if result[i].ServiceOfferingName != result[j].ServiceOfferingName {
return result[i].ServiceOfferingName < result[j].ServiceOfferingName
}
return result[i].ServicePlanName < result[j].ServicePlanName
})
return result, allWarnings, err
}
func (actor *Actor) EnableServiceAccess(offeringName, brokerName, orgName, planName string) (SkippedPlans, Warnings, error) {
var allWarnings Warnings
offering, offeringWarnings, err := actor.CloudControllerClient.GetServiceOfferingByNameAndBroker(offeringName, brokerName)
allWarnings = append(allWarnings, offeringWarnings...)
if err != nil {
return nil, allWarnings, actionerror.EnrichAPIErrors(err)
}
plansQuery := buildPlansFilterForUpdate(offering.GUID, planName)
plans, planWarnings, err := actor.CloudControllerClient.GetServicePlans(plansQuery...)
allWarnings = append(allWarnings, planWarnings...)
if err != nil {
return nil, allWarnings, err
}
if len(plans) == 0 {
return nil, allWarnings, actionerror.ServicePlanNotFoundError{
PlanName: planName,
OfferingName: offeringName,
}
}
if offeringIsSpaceScoped(plans) {
return nil, allWarnings, actionerror.ServicePlanVisibilityTypeError{}
}
visibility := ccv3.ServicePlanVisibility{Type: visiblePublic}
if orgName != "" {
org, orgWarnings, err := actor.GetOrganizationByName(orgName)
allWarnings = append(allWarnings, orgWarnings...)
if err != nil {
return nil, allWarnings, err
}
visibility.Type = visibleOrganization
visibility.Organizations = []ccv3.VisibilityDetail{{GUID: org.GUID}}
}
var skipped SkippedPlans
for _, plan := range plans {
if plan.VisibilityType == visiblePublic && visibility.Type == visibleOrganization {
skipped = append(skipped, plan.Name)
continue
}
_, visibilityWarnings, err := actor.CloudControllerClient.UpdateServicePlanVisibility(
plan.GUID,
visibility,
)
allWarnings = append(allWarnings, visibilityWarnings...)
if err != nil {
return nil, allWarnings, err
}
}
return skipped, allWarnings, nil
}
func (actor *Actor) DisableServiceAccess(offeringName, brokerName, orgName, planName string) (SkippedPlans, Warnings, error) {
var allWarnings Warnings
offering, offeringWarnings, err := actor.CloudControllerClient.GetServiceOfferingByNameAndBroker(offeringName, brokerName)
allWarnings = append(allWarnings, offeringWarnings...)
if err != nil {
return SkippedPlans{}, allWarnings, actionerror.EnrichAPIErrors(err)
}
plansQuery := buildPlansFilterForUpdate(offering.GUID, planName)
plans, planWarnings, err := actor.CloudControllerClient.GetServicePlans(plansQuery...)
allWarnings = append(allWarnings, planWarnings...)
if err != nil {
return SkippedPlans{}, allWarnings, err
}
if len(plans) == 0 {
return SkippedPlans{}, allWarnings, actionerror.ServicePlanNotFoundError{
PlanName: planName,
OfferingName: offeringName,
}
}
if offeringIsSpaceScoped(plans) {
return SkippedPlans{}, allWarnings, actionerror.ServicePlanVisibilityTypeError{}
}
var (
disableWarnings Warnings
skipped SkippedPlans
)
if orgName != "" {
skipped, disableWarnings, err = actor.disableOrganizationServiceAccess(plans, orgName)
} else {
skipped, disableWarnings, err = actor.disableAllServiceAccess(plans)
}
allWarnings = append(allWarnings, disableWarnings...)
return skipped, allWarnings, err
}
func (actor *Actor) disableAllServiceAccess(plans []ccv3.ServicePlan) (SkippedPlans, Warnings, error) {
var (
allWarnings Warnings
skipped SkippedPlans
)
visibility := ccv3.ServicePlanVisibility{Type: visibleAdmin}
for _, plan := range plans {
if plan.VisibilityType == visibleAdmin {
skipped = append(skipped, plan.Name)
continue
}
_, visibilityWarnings, err := actor.CloudControllerClient.UpdateServicePlanVisibility(
plan.GUID,
visibility,
)
allWarnings = append(allWarnings, visibilityWarnings...)
if err != nil {
return skipped, allWarnings, err
}
}
return skipped, allWarnings, nil
}
func (actor *Actor) disableOrganizationServiceAccess(plans []ccv3.ServicePlan, orgName string) (SkippedPlans, Warnings, error) {
var allWarnings Warnings
org, orgWarnings, err := actor.GetOrganizationByName(orgName)
allWarnings = append(allWarnings, orgWarnings...)
if err != nil {
return nil, allWarnings, err
}
for _, plan := range plans {
if plan.VisibilityType == visiblePublic {
return nil, allWarnings, errors.New("Cannot remove organization level access for public plans.")
}
}
var skipped SkippedPlans
for _, plan := range plans {
if plan.VisibilityType == visibleAdmin {
skipped = append(skipped, plan.Name)
continue
}
deleteWarnings, err := actor.CloudControllerClient.DeleteServicePlanVisibility(plan.GUID, org.GUID)
allWarnings = append(allWarnings, deleteWarnings...)
if err != nil {
return skipped, allWarnings, err
}
}
return skipped, allWarnings, nil
}
func (actor *Actor) getServiceOfferings(service, broker string) (map[string]offeringDetails, Warnings, error) {
var offeringsQuery []ccv3.Query
if broker != "" {
offeringsQuery = append(offeringsQuery, ccv3.Query{
Key: ccv3.ServiceBrokerNamesFilter,
Values: []string{broker},
})
}
if service != "" {
offeringsQuery = append(offeringsQuery, ccv3.Query{
Key: ccv3.NameFilter,
Values: []string{service},
})
}
serviceOfferings, warnings, err := actor.CloudControllerClient.GetServiceOfferings(offeringsQuery...)
if err != nil {
return nil, Warnings(warnings), err
}
if len(serviceOfferings) == 0 && len(offeringsQuery) > 0 {
return nil, Warnings(warnings), actionerror.ServiceNotFoundError{Name: service, Broker: broker}
}
offerings := make(map[string]offeringDetails)
for _, o := range serviceOfferings {
offerings[o.GUID] = offeringDetails{
offeringName: o.Name,
brokerName: o.ServiceBrokerName,
}
}
return offerings, Warnings(warnings), err
}
func (actor *Actor) getServicePlanVisibilityDetails(plan ServicePlanWithSpaceAndOrganization) (names []string, warnings Warnings, err error) {
if plan.VisibilityType == visibleOrganization {
result, vwarn, err := actor.CloudControllerClient.GetServicePlanVisibility(plan.GUID)
warnings = Warnings(vwarn)
if err != nil {
return nil, warnings, err
}
for _, organization := range result.Organizations {
names = append(names, organization.Name)
}
}
if plan.VisibilityType == visibleSpace {
names = []string{fmt.Sprintf("%s (org: %s)", plan.SpaceName, plan.OrganizationName)}
}
return names, warnings, nil
}
func buildPlansFilterForGet(offeringName, brokerName, orgGUID string) (query []ccv3.Query) {
if offeringName != "" {
query = append(query, ccv3.Query{
Key: ccv3.ServiceOfferingNamesFilter,
Values: []string{offeringName},
})
}
if brokerName != "" {
query = append(query, ccv3.Query{
Key: ccv3.ServiceBrokerNamesFilter,
Values: []string{brokerName},
})
}
if orgGUID != "" {
query = append(query, ccv3.Query{
Key: ccv3.OrganizationGUIDFilter,
Values: []string{orgGUID},
})
}
return query
}
func buildPlansFilterForUpdate(offeringGUID, planName string) []ccv3.Query {
plansQuery := []ccv3.Query{{
Key: ccv3.ServiceOfferingGUIDsFilter,
Values: []string{offeringGUID},
}}
if planName != "" {
plansQuery = append(plansQuery, ccv3.Query{
Key: ccv3.NameFilter,
Values: []string{planName},
})
}
return plansQuery
}
func offeringIsSpaceScoped(plans []ccv3.ServicePlan) bool {
// All plans from a space scoped offering will have the same visibility type
return plans[0].VisibilityType == visibleSpace
}