forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_access.go
136 lines (123 loc) · 5.45 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
package serviceaccess
import (
"fmt"
"strings"
"github.com/cloudfoundry/cli/cf/actors"
"github.com/cloudfoundry/cli/cf/api/authentication"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/flag_helpers"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type ServiceAccess struct {
ui terminal.UI
config configuration.Reader
actor actors.ServiceActor
tokenRefresher authentication.TokenRefresher
}
func NewServiceAccess(ui terminal.UI, config configuration.Reader, actor actors.ServiceActor, tokenRefresher authentication.TokenRefresher) (cmd *ServiceAccess) {
return &ServiceAccess{
ui: ui,
config: config,
actor: actor,
tokenRefresher: tokenRefresher,
}
}
func (cmd *ServiceAccess) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "service-access",
Description: T("List service access settings"),
Usage: "CF_NAME service-access [-b BROKER] [-e SERVICE] [-o ORG]",
Flags: []cli.Flag{
flag_helpers.NewStringFlag("b", T("access for plans of a particular broker")),
flag_helpers.NewStringFlag("e", T("access for plans of a particular service offering")),
flag_helpers.NewStringFlag("o", T("plans accessible by a particular organization")),
},
}
}
func (cmd *ServiceAccess) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
return
}
func (cmd *ServiceAccess) Run(c *cli.Context) {
cmd.tokenRefresher.RefreshAuthToken()
brokerName := c.String("b")
serviceName := c.String("e")
orgName := c.String("o")
if brokerName != "" && serviceName != "" && orgName != "" {
cmd.ui.Say(T("getting service access for broker {{.Broker}} and service {{.Service}} and organization {{.Organization}} as {{.Username}}...", map[string]interface{}{
"Broker": terminal.EntityNameColor(brokerName),
"Service": terminal.EntityNameColor(serviceName),
"Organization": terminal.EntityNameColor(orgName),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
} else if serviceName != "" && orgName != "" {
cmd.ui.Say(T("getting service access for service {{.Service}} and organization {{.Organization}} as {{.Username}}...", map[string]interface{}{
"Service": terminal.EntityNameColor(serviceName),
"Organization": terminal.EntityNameColor(orgName),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
} else if brokerName != "" && orgName != "" {
cmd.ui.Say(T("getting service access for broker {{.Broker}} and organization {{.Organization}} as {{.Username}}...", map[string]interface{}{
"Broker": terminal.EntityNameColor(brokerName),
"Organization": terminal.EntityNameColor(orgName),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
} else if brokerName != "" && serviceName != "" {
cmd.ui.Say(T("getting service access for broker {{.Broker}} and service {{.Service}} as {{.Username}}...", map[string]interface{}{
"Broker": terminal.EntityNameColor(brokerName),
"Service": terminal.EntityNameColor(serviceName),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
} else if brokerName != "" {
cmd.ui.Say(T("getting service access for broker {{.Broker}} as {{.Username}}...", map[string]interface{}{
"Broker": terminal.EntityNameColor(brokerName),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
} else if serviceName != "" {
cmd.ui.Say(T("getting service access for service {{.Service}} as {{.Username}}...", map[string]interface{}{
"Service": terminal.EntityNameColor(serviceName),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
} else if orgName != "" {
cmd.ui.Say(T("getting service access for organization {{.Organization}} as {{.Username}}...", map[string]interface{}{
"Organization": terminal.EntityNameColor(orgName),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
} else {
cmd.ui.Say(T("getting service access as {{.Username}}...", map[string]interface{}{
"Username": terminal.EntityNameColor(cmd.config.Username())}))
}
brokers, err := cmd.actor.FilterBrokers(brokerName, serviceName, orgName)
if err != nil {
cmd.ui.Failed(T("Failed fetching service brokers.\n{{.Error}}", map[string]interface{}{"Error": err}))
return
}
cmd.printTable(brokers)
}
func (cmd ServiceAccess) printTable(brokers []models.ServiceBroker) {
for _, serviceBroker := range brokers {
cmd.ui.Say(fmt.Sprintf(T("broker: {{.Name}}", map[string]interface{}{"Name": serviceBroker.Name})))
table := terminal.NewTable(cmd.ui, []string{"", T("service"), T("plan"), T("access"), T("orgs")})
for _, service := range serviceBroker.Services {
if len(service.Plans) > 0 {
for _, plan := range service.Plans {
table.Add("", service.Label, plan.Name, cmd.formatAccess(plan.Public, plan.OrgNames), strings.Join(plan.OrgNames, ","))
}
} else {
table.Add("", service.Label, "", "", "")
}
}
table.Print()
cmd.ui.Say("")
}
return
}
func (cmd ServiceAccess) formatAccess(public bool, orgNames []string) string {
if public {
return T("all")
}
if len(orgNames) > 0 {
return T("limited")
}
return T("none")
}