github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+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  
    33  	return commandregistry.CommandMetadata{
    34  		Name:        "service",
    35  		Description: T("Show service instance info"),
    36  		Usage: []string{
    37  			T("CF_NAME service SERVICE_INSTANCE"),
    38  		},
    39  		Flags: fs,
    40  	}
    41  }
    42  
    43  func (cmd *ShowService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    44  	if len(fc.Args()) != 1 {
    45  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("service"))
    46  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    47  	}
    48  
    49  	cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
    50  
    51  	reqs := []requirements.Requirement{
    52  		requirementsFactory.NewLoginRequirement(),
    53  		requirementsFactory.NewTargetedSpaceRequirement(),
    54  		cmd.serviceInstanceReq,
    55  	}
    56  
    57  	return reqs, nil
    58  }
    59  
    60  func (cmd *ShowService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    61  	cmd.ui = deps.UI
    62  
    63  	cmd.pluginCall = pluginCall
    64  	cmd.pluginModel = deps.PluginModels.Service
    65  	cmd.appRepo = deps.RepoLocator.GetApplicationRepository()
    66  
    67  	return cmd
    68  }
    69  
    70  func (cmd *ShowService) Execute(c flags.FlagContext) error {
    71  	serviceInstance := cmd.serviceInstanceReq.GetServiceInstance()
    72  
    73  	boundApps := []string{}
    74  	for _, serviceBinding := range serviceInstance.ServiceBindings {
    75  		app, err := cmd.appRepo.GetApp(serviceBinding.AppGUID)
    76  		if err != nil {
    77  			cmd.ui.Warn(T("Unable to retrieve information for bound application GUID " + serviceBinding.AppGUID))
    78  		}
    79  		boundApps = append(boundApps, app.ApplicationFields.Name)
    80  	}
    81  
    82  	if cmd.pluginCall {
    83  		cmd.populatePluginModel(serviceInstance)
    84  		return nil
    85  	}
    86  
    87  	if c.Bool("guid") {
    88  		cmd.ui.Say(serviceInstance.GUID)
    89  	} else {
    90  		cmd.ui.Say("")
    91  		cmd.ui.Say(T("Service instance: {{.ServiceName}}", map[string]interface{}{"ServiceName": terminal.EntityNameColor(serviceInstance.Name)}))
    92  
    93  		if serviceInstance.IsUserProvided() {
    94  			cmd.ui.Say(T("Service: {{.ServiceDescription}}",
    95  				map[string]interface{}{
    96  					"ServiceDescription": terminal.EntityNameColor(T("user-provided")),
    97  				}))
    98  			cmd.ui.Say(T("Bound apps: {{.BoundApplications}}",
    99  				map[string]interface{}{
   100  					"BoundApplications": terminal.EntityNameColor(strings.Join(boundApps, ",")),
   101  				}))
   102  		} else {
   103  			cmd.ui.Say(T("Service: {{.ServiceDescription}}",
   104  				map[string]interface{}{
   105  					"ServiceDescription": terminal.EntityNameColor(serviceInstance.ServiceOffering.Label),
   106  				}))
   107  			cmd.ui.Say(T("Bound apps: {{.BoundApplications}}",
   108  				map[string]interface{}{
   109  					"BoundApplications": terminal.EntityNameColor(strings.Join(boundApps, ",")),
   110  				}))
   111  			cmd.ui.Say(T("Tags: {{.Tags}}",
   112  				map[string]interface{}{
   113  					"Tags": terminal.EntityNameColor(strings.Join(serviceInstance.Tags, ", ")),
   114  				}))
   115  			cmd.ui.Say(T("Plan: {{.ServicePlanName}}",
   116  				map[string]interface{}{
   117  					"ServicePlanName": terminal.EntityNameColor(serviceInstance.ServicePlan.Name),
   118  				}))
   119  			cmd.ui.Say(T("Description: {{.ServiceDescription}}", map[string]interface{}{"ServiceDescription": terminal.EntityNameColor(serviceInstance.ServiceOffering.Description)}))
   120  			cmd.ui.Say(T("Documentation url: {{.URL}}",
   121  				map[string]interface{}{
   122  					"URL": terminal.EntityNameColor(serviceInstance.ServiceOffering.DocumentationURL),
   123  				}))
   124  			cmd.ui.Say(T("Dashboard: {{.URL}}",
   125  				map[string]interface{}{
   126  					"URL": terminal.EntityNameColor(serviceInstance.DashboardURL),
   127  				}))
   128  			cmd.ui.Say("")
   129  			cmd.ui.Say(T("Last Operation"))
   130  			cmd.ui.Say(T("Status: {{.State}}",
   131  				map[string]interface{}{
   132  					"State": terminal.EntityNameColor(InstanceStateToStatus(serviceInstance.LastOperation.Type, serviceInstance.LastOperation.State, serviceInstance.IsUserProvided())),
   133  				}))
   134  			cmd.ui.Say(T("Message: {{.Message}}",
   135  				map[string]interface{}{
   136  					"Message": terminal.EntityNameColor(serviceInstance.LastOperation.Description),
   137  				}))
   138  			if "" != serviceInstance.LastOperation.CreatedAt {
   139  				cmd.ui.Say(T("Started: {{.Started}}",
   140  					map[string]interface{}{
   141  						"Started": terminal.EntityNameColor(serviceInstance.LastOperation.CreatedAt),
   142  					}))
   143  			}
   144  			cmd.ui.Say(T("Updated: {{.Updated}}",
   145  				map[string]interface{}{
   146  					"Updated": terminal.EntityNameColor(serviceInstance.LastOperation.UpdatedAt),
   147  				}))
   148  		}
   149  	}
   150  	return nil
   151  }
   152  
   153  func InstanceStateToStatus(operationType string, state string, isUserProvidedService bool) string {
   154  	if isUserProvidedService {
   155  		return ""
   156  	}
   157  
   158  	switch state {
   159  	case "in progress":
   160  		return T("{{.OperationType}} in progress", map[string]interface{}{"OperationType": operationType})
   161  	case "failed":
   162  		return T("{{.OperationType}} failed", map[string]interface{}{"OperationType": operationType})
   163  	case "succeeded":
   164  		return T("{{.OperationType}} succeeded", map[string]interface{}{"OperationType": operationType})
   165  	default:
   166  		return ""
   167  	}
   168  }
   169  
   170  func (cmd *ShowService) populatePluginModel(serviceInstance models.ServiceInstance) {
   171  	cmd.pluginModel.Name = serviceInstance.Name
   172  	cmd.pluginModel.Guid = serviceInstance.GUID
   173  	cmd.pluginModel.DashboardUrl = serviceInstance.DashboardURL
   174  	cmd.pluginModel.IsUserProvided = serviceInstance.IsUserProvided()
   175  	cmd.pluginModel.LastOperation.Type = serviceInstance.LastOperation.Type
   176  	cmd.pluginModel.LastOperation.State = serviceInstance.LastOperation.State
   177  	cmd.pluginModel.LastOperation.Description = serviceInstance.LastOperation.Description
   178  	cmd.pluginModel.LastOperation.CreatedAt = serviceInstance.LastOperation.CreatedAt
   179  	cmd.pluginModel.LastOperation.UpdatedAt = serviceInstance.LastOperation.UpdatedAt
   180  	cmd.pluginModel.ServicePlan.Name = serviceInstance.ServicePlan.Name
   181  	cmd.pluginModel.ServicePlan.Guid = serviceInstance.ServicePlan.GUID
   182  	cmd.pluginModel.ServiceOffering.DocumentationUrl = serviceInstance.ServiceOffering.DocumentationURL
   183  	cmd.pluginModel.ServiceOffering.Name = serviceInstance.ServiceOffering.Label
   184  }