github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/serviceaccess/service_access.go (about) 1 package serviceaccess 2 3 import ( 4 "fmt" 5 "strings" 6 7 "code.cloudfoundry.org/cli/cf/actors" 8 "code.cloudfoundry.org/cli/cf/api/authentication" 9 "code.cloudfoundry.org/cli/cf/commandregistry" 10 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 11 "code.cloudfoundry.org/cli/cf/flags" 12 . "code.cloudfoundry.org/cli/cf/i18n" 13 "code.cloudfoundry.org/cli/cf/models" 14 "code.cloudfoundry.org/cli/cf/requirements" 15 "code.cloudfoundry.org/cli/cf/terminal" 16 ) 17 18 type ServiceAccess struct { 19 ui terminal.UI 20 config coreconfig.Reader 21 actor actors.ServiceActor 22 tokenRefresher authentication.TokenRefresher 23 } 24 25 func init() { 26 commandregistry.Register(&ServiceAccess{}) 27 } 28 29 func (cmd *ServiceAccess) MetaData() commandregistry.CommandMetadata { 30 fs := make(map[string]flags.FlagSet) 31 fs["b"] = &flags.StringFlag{ShortName: "b", Usage: T("Access for plans of a particular broker")} 32 fs["e"] = &flags.StringFlag{ShortName: "e", Usage: T("Access for service name of a particular service offering")} 33 fs["o"] = &flags.StringFlag{ShortName: "o", Usage: T("Plans accessible by a particular organization")} 34 35 return commandregistry.CommandMetadata{ 36 Name: "service-access", 37 Description: T("List service access settings"), 38 Usage: []string{ 39 "CF_NAME service-access [-b BROKER] [-e SERVICE] [-o ORG]", 40 }, 41 Flags: fs, 42 } 43 } 44 45 func (cmd *ServiceAccess) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 46 usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), 47 T("No argument required"), 48 func() bool { 49 return len(fc.Args()) != 0 50 }, 51 ) 52 53 reqs := []requirements.Requirement{ 54 usageReq, 55 requirementsFactory.NewLoginRequirement(), 56 } 57 58 return reqs, nil 59 } 60 61 func (cmd *ServiceAccess) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 62 cmd.ui = deps.UI 63 cmd.config = deps.Config 64 cmd.actor = deps.ServiceHandler 65 cmd.tokenRefresher = deps.RepoLocator.GetAuthenticationRepository() 66 return cmd 67 } 68 69 func (cmd *ServiceAccess) Execute(c flags.FlagContext) error { 70 _, err := cmd.tokenRefresher.RefreshAuthToken() 71 if err != nil { 72 return err 73 } 74 75 brokerName := c.String("b") 76 serviceName := c.String("e") 77 orgName := c.String("o") 78 79 if brokerName != "" && serviceName != "" && orgName != "" { 80 cmd.ui.Say(T("Getting service access for broker {{.Broker}} and service {{.Service}} and organization {{.Organization}} as {{.Username}}...", map[string]interface{}{ 81 "Broker": terminal.EntityNameColor(brokerName), 82 "Service": terminal.EntityNameColor(serviceName), 83 "Organization": terminal.EntityNameColor(orgName), 84 "Username": terminal.EntityNameColor(cmd.config.Username())})) 85 } else if serviceName != "" && orgName != "" { 86 cmd.ui.Say(T("Getting service access for service {{.Service}} and organization {{.Organization}} as {{.Username}}...", map[string]interface{}{ 87 "Service": terminal.EntityNameColor(serviceName), 88 "Organization": terminal.EntityNameColor(orgName), 89 "Username": terminal.EntityNameColor(cmd.config.Username())})) 90 } else if brokerName != "" && orgName != "" { 91 cmd.ui.Say(T("Getting service access for broker {{.Broker}} and organization {{.Organization}} as {{.Username}}...", map[string]interface{}{ 92 "Broker": terminal.EntityNameColor(brokerName), 93 "Organization": terminal.EntityNameColor(orgName), 94 "Username": terminal.EntityNameColor(cmd.config.Username())})) 95 } else if brokerName != "" && serviceName != "" { 96 cmd.ui.Say(T("Getting service access for broker {{.Broker}} and service {{.Service}} as {{.Username}}...", map[string]interface{}{ 97 "Broker": terminal.EntityNameColor(brokerName), 98 "Service": terminal.EntityNameColor(serviceName), 99 "Username": terminal.EntityNameColor(cmd.config.Username())})) 100 } else if brokerName != "" { 101 cmd.ui.Say(T("Getting service access for broker {{.Broker}} as {{.Username}}...", map[string]interface{}{ 102 "Broker": terminal.EntityNameColor(brokerName), 103 "Username": terminal.EntityNameColor(cmd.config.Username())})) 104 } else if serviceName != "" { 105 cmd.ui.Say(T("Getting service access for service {{.Service}} as {{.Username}}...", map[string]interface{}{ 106 "Service": terminal.EntityNameColor(serviceName), 107 "Username": terminal.EntityNameColor(cmd.config.Username())})) 108 } else if orgName != "" { 109 cmd.ui.Say(T("Getting service access for organization {{.Organization}} as {{.Username}}...", map[string]interface{}{ 110 "Organization": terminal.EntityNameColor(orgName), 111 "Username": terminal.EntityNameColor(cmd.config.Username())})) 112 } else { 113 cmd.ui.Say(T("Getting service access as {{.Username}}...", map[string]interface{}{ 114 "Username": terminal.EntityNameColor(cmd.config.Username())})) 115 } 116 117 brokers, err := cmd.actor.FilterBrokers(brokerName, serviceName, orgName) 118 if err != nil { 119 return err 120 } 121 cmd.printTable(brokers) 122 return nil 123 } 124 125 func (cmd ServiceAccess) printTable(brokers []models.ServiceBroker) error { 126 for _, serviceBroker := range brokers { 127 cmd.ui.Say(fmt.Sprintf(T("broker: {{.Name}}", map[string]interface{}{"Name": serviceBroker.Name}))) 128 129 table := cmd.ui.Table([]string{"", T("service"), T("plan"), T("access"), T("orgs")}) 130 for _, service := range serviceBroker.Services { 131 if len(service.Plans) > 0 { 132 for _, plan := range service.Plans { 133 table.Add("", service.Label, plan.Name, cmd.formatAccess(plan.Public, plan.OrgNames), strings.Join(plan.OrgNames, ",")) 134 } 135 } else { 136 table.Add("", service.Label, "", "", "") 137 } 138 } 139 err := table.Print() 140 if err != nil { 141 return err 142 } 143 144 cmd.ui.Say("") 145 } 146 return nil 147 } 148 149 func (cmd ServiceAccess) formatAccess(public bool, orgNames []string) string { 150 if public { 151 return T("all") 152 } 153 if len(orgNames) > 0 { 154 return T("limited") 155 } 156 return T("none") 157 }