github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/serviceaccess/disable_service_access.go (about) 1 package serviceaccess 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/actors" 7 "code.cloudfoundry.org/cli/cf/api/authentication" 8 "code.cloudfoundry.org/cli/cf/commandregistry" 9 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 10 "code.cloudfoundry.org/cli/cf/flags" 11 . "code.cloudfoundry.org/cli/cf/i18n" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 ) 15 16 type DisableServiceAccess struct { 17 ui terminal.UI 18 config coreconfig.Reader 19 actor actors.ServicePlanActor 20 tokenRefresher authentication.TokenRefresher 21 } 22 23 func init() { 24 commandregistry.Register(&DisableServiceAccess{}) 25 } 26 27 func (cmd *DisableServiceAccess) MetaData() commandregistry.CommandMetadata { 28 fs := make(map[string]flags.FlagSet) 29 fs["p"] = &flags.StringFlag{ShortName: "p", Usage: T("Disable access to a specified service plan")} 30 fs["o"] = &flags.StringFlag{ShortName: "o", Usage: T("Disable access for a specified organization")} 31 32 return commandregistry.CommandMetadata{ 33 Name: "disable-service-access", 34 Description: T("Disable access to a service or service plan for one or all orgs"), 35 Usage: []string{ 36 "CF_NAME disable-service-access SERVICE [-p PLAN] [-o ORG]", 37 }, 38 Flags: fs, 39 } 40 } 41 42 func (cmd *DisableServiceAccess) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 43 if len(fc.Args()) != 1 { 44 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("disable-service-access")) 45 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 46 } 47 48 reqs := []requirements.Requirement{ 49 requirementsFactory.NewLoginRequirement(), 50 } 51 52 return reqs, nil 53 } 54 55 func (cmd *DisableServiceAccess) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 56 cmd.ui = deps.UI 57 cmd.config = deps.Config 58 cmd.actor = deps.ServicePlanHandler 59 cmd.tokenRefresher = deps.RepoLocator.GetAuthenticationRepository() 60 return cmd 61 } 62 63 func (cmd *DisableServiceAccess) Execute(c flags.FlagContext) error { 64 _, err := cmd.tokenRefresher.RefreshAuthToken() 65 if err != nil { 66 return err 67 } 68 69 serviceName := c.Args()[0] 70 planName := c.String("p") 71 orgName := c.String("o") 72 73 if planName != "" && orgName != "" { 74 err = cmd.disablePlanAndOrgForService(serviceName, planName, orgName) 75 } else if planName != "" { 76 err = cmd.disableSinglePlanForService(serviceName, planName) 77 } else if orgName != "" { 78 err = cmd.disablePlansForSingleOrgForService(serviceName, orgName) 79 } else { 80 err = cmd.disableServiceForAll(serviceName) 81 } 82 if err != nil { 83 return err 84 } 85 86 cmd.ui.Ok() 87 return nil 88 } 89 90 func (cmd *DisableServiceAccess) disableServiceForAll(serviceName string) error { 91 cmd.ui.Say(T("Disabling access to all plans of service {{.ServiceName}} for all orgs as {{.UserName}}...", 92 map[string]interface{}{ 93 "ServiceName": terminal.EntityNameColor(serviceName), 94 "UserName": terminal.EntityNameColor(cmd.config.Username()), 95 }, 96 )) 97 98 return cmd.actor.UpdateAllPlansForService(serviceName, false) 99 } 100 101 func (cmd *DisableServiceAccess) disablePlanAndOrgForService(serviceName string, planName string, orgName string) error { 102 cmd.ui.Say(T("Disabling access to plan {{.PlanName}} of service {{.ServiceName}} for org {{.OrgName}} as {{.Username}}...", 103 map[string]interface{}{ 104 "PlanName": terminal.EntityNameColor(planName), 105 "ServiceName": terminal.EntityNameColor(serviceName), 106 "OrgName": terminal.EntityNameColor(orgName), 107 "Username": terminal.EntityNameColor(cmd.config.Username()), 108 }, 109 )) 110 111 return cmd.actor.UpdatePlanAndOrgForService(serviceName, planName, orgName, false) 112 } 113 114 func (cmd *DisableServiceAccess) disableSinglePlanForService(serviceName string, planName string) error { 115 cmd.ui.Say(T("Disabling access of plan {{.PlanName}} for service {{.ServiceName}} as {{.Username}}...", 116 map[string]interface{}{ 117 "PlanName": terminal.EntityNameColor(planName), 118 "ServiceName": terminal.EntityNameColor(serviceName), 119 "Username": terminal.EntityNameColor(cmd.config.Username()), 120 }, 121 )) 122 123 return cmd.actor.UpdateSinglePlanForService(serviceName, planName, false) 124 } 125 126 func (cmd *DisableServiceAccess) disablePlansForSingleOrgForService(serviceName string, orgName string) error { 127 cmd.ui.Say(T("Disabling access to all plans of service {{.ServiceName}} for the org {{.OrgName}} as {{.Username}}...", 128 map[string]interface{}{ 129 "ServiceName": terminal.EntityNameColor(serviceName), 130 "OrgName": terminal.EntityNameColor(orgName), 131 "Username": terminal.EntityNameColor(cmd.config.Username()), 132 }, 133 )) 134 135 return cmd.actor.UpdateOrgForService(serviceName, orgName, false) 136 }