github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/service.go (about)

     1  package service
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/command_registry"
     5  	. "github.com/cloudfoundry/cli/cf/i18n"
     6  	"github.com/cloudfoundry/cli/cf/models"
     7  	"github.com/cloudfoundry/cli/cf/requirements"
     8  	"github.com/cloudfoundry/cli/cf/terminal"
     9  	"github.com/cloudfoundry/cli/flags"
    10  	"github.com/cloudfoundry/cli/flags/flag"
    11  	"github.com/cloudfoundry/cli/plugin/models"
    12  )
    13  
    14  type ShowService struct {
    15  	ui                 terminal.UI
    16  	serviceInstanceReq requirements.ServiceInstanceRequirement
    17  	pluginModel        *plugin_models.GetService_Model
    18  	pluginCall         bool
    19  }
    20  
    21  func init() {
    22  	command_registry.Register(&ShowService{})
    23  }
    24  
    25  func (cmd *ShowService) MetaData() command_registry.CommandMetadata {
    26  	fs := make(map[string]flags.FlagSet)
    27  	fs["guid"] = &cliFlags.BoolFlag{Name: "guid", Usage: T("Retrieve and display the given service's guid.  All other output for the service is suppressed.")}
    28  
    29  	return command_registry.CommandMetadata{
    30  		Name:        "service",
    31  		Description: T("Show service instance info"),
    32  		Usage:       T("CF_NAME service SERVICE_INSTANCE"),
    33  		Flags:       fs,
    34  	}
    35  }
    36  
    37  func (cmd *ShowService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    38  	if len(fc.Args()) != 1 {
    39  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("service"))
    40  	}
    41  
    42  	cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
    43  
    44  	reqs = []requirements.Requirement{
    45  		requirementsFactory.NewLoginRequirement(),
    46  		requirementsFactory.NewTargetedSpaceRequirement(),
    47  		cmd.serviceInstanceReq,
    48  	}
    49  
    50  	return
    51  }
    52  
    53  func (cmd *ShowService) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    54  	cmd.ui = deps.Ui
    55  
    56  	cmd.pluginCall = pluginCall
    57  	cmd.pluginModel = deps.PluginModels.Service
    58  
    59  	return cmd
    60  }
    61  
    62  func (cmd *ShowService) Execute(c flags.FlagContext) {
    63  	serviceInstance := cmd.serviceInstanceReq.GetServiceInstance()
    64  
    65  	if cmd.pluginCall {
    66  		cmd.populatePluginModel(serviceInstance)
    67  		return
    68  	}
    69  
    70  	if c.Bool("guid") {
    71  		cmd.ui.Say(serviceInstance.Guid)
    72  	} else {
    73  		cmd.ui.Say("")
    74  		cmd.ui.Say(T("Service instance: {{.ServiceName}}", map[string]interface{}{"ServiceName": terminal.EntityNameColor(serviceInstance.Name)}))
    75  
    76  		if serviceInstance.IsUserProvided() {
    77  			cmd.ui.Say(T("Service: {{.ServiceDescription}}",
    78  				map[string]interface{}{
    79  					"ServiceDescription": terminal.EntityNameColor(T("user-provided")),
    80  				}))
    81  		} else {
    82  			cmd.ui.Say(T("Service: {{.ServiceDescription}}",
    83  				map[string]interface{}{
    84  					"ServiceDescription": terminal.EntityNameColor(serviceInstance.ServiceOffering.Label),
    85  				}))
    86  			cmd.ui.Say(T("Plan: {{.ServicePlanName}}",
    87  				map[string]interface{}{
    88  					"ServicePlanName": terminal.EntityNameColor(serviceInstance.ServicePlan.Name),
    89  				}))
    90  			cmd.ui.Say(T("Description: {{.ServiceDescription}}", map[string]interface{}{"ServiceDescription": terminal.EntityNameColor(serviceInstance.ServiceOffering.Description)}))
    91  			cmd.ui.Say(T("Documentation url: {{.URL}}",
    92  				map[string]interface{}{
    93  					"URL": terminal.EntityNameColor(serviceInstance.ServiceOffering.DocumentationUrl),
    94  				}))
    95  			cmd.ui.Say(T("Dashboard: {{.URL}}",
    96  				map[string]interface{}{
    97  					"URL": terminal.EntityNameColor(serviceInstance.DashboardUrl),
    98  				}))
    99  			cmd.ui.Say("")
   100  			cmd.ui.Say(T("Last Operation"))
   101  			cmd.ui.Say(T("Status: {{.State}}",
   102  				map[string]interface{}{
   103  					"State": terminal.EntityNameColor(ServiceInstanceStateToStatus(serviceInstance.LastOperation.Type, serviceInstance.LastOperation.State, serviceInstance.IsUserProvided())),
   104  				}))
   105  			cmd.ui.Say(T("Message: {{.Message}}",
   106  				map[string]interface{}{
   107  					"Message": terminal.EntityNameColor(serviceInstance.LastOperation.Description),
   108  				}))
   109  			if "" != serviceInstance.LastOperation.CreatedAt {
   110  				cmd.ui.Say(T("Started: {{.Started}}",
   111  					map[string]interface{}{
   112  						"Started": terminal.EntityNameColor(serviceInstance.LastOperation.CreatedAt),
   113  					}))
   114  			}
   115  			cmd.ui.Say(T("Updated: {{.Updated}}",
   116  				map[string]interface{}{
   117  					"Updated": terminal.EntityNameColor(serviceInstance.LastOperation.UpdatedAt),
   118  				}))
   119  		}
   120  	}
   121  }
   122  
   123  func ServiceInstanceStateToStatus(operationType string, state string, isUserProvidedService bool) string {
   124  	if isUserProvidedService {
   125  		return ""
   126  	}
   127  
   128  	switch state {
   129  	case "in progress":
   130  		return T("{{.OperationType}} in progress", map[string]interface{}{"OperationType": operationType})
   131  	case "failed":
   132  		return T("{{.OperationType}} failed", map[string]interface{}{"OperationType": operationType})
   133  	case "succeeded":
   134  		return T("{{.OperationType}} succeeded", map[string]interface{}{"OperationType": operationType})
   135  	default:
   136  		return ""
   137  	}
   138  }
   139  
   140  func (cmd *ShowService) populatePluginModel(serviceInstance models.ServiceInstance) {
   141  	cmd.pluginModel.Name = serviceInstance.Name
   142  	cmd.pluginModel.Guid = serviceInstance.Guid
   143  	cmd.pluginModel.DashboardUrl = serviceInstance.DashboardUrl
   144  	cmd.pluginModel.IsUserProvided = serviceInstance.IsUserProvided()
   145  	cmd.pluginModel.LastOperation.Type = serviceInstance.LastOperation.Type
   146  	cmd.pluginModel.LastOperation.State = serviceInstance.LastOperation.State
   147  	cmd.pluginModel.LastOperation.Description = serviceInstance.LastOperation.Description
   148  	cmd.pluginModel.LastOperation.CreatedAt = serviceInstance.LastOperation.CreatedAt
   149  	cmd.pluginModel.LastOperation.UpdatedAt = serviceInstance.LastOperation.UpdatedAt
   150  	cmd.pluginModel.ServicePlan.Name = serviceInstance.ServicePlan.Name
   151  	cmd.pluginModel.ServicePlan.Guid = serviceInstance.ServicePlan.Guid
   152  	cmd.pluginModel.ServiceOffering.DocumentationUrl = serviceInstance.ServiceOffering.DocumentationUrl
   153  	cmd.pluginModel.ServiceOffering.Name = serviceInstance.ServiceOffering.Label
   154  }