forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable_service_access.go
144 lines (124 loc) · 5.79 KB
/
disable_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
package serviceaccess
import (
"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/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type DisableServiceAccess struct {
ui terminal.UI
config configuration.Reader
actor actors.ServicePlanActor
tokenRefresher authentication.TokenRefresher
}
func NewDisableServiceAccess(ui terminal.UI, config configuration.Reader, actor actors.ServicePlanActor, tokenRefresher authentication.TokenRefresher) (cmd *DisableServiceAccess) {
return &DisableServiceAccess{
ui: ui,
config: config,
actor: actor,
tokenRefresher: tokenRefresher,
}
}
func (cmd *DisableServiceAccess) GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) ([]requirements.Requirement, error) {
if len(context.Args()) != 1 {
cmd.ui.FailWithUsage(context)
}
return []requirements.Requirement{requirementsFactory.NewLoginRequirement()}, nil
}
func (cmd *DisableServiceAccess) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "disable-service-access",
Description: T("Disable access to a service or service plan for one or all orgs"),
Usage: "CF_NAME disable-service-access SERVICE [-p PLAN] [-o ORG]",
Flags: []cli.Flag{
flag_helpers.NewStringFlag("p", T("Disable access to a particular service plan")),
flag_helpers.NewStringFlag("o", T("Disable access to a particular organization")),
},
}
}
func (cmd *DisableServiceAccess) Run(c *cli.Context) {
cmd.tokenRefresher.RefreshAuthToken()
serviceName := c.Args()[0]
planName := c.String("p")
orgName := c.String("o")
if planName != "" && orgName != "" {
cmd.disablePlanAndOrgForService(serviceName, planName, orgName)
} else if planName != "" {
cmd.disableSinglePlanForService(serviceName, planName)
} else if orgName != "" {
cmd.disablePlansForSingleOrgForService(serviceName, orgName)
} else {
cmd.disableServiceForAll(serviceName)
}
cmd.ui.Say(T("OK"))
}
func (cmd *DisableServiceAccess) disableServiceForAll(serviceName string) {
allPlansAlreadySet, err := cmd.actor.UpdateAllPlansForService(serviceName, false)
if err != nil {
cmd.ui.Failed(err.Error())
}
if allPlansAlreadySet {
cmd.ui.Say(T("All plans of the service are already inaccessible for all orgs"))
} else {
cmd.ui.Say(T("Disabling access to all plans of service {{.ServiceName}} for all orgs as {{.UserName}}...", map[string]interface{}{"ServiceName": terminal.EntityNameColor(serviceName), "UserName": terminal.EntityNameColor(cmd.config.Username())}))
}
}
func (cmd *DisableServiceAccess) disablePlanAndOrgForService(serviceName string, planName string, orgName string) {
planOriginalAccess, err := cmd.actor.UpdatePlanAndOrgForService(serviceName, planName, orgName, false)
if err != nil {
cmd.ui.Failed(err.Error())
}
if planOriginalAccess == actors.None {
cmd.ui.Say(T("The plan is already inaccessible for this org"))
} else if planOriginalAccess == actors.Limited {
cmd.ui.Say(T("Disabling access to plan {{.PlanName}} of service {{.ServiceName}} for org {{.OrgName}} as {{.Username}}...", map[string]interface{}{"PlanName": terminal.EntityNameColor(planName), "ServiceName": terminal.EntityNameColor(serviceName), "OrgName": terminal.EntityNameColor(orgName), "Username": terminal.EntityNameColor(cmd.config.Username())}))
} else {
cmd.ui.Say(T("No action taken. You must disable access to the {{.PlanName}} plan of {{.ServiceName}} service for all orgs and then grant access for all orgs except the {{.OrgName}} org.",
map[string]interface{}{
"PlanName": terminal.EntityNameColor(planName),
"ServiceName": terminal.EntityNameColor(serviceName),
"OrgName": terminal.EntityNameColor(orgName),
}))
}
return
}
func (cmd *DisableServiceAccess) disableSinglePlanForService(serviceName string, planName string) {
planOriginalAccess, err := cmd.actor.UpdateSinglePlanForService(serviceName, planName, false)
if err != nil {
cmd.ui.Failed(err.Error())
}
if planOriginalAccess == actors.None {
cmd.ui.Say(T("The plan is already inaccessible for all orgs"))
} else {
cmd.ui.Say(T("Disabling access of plan {{.PlanName}} for service {{.ServiceName}} as {{.Username}}...", map[string]interface{}{"PlanName": terminal.EntityNameColor(planName), "ServiceName": terminal.EntityNameColor(serviceName), "Username": terminal.EntityNameColor(cmd.config.Username())}))
}
return
}
func (cmd *DisableServiceAccess) disablePlansForSingleOrgForService(serviceName string, orgName string) {
serviceAccess, err := cmd.actor.FindServiceAccess(serviceName)
if err != nil {
cmd.ui.Failed(err.Error())
}
if serviceAccess == actors.AllPlansArePublic {
cmd.ui.Say(T("No action taken. You must disable access to all plans of {{.ServiceName}} service for all orgs and then grant access for all orgs except the {{.OrgName}} org.",
map[string]interface{}{
"ServiceName": terminal.EntityNameColor(serviceName),
"OrgName": terminal.EntityNameColor(orgName),
}))
return
}
allPlansWereSet, err := cmd.actor.UpdateOrgForService(serviceName, orgName, false)
if err != nil {
cmd.ui.Failed(err.Error())
}
if allPlansWereSet {
cmd.ui.Say(T("All plans of the service are already inaccessible for this org"))
} else {
cmd.ui.Say(T("Disabling access to all plans of service {{.ServiceName}} for the org {{.OrgName}} as {{.Username}}...", map[string]interface{}{"ServiceName": terminal.EntityNameColor(serviceName), "OrgName": terminal.EntityNameColor(orgName), "Username": terminal.EntityNameColor(cmd.config.Username())}))
}
}