github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v6/service_access_command.go (about) 1 package v6 2 3 import ( 4 "sort" 5 "strings" 6 7 "code.cloudfoundry.org/cli/actor/sharedaction" 8 "code.cloudfoundry.org/cli/actor/v2action" 9 "code.cloudfoundry.org/cli/actor/v2action/composite" 10 "code.cloudfoundry.org/cli/command" 11 "code.cloudfoundry.org/cli/command/v6/shared" 12 "code.cloudfoundry.org/cli/util/sorting" 13 ) 14 15 //go:generate counterfeiter . ServiceAccessActor 16 17 type ServiceAccessActor interface { 18 GetServiceBrokerSummaries(broker string, service string, organization string) ([]v2action.ServiceBrokerSummary, v2action.Warnings, error) 19 } 20 21 type ServiceAccessCommand struct { 22 Broker string `short:"b" description:"Access for plans of a particular broker"` 23 Service string `short:"e" description:"Access for service name of a particular service offering"` 24 Organization string `short:"o" description:"Plans accessible by a particular organization"` 25 usage interface{} `usage:"CF_NAME service-access [-b BROKER] [-e SERVICE] [-o ORG]"` 26 relatedCommands interface{} `related_commands:"marketplace, disable-service-access, enable-service-access, service-brokers"` 27 28 UI command.UI 29 Config command.Config 30 SharedActor command.SharedActor 31 Actor ServiceAccessActor 32 } 33 34 func (cmd *ServiceAccessCommand) Setup(config command.Config, ui command.UI) error { 35 cmd.UI = ui 36 cmd.Config = config 37 cmd.SharedActor = sharedaction.NewActor(config) 38 39 ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui) 40 if err != nil { 41 return err 42 } 43 baseActor := v2action.NewActor(ccClient, uaaClient, config) 44 cmd.Actor = &composite.ServiceBrokerSummaryCompositeActor{ 45 ServiceActor: baseActor, 46 BrokerActor: baseActor, 47 OrgActor: baseActor, 48 VisibilityActor: baseActor, 49 } 50 51 return nil 52 } 53 54 func (cmd ServiceAccessCommand) Execute(args []string) error { 55 if err := cmd.SharedActor.CheckTarget(false, false); err != nil { 56 return err 57 } 58 59 user, err := cmd.Config.CurrentUser() 60 if err != nil { 61 return err 62 } 63 64 template := serviceAccessMessages[serviceAccessOptions{Broker: cmd.Broker != "", Service: cmd.Service != "", Org: cmd.Organization != ""}] 65 cmd.UI.DisplayTextWithFlavor(template, map[string]interface{}{ 66 "Broker": cmd.Broker, 67 "Service": cmd.Service, 68 "Org": cmd.Organization, 69 "CurrentUser": user.Name, 70 }) 71 72 summaries, warnings, err := cmd.Actor.GetServiceBrokerSummaries(cmd.Broker, cmd.Service, cmd.Organization) 73 cmd.UI.DisplayWarnings(warnings) 74 if err != nil { 75 return err 76 } 77 78 sortBrokers(summaries) 79 80 tableHeaders := []string{"service", "plan", "access", "orgs"} 81 for _, broker := range summaries { 82 cmd.UI.DisplayText("broker: {{.BrokerName}}", map[string]interface{}{ 83 "BrokerName": broker.Name, 84 }) 85 86 data := [][]string{tableHeaders} 87 88 for _, service := range broker.Services { 89 for _, plan := range service.Plans { 90 data = append(data, []string{ 91 service.Label, 92 plan.Name, 93 cmd.UI.TranslateText(formatAccess(plan)), 94 strings.Join(plan.VisibleTo, ","), 95 }) 96 } 97 } 98 99 cmd.UI.DisplayTableWithHeader(" ", data, 3) 100 cmd.UI.DisplayNewline() 101 } 102 103 return nil 104 } 105 106 func formatAccess(plan v2action.ServicePlanSummary) string { 107 if plan.Public { 108 return "all" 109 } 110 111 if len(plan.VisibleTo) > 0 { 112 return "limited" 113 } 114 115 return "none" 116 } 117 118 func sortBrokers(brokers []v2action.ServiceBrokerSummary) { 119 sort.Slice(brokers, func(i, j int) bool { 120 return sorting.LessIgnoreCase(brokers[i].Name, brokers[j].Name) 121 }) 122 123 for _, broker := range brokers { 124 sortServices(broker.Services) 125 } 126 } 127 128 func sortServices(services []v2action.ServiceSummary) { 129 sort.Slice(services, func(i, j int) bool { 130 return sorting.LessIgnoreCase(services[i].Label, services[j].Label) 131 }) 132 133 for _, service := range services { 134 sortPlans(service.Plans) 135 } 136 } 137 138 func sortPlans(plans []v2action.ServicePlanSummary) { 139 sort.Slice(plans, func(i, j int) bool { 140 return sorting.LessIgnoreCase(plans[i].Name, plans[j].Name) 141 }) 142 143 for _, plan := range plans { 144 sortOrgs(plan.VisibleTo) 145 } 146 } 147 148 func sortOrgs(orgs []string) { 149 sort.Strings(orgs) 150 } 151 152 type serviceAccessOptions struct { 153 Broker bool 154 Service bool 155 Org bool 156 } 157 158 var serviceAccessMessages = map[serviceAccessOptions]string{ 159 {Broker: false, Service: false, Org: false}: "Getting service access as {{.CurrentUser}}...", 160 {Broker: true, Service: false, Org: false}: "Getting service access for broker {{.Broker}} as {{.CurrentUser}}...", 161 {Broker: true, Service: true, Org: false}: "Getting service access for broker {{.Broker}} and service {{.Service}} as {{.CurrentUser}}...", 162 {Broker: true, Service: true, Org: true}: "Getting service access for broker {{.Broker}} and service {{.Service}} and organization {{.Org}} as {{.CurrentUser}}...", 163 {Broker: true, Service: false, Org: true}: "Getting service access for broker {{.Broker}} and organization {{.Org}} as {{.CurrentUser}}...", 164 {Broker: false, Service: true, Org: false}: "Getting service access for service {{.Service}} as {{.CurrentUser}}...", 165 {Broker: false, Service: true, Org: true}: "Getting service access for service {{.Service}} and organization {{.Org}} as {{.CurrentUser}}...", 166 {Broker: false, Service: false, Org: true}: "Getting service access for organization {{.Org}} as {{.CurrentUser}}...", 167 }