-
Notifications
You must be signed in to change notification settings - Fork 927
/
service_access_command.go
167 lines (136 loc) · 5.12 KB
/
service_access_command.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
package v6
import (
"sort"
"strings"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/actor/v2action/composite"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/v6/shared"
"code.cloudfoundry.org/cli/util/sorting"
)
//go:generate counterfeiter . ServiceAccessActor
type ServiceAccessActor interface {
GetServiceBrokerSummaries(broker string, service string, organization string) ([]v2action.ServiceBrokerSummary, v2action.Warnings, error)
}
type ServiceAccessCommand struct {
Broker string `short:"b" description:"Access for plans of a particular broker"`
Service string `short:"e" description:"Access for service name of a particular service offering"`
Organization string `short:"o" description:"Plans accessible by a particular organization"`
usage interface{} `usage:"CF_NAME service-access [-b BROKER] [-e SERVICE] [-o ORG]"`
relatedCommands interface{} `related_commands:"marketplace, disable-service-access, enable-service-access, service-brokers"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor ServiceAccessActor
}
func (cmd *ServiceAccessCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.NewClients(config, ui, true)
if err != nil {
return err
}
baseActor := v2action.NewActor(ccClient, uaaClient, config)
cmd.Actor = &composite.ServiceBrokerSummaryCompositeActor{
ServiceActor: baseActor,
BrokerActor: baseActor,
OrgActor: baseActor,
VisibilityActor: baseActor,
}
return nil
}
func (cmd ServiceAccessCommand) Execute(args []string) error {
if err := cmd.SharedActor.CheckTarget(false, false); err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
template := serviceAccessMessages[serviceAccessOptions{Broker: cmd.Broker != "", Service: cmd.Service != "", Org: cmd.Organization != ""}]
cmd.UI.DisplayTextWithFlavor(template, map[string]interface{}{
"Broker": cmd.Broker,
"Service": cmd.Service,
"Org": cmd.Organization,
"CurrentUser": user.Name,
})
summaries, warnings, err := cmd.Actor.GetServiceBrokerSummaries(cmd.Broker, cmd.Service, cmd.Organization)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
sortBrokers(summaries)
tableHeaders := []string{"service", "plan", "access", "orgs"}
for _, broker := range summaries {
cmd.UI.DisplayText("broker: {{.BrokerName}}", map[string]interface{}{
"BrokerName": broker.Name,
})
data := [][]string{tableHeaders}
for _, service := range broker.Services {
for _, plan := range service.Plans {
data = append(data, []string{
service.Label,
plan.Name,
cmd.UI.TranslateText(formatAccess(plan)),
strings.Join(plan.VisibleTo, ","),
})
}
}
cmd.UI.DisplayTableWithHeader(" ", data, 3)
cmd.UI.DisplayNewline()
}
return nil
}
func formatAccess(plan v2action.ServicePlanSummary) string {
if plan.Public {
return "all"
}
if len(plan.VisibleTo) > 0 {
return "limited"
}
return "none"
}
func sortBrokers(brokers []v2action.ServiceBrokerSummary) {
sort.Slice(brokers, func(i, j int) bool {
return sorting.LessIgnoreCase(brokers[i].Name, brokers[j].Name)
})
for _, broker := range brokers {
sortServices(broker.Services)
}
}
func sortServices(services []v2action.ServiceSummary) {
sort.Slice(services, func(i, j int) bool {
return sorting.LessIgnoreCase(services[i].Label, services[j].Label)
})
for _, service := range services {
sortPlans(service.Plans)
}
}
func sortPlans(plans []v2action.ServicePlanSummary) {
sort.Slice(plans, func(i, j int) bool {
return sorting.LessIgnoreCase(plans[i].Name, plans[j].Name)
})
for _, plan := range plans {
sortOrgs(plan.VisibleTo)
}
}
func sortOrgs(orgs []string) {
sort.Strings(orgs)
}
type serviceAccessOptions struct {
Broker bool
Service bool
Org bool
}
var serviceAccessMessages = map[serviceAccessOptions]string{
{Broker: false, Service: false, Org: false}: "Getting service access as {{.CurrentUser}}...",
{Broker: true, Service: false, Org: false}: "Getting service access for broker {{.Broker}} as {{.CurrentUser}}...",
{Broker: true, Service: true, Org: false}: "Getting service access for broker {{.Broker}} and service {{.Service}} as {{.CurrentUser}}...",
{Broker: true, Service: true, Org: true}: "Getting service access for broker {{.Broker}} and service {{.Service}} and organization {{.Org}} as {{.CurrentUser}}...",
{Broker: true, Service: false, Org: true}: "Getting service access for broker {{.Broker}} and organization {{.Org}} as {{.CurrentUser}}...",
{Broker: false, Service: true, Org: false}: "Getting service access for service {{.Service}} as {{.CurrentUser}}...",
{Broker: false, Service: true, Org: true}: "Getting service access for service {{.Service}} and organization {{.Org}} as {{.CurrentUser}}...",
{Broker: false, Service: false, Org: true}: "Getting service access for organization {{.Org}} as {{.CurrentUser}}...",
}