code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/service_access_command.go (about) 1 package v7 2 3 import ( 4 "strings" 5 6 "code.cloudfoundry.org/cli/util/ui" 7 8 "code.cloudfoundry.org/cli/actor/v7action" 9 "code.cloudfoundry.org/cli/command" 10 ) 11 12 type ServiceAccessCommand struct { 13 BaseCommand 14 15 Broker string `short:"b" description:"Access for plans of a particular broker"` 16 ServiceOffering string `short:"e" description:"Access for plans of a particular service offering"` 17 Organization string `short:"o" description:"Plans accessible by a particular organization"` 18 usage interface{} `usage:"CF_NAME service-access [-b BROKER] [-e SERVICE] [-o ORG]"` 19 relatedCommands interface{} `related_commands:"marketplace, disable-service-access, enable-service-access, service-brokers"` 20 } 21 22 func (cmd ServiceAccessCommand) Execute(args []string) error { 23 if err := cmd.SharedActor.CheckTarget(false, false); err != nil { 24 return err 25 } 26 27 if err := cmd.displayMessage(); err != nil { 28 return err 29 } 30 31 servicePlanAccess, warnings, err := cmd.Actor.GetServiceAccess(cmd.ServiceOffering, cmd.Broker, cmd.Organization) 32 cmd.UI.DisplayWarnings(warnings) 33 if err != nil { 34 return err 35 } 36 37 if len(servicePlanAccess) == 0 { 38 cmd.UI.DisplayText("No service plans found.") 39 return nil 40 } 41 42 var data [][]string 43 44 for index, plan := range servicePlanAccess { 45 if len(data) == 0 { 46 data = [][]string{getTableHeaders(plan)} 47 } 48 49 data = append(data, []string{ 50 plan.ServiceOfferingName, 51 plan.ServicePlanName, 52 accessFromVisibilityType(string(plan.VisibilityType)), 53 strings.Join(plan.VisibilityDetails, ","), 54 }) 55 56 endOfList := (index + 1) == len(servicePlanAccess) 57 58 endOfGrouping := endOfList || plan.BrokerName != servicePlanAccess[index+1].BrokerName 59 60 if endOfGrouping { 61 cmd.UI.DisplayText("broker: {{.BrokerName}}", map[string]interface{}{ 62 "BrokerName": plan.BrokerName, 63 }) 64 cmd.UI.DisplayTableWithHeader(" ", data, ui.DefaultTableSpacePadding) 65 66 data = nil 67 } 68 69 if endOfGrouping && !endOfList { 70 cmd.UI.DisplayNewline() 71 } 72 } 73 74 return nil 75 } 76 77 func getTableHeaders(plan v7action.ServicePlanAccess) []string { 78 if string(plan.VisibilityType) == "space" { 79 return []string{"offering", "plan", "access", "space"} 80 } 81 return []string{"offering", "plan", "access", "orgs"} 82 } 83 84 func accessFromVisibilityType(visibilityType string) string { 85 switch visibilityType { 86 case "public": 87 return "all" 88 case "admin": 89 return "none" 90 default: 91 return "limited" 92 } 93 } 94 95 func (cmd ServiceAccessCommand) displayMessage() error { 96 user, err := cmd.Config.CurrentUser() 97 if err != nil { 98 return err 99 } 100 101 getServiceAccessMessage{ 102 Broker: cmd.Broker, 103 ServiceOffering: cmd.ServiceOffering, 104 Organization: cmd.Organization, 105 User: user.Name, 106 }.displayMessage(cmd.UI) 107 108 cmd.UI.DisplayNewline() 109 110 return nil 111 } 112 113 type getServiceAccessMessage struct { 114 Broker, ServiceOffering, Organization, User string 115 } 116 117 func (msg getServiceAccessMessage) displayMessage(ui command.UI) { 118 var resources []string 119 120 if msg.Broker != "" { 121 resources = append(resources, "broker {{.Broker}}") 122 } 123 124 if msg.ServiceOffering != "" { 125 resources = append(resources, "service offering {{.ServiceOffering}}") 126 } 127 128 if msg.Organization != "" { 129 resources = append(resources, "organization {{.Org}}") 130 } 131 132 template := "Getting service access" 133 134 if len(resources) != 0 { 135 template += " for " + strings.Join(resources, " and ") 136 } 137 138 template += " as {{.CurrentUser}}..." 139 140 ui.DisplayTextWithFlavor(template, map[string]interface{}{ 141 "Broker": msg.Broker, 142 "ServiceOffering": msg.ServiceOffering, 143 "Org": msg.Organization, 144 "CurrentUser": msg.User, 145 }) 146 }