github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/service/service.go (about) 1 package service 2 3 import ( 4 "fmt" 5 "strings" 6 7 "code.cloudfoundry.org/cli/cf/api/applications" 8 "code.cloudfoundry.org/cli/cf/commandregistry" 9 "code.cloudfoundry.org/cli/cf/flags" 10 . "code.cloudfoundry.org/cli/cf/i18n" 11 "code.cloudfoundry.org/cli/cf/models" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 "code.cloudfoundry.org/cli/plugin/models" 15 ) 16 17 type ShowService struct { 18 ui terminal.UI 19 serviceInstanceReq requirements.ServiceInstanceRequirement 20 pluginModel *plugin_models.GetService_Model 21 pluginCall bool 22 appRepo applications.Repository 23 } 24 25 func init() { 26 commandregistry.Register(&ShowService{}) 27 } 28 29 func (cmd *ShowService) MetaData() commandregistry.CommandMetadata { 30 fs := make(map[string]flags.FlagSet) 31 fs["guid"] = &flags.BoolFlag{Name: "guid", Usage: T("Retrieve and display the given service's guid. All other output for the service is suppressed.")} 32 T("user-provided") 33 34 return commandregistry.CommandMetadata{ 35 Name: "service", 36 Description: T("Show service instance info"), 37 Usage: []string{ 38 T("CF_NAME service SERVICE_INSTANCE"), 39 }, 40 Flags: fs, 41 } 42 } 43 44 func (cmd *ShowService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 45 if len(fc.Args()) != 1 { 46 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("service")) 47 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 48 } 49 50 cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0]) 51 52 reqs := []requirements.Requirement{ 53 requirementsFactory.NewLoginRequirement(), 54 requirementsFactory.NewTargetedSpaceRequirement(), 55 cmd.serviceInstanceReq, 56 } 57 58 return reqs, nil 59 } 60 61 func (cmd *ShowService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 62 cmd.ui = deps.UI 63 64 cmd.pluginCall = pluginCall 65 cmd.pluginModel = deps.PluginModels.Service 66 cmd.appRepo = deps.RepoLocator.GetApplicationRepository() 67 68 return cmd 69 } 70 71 func (cmd *ShowService) Execute(c flags.FlagContext) error { 72 serviceInstance := cmd.serviceInstanceReq.GetServiceInstance() 73 74 boundApps := []string{} 75 for _, serviceBinding := range serviceInstance.ServiceBindings { 76 app, err := cmd.appRepo.GetApp(serviceBinding.AppGUID) 77 if err != nil { 78 cmd.ui.Warn(T("Unable to retrieve information for bound application GUID " + serviceBinding.AppGUID)) 79 } 80 boundApps = append(boundApps, app.ApplicationFields.Name) 81 } 82 83 if cmd.pluginCall { 84 cmd.populatePluginModel(serviceInstance) 85 return nil 86 } 87 88 if c.Bool("guid") { 89 cmd.ui.Say(serviceInstance.GUID) 90 } else { 91 cmd.ui.Say("") 92 cmd.ui.Say(T("Service instance: {{.ServiceName}}", map[string]interface{}{"ServiceName": terminal.EntityNameColor(serviceInstance.Name)})) 93 94 if serviceInstance.IsUserProvided() { 95 cmd.ui.Say(T("Service: {{.ServiceDescription}}", 96 map[string]interface{}{ 97 "ServiceDescription": terminal.EntityNameColor(T("user-provided")), 98 })) 99 cmd.ui.Say(T("Bound apps: {{.BoundApplications}}", 100 map[string]interface{}{ 101 "BoundApplications": terminal.EntityNameColor(strings.Join(boundApps, ",")), 102 })) 103 } else { 104 cmd.ui.Say(T("Service: {{.ServiceDescription}}", 105 map[string]interface{}{ 106 "ServiceDescription": terminal.EntityNameColor(serviceInstance.ServiceOffering.Label), 107 })) 108 cmd.ui.Say(T("Bound apps: {{.BoundApplications}}", 109 map[string]interface{}{ 110 "BoundApplications": terminal.EntityNameColor(strings.Join(boundApps, ",")), 111 })) 112 cmd.ui.Say(T("Tags: {{.Tags}}", 113 map[string]interface{}{ 114 "Tags": terminal.EntityNameColor(strings.Join(serviceInstance.Tags, ", ")), 115 })) 116 cmd.ui.Say(T("Plan: {{.ServicePlanName}}", 117 map[string]interface{}{ 118 "ServicePlanName": terminal.EntityNameColor(serviceInstance.ServicePlan.Name), 119 })) 120 cmd.ui.Say(T("Description: {{.ServiceDescription}}", map[string]interface{}{"ServiceDescription": terminal.EntityNameColor(serviceInstance.ServiceOffering.Description)})) 121 cmd.ui.Say(T("Documentation url: {{.URL}}", 122 map[string]interface{}{ 123 "URL": terminal.EntityNameColor(serviceInstance.ServiceOffering.DocumentationURL), 124 })) 125 cmd.ui.Say(T("Dashboard: {{.URL}}", 126 map[string]interface{}{ 127 "URL": terminal.EntityNameColor(serviceInstance.DashboardURL), 128 })) 129 cmd.ui.Say("") 130 cmd.ui.Say(T("Last Operation")) 131 cmd.ui.Say(T("Status: {{.State}}", 132 map[string]interface{}{ 133 "State": terminal.EntityNameColor(InstanceStateToStatus(serviceInstance.LastOperation.Type, serviceInstance.LastOperation.State, serviceInstance.IsUserProvided())), 134 })) 135 cmd.ui.Say(T("Message: {{.Message}}", 136 map[string]interface{}{ 137 "Message": terminal.EntityNameColor(serviceInstance.LastOperation.Description), 138 })) 139 if "" != serviceInstance.LastOperation.CreatedAt { 140 cmd.ui.Say(T("Started: {{.Started}}", 141 map[string]interface{}{ 142 "Started": terminal.EntityNameColor(serviceInstance.LastOperation.CreatedAt), 143 })) 144 } 145 cmd.ui.Say(T("Updated: {{.Updated}}", 146 map[string]interface{}{ 147 "Updated": terminal.EntityNameColor(serviceInstance.LastOperation.UpdatedAt), 148 })) 149 } 150 } 151 return nil 152 } 153 154 func InstanceStateToStatus(operationType string, state string, isUserProvidedService bool) string { 155 if isUserProvidedService { 156 return "" 157 } 158 159 switch state { 160 case "in progress": 161 return T("{{.OperationType}} in progress", map[string]interface{}{"OperationType": operationType}) 162 case "failed": 163 return T("{{.OperationType}} failed", map[string]interface{}{"OperationType": operationType}) 164 case "succeeded": 165 return T("{{.OperationType}} succeeded", map[string]interface{}{"OperationType": operationType}) 166 default: 167 return "" 168 } 169 } 170 171 func (cmd *ShowService) populatePluginModel(serviceInstance models.ServiceInstance) { 172 cmd.pluginModel.Name = serviceInstance.Name 173 cmd.pluginModel.Guid = serviceInstance.GUID 174 cmd.pluginModel.DashboardUrl = serviceInstance.DashboardURL 175 cmd.pluginModel.IsUserProvided = serviceInstance.IsUserProvided() 176 cmd.pluginModel.LastOperation.Type = serviceInstance.LastOperation.Type 177 cmd.pluginModel.LastOperation.State = serviceInstance.LastOperation.State 178 cmd.pluginModel.LastOperation.Description = serviceInstance.LastOperation.Description 179 cmd.pluginModel.LastOperation.CreatedAt = serviceInstance.LastOperation.CreatedAt 180 cmd.pluginModel.LastOperation.UpdatedAt = serviceInstance.LastOperation.UpdatedAt 181 cmd.pluginModel.ServicePlan.Name = serviceInstance.ServicePlan.Name 182 cmd.pluginModel.ServicePlan.Guid = serviceInstance.ServicePlan.GUID 183 cmd.pluginModel.ServiceOffering.DocumentationUrl = serviceInstance.ServiceOffering.DocumentationURL 184 cmd.pluginModel.ServiceOffering.Name = serviceInstance.ServiceOffering.Label 185 }