github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/service_command.go (about) 1 package v6 2 3 import ( 4 "fmt" 5 "strings" 6 7 "code.cloudfoundry.org/cli/actor/sharedaction" 8 "code.cloudfoundry.org/cli/actor/v2action" 9 "code.cloudfoundry.org/cli/command" 10 "code.cloudfoundry.org/cli/command/flag" 11 "code.cloudfoundry.org/cli/command/translatableerror" 12 "code.cloudfoundry.org/cli/command/v6/shared" 13 ) 14 15 //go:generate counterfeiter . ServiceActor 16 17 type ServiceActor interface { 18 GetServiceInstanceByNameAndSpace(name string, spaceGUID string) (v2action.ServiceInstance, v2action.Warnings, error) 19 GetServiceInstanceSummaryByNameAndSpace(name string, spaceGUID string) (v2action.ServiceInstanceSummary, v2action.Warnings, error) 20 } 21 22 type ServiceCommand struct { 23 RequiredArgs flag.ServiceInstance `positional-args:"yes"` 24 GUID bool `long:"guid" description:"Retrieve and display the given service's guid. All other output for the service is suppressed."` 25 usage interface{} `usage:"CF_NAME service SERVICE_INSTANCE"` 26 relatedCommands interface{} `related_commands:"bind-service, rename-service, update-service"` 27 28 UI command.UI 29 Config command.Config 30 SharedActor command.SharedActor 31 Actor ServiceActor 32 } 33 34 func (cmd *ServiceCommand) 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.NewClients(config, ui, true) 40 if err != nil { 41 return err 42 } 43 cmd.Actor = v2action.NewActor(ccClient, uaaClient, config) 44 45 return nil 46 } 47 48 func (cmd ServiceCommand) Execute(args []string) error { 49 err := cmd.SharedActor.CheckTarget(true, true) 50 if err != nil { 51 return err 52 } 53 54 if cmd.GUID { 55 return cmd.displayServiceInstanceGUID() 56 } 57 58 return cmd.displayServiceInstanceSummary() 59 } 60 61 func (cmd ServiceCommand) displayServiceInstanceGUID() error { 62 serviceInstance, warnings, err := cmd.Actor.GetServiceInstanceByNameAndSpace(cmd.RequiredArgs.ServiceInstance, cmd.Config.TargetedSpace().GUID) 63 cmd.UI.DisplayWarnings(warnings) 64 if err != nil { 65 return err 66 } 67 68 cmd.UI.DisplayText(serviceInstance.GUID) 69 return nil 70 } 71 72 func (cmd ServiceCommand) displayServiceInstanceSummary() error { 73 user, err := cmd.Config.CurrentUser() 74 if err != nil { 75 return err 76 } 77 78 cmd.UI.DisplayTextWithFlavor("Showing info of service {{.ServiceInstanceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.UserName}}...", map[string]interface{}{ 79 "ServiceInstanceName": cmd.RequiredArgs.ServiceInstance, 80 "OrgName": cmd.Config.TargetedOrganization().Name, 81 "SpaceName": cmd.Config.TargetedSpace().Name, 82 "UserName": user.Name, 83 }) 84 cmd.UI.DisplayNewline() 85 86 serviceInstanceSummary, warnings, err := cmd.Actor.GetServiceInstanceSummaryByNameAndSpace(cmd.RequiredArgs.ServiceInstance, cmd.Config.TargetedSpace().GUID) 87 cmd.UI.DisplayWarnings(warnings) 88 if err != nil { 89 return err 90 } 91 92 if serviceInstanceSummary.IsManaged() { 93 cmd.displayManagedServiceInstanceSummary(serviceInstanceSummary) 94 cmd.displayManagedServiceInstanceLastOperation(serviceInstanceSummary) 95 cmd.displayBoundApplicationsIfExists(serviceInstanceSummary) 96 return nil 97 } 98 99 cmd.displayUserProvidedServiceInstanceSummary(serviceInstanceSummary) 100 cmd.displayBoundApplicationsIfExists(serviceInstanceSummary) 101 return nil 102 } 103 104 func (cmd ServiceCommand) displayManagedServiceInstanceSummary(serviceInstanceSummary v2action.ServiceInstanceSummary) { 105 table := [][]string{{cmd.UI.TranslateText("name:"), serviceInstanceSummary.Name}} 106 107 if serviceInstanceSummary.IsSharedFrom() { 108 table = append(table, []string{ 109 cmd.UI.TranslateText("shared from org/space:"), 110 fmt.Sprintf( 111 "%s / %s", 112 serviceInstanceSummary.ServiceInstanceSharedFrom.OrganizationName, 113 serviceInstanceSummary.ServiceInstanceSharedFrom.SpaceName, 114 ), 115 }) 116 } 117 118 table = append(table, [][]string{ 119 {cmd.UI.TranslateText("service:"), serviceInstanceSummary.Service.Label}, 120 {cmd.UI.TranslateText("tags:"), strings.Join(serviceInstanceSummary.Tags, ", ")}, 121 {cmd.UI.TranslateText("plan:"), serviceInstanceSummary.ServicePlan.Name}, 122 {cmd.UI.TranslateText("description:"), serviceInstanceSummary.Service.Description}, 123 {cmd.UI.TranslateText("documentation:"), serviceInstanceSummary.Service.DocumentationURL}, 124 {cmd.UI.TranslateText("dashboard:"), serviceInstanceSummary.DashboardURL}, 125 {cmd.UI.TranslateText("service broker:"), serviceInstanceSummary.Service.ServiceBrokerName}, 126 }...) 127 128 cmd.UI.DisplayKeyValueTable("", table, 3) 129 130 if serviceInstanceSummary.IsNotShared() && serviceInstanceSummary.IsShareable() { 131 cmd.UI.DisplayNewline() 132 cmd.UI.DisplayText("This service is not currently shared.") 133 return 134 } 135 136 if serviceInstanceSummary.IsSharedTo() { 137 cmd.displayManagedServiceInstanceSharedWithInformation(serviceInstanceSummary) 138 } 139 } 140 141 func (cmd ServiceCommand) displayManagedServiceInstanceSharedWithInformation(serviceInstanceSummary v2action.ServiceInstanceSummary) { 142 if !serviceInstanceSummary.ServiceInstanceSharingFeatureFlag || !serviceInstanceSummary.Service.Extra.Shareable { 143 cmd.UI.DisplayNewline() 144 cmd.UI.DisplayText(translatableerror.ServiceInstanceNotShareableError{ 145 FeatureFlagEnabled: serviceInstanceSummary.ServiceInstanceSharingFeatureFlag, 146 ServiceBrokerSharingEnabled: serviceInstanceSummary.Service.Extra.Shareable, 147 }.Error()) 148 } 149 150 cmd.UI.DisplayNewline() 151 cmd.UI.DisplayText("shared with spaces:") 152 153 sharedTosTable := [][]string{{ 154 cmd.UI.TranslateText("org"), 155 cmd.UI.TranslateText("space"), 156 cmd.UI.TranslateText("bindings"), 157 }} 158 159 for _, sharedTo := range serviceInstanceSummary.ServiceInstanceSharedTos { 160 sharedTosTable = append(sharedTosTable, []string{ 161 sharedTo.OrganizationName, 162 sharedTo.SpaceName, 163 fmt.Sprintf("%d", sharedTo.BoundAppCount), 164 }) 165 } 166 167 cmd.UI.DisplayTableWithHeader("", sharedTosTable, 3) 168 } 169 170 func (cmd ServiceCommand) displayManagedServiceInstanceLastOperation(serviceInstanceSummary v2action.ServiceInstanceSummary) { 171 cmd.UI.DisplayNewline() 172 cmd.UI.DisplayText("Showing status of last operation from service {{.ServiceInstanceName}}...", map[string]interface{}{"ServiceInstanceName": serviceInstanceSummary.Name}) 173 cmd.UI.DisplayNewline() 174 lastOperationTable := [][]string{ 175 {cmd.UI.TranslateText("status:"), fmt.Sprintf("%s %s", serviceInstanceSummary.ServiceInstance.LastOperation.Type, serviceInstanceSummary.ServiceInstance.LastOperation.State)}, 176 {cmd.UI.TranslateText("message:"), serviceInstanceSummary.ServiceInstance.LastOperation.Description}, 177 {cmd.UI.TranslateText("started:"), serviceInstanceSummary.ServiceInstance.LastOperation.CreatedAt}, 178 {cmd.UI.TranslateText("updated:"), serviceInstanceSummary.ServiceInstance.LastOperation.UpdatedAt}, 179 } 180 cmd.UI.DisplayKeyValueTable("", lastOperationTable, 3) 181 } 182 183 func (cmd ServiceCommand) displayUserProvidedServiceInstanceSummary(serviceInstanceSummary v2action.ServiceInstanceSummary) { 184 table := [][]string{ 185 {cmd.UI.TranslateText("name:"), serviceInstanceSummary.Name}, 186 {cmd.UI.TranslateText("service:"), cmd.UI.TranslateText("user-provided")}, 187 {cmd.UI.TranslateText("tags:"), strings.Join(serviceInstanceSummary.Tags, ", ")}, 188 } 189 if serviceInstanceSummary.RouteServiceURL != "" { 190 table = append(table, []string{cmd.UI.TranslateText("route service url:"), serviceInstanceSummary.RouteServiceURL}) 191 } 192 cmd.UI.DisplayKeyValueTable("", table, 3) 193 } 194 195 func (cmd ServiceCommand) displayBoundApplicationsIfExists(serviceInstanceSummary v2action.ServiceInstanceSummary) { 196 cmd.UI.DisplayNewline() 197 198 if len(serviceInstanceSummary.BoundApplications) == 0 { 199 cmd.UI.DisplayText("There are no bound apps for this service.") 200 return 201 } 202 203 cmd.UI.DisplayText("bound apps:") 204 boundAppsTable := [][]string{{ 205 cmd.UI.TranslateText("name"), 206 cmd.UI.TranslateText("binding name"), 207 cmd.UI.TranslateText("status"), 208 cmd.UI.TranslateText("message"), 209 }} 210 211 for _, boundApplication := range serviceInstanceSummary.BoundApplications { 212 boundAppsTable = append(boundAppsTable, []string{ 213 boundApplication.AppName, 214 boundApplication.ServiceBindingName, 215 fmt.Sprintf("%s %s", boundApplication.LastOperation.Type, boundApplication.LastOperation.State), 216 boundApplication.LastOperation.Description, 217 }) 218 } 219 220 cmd.UI.DisplayTableWithHeader("", boundAppsTable, 3) 221 }