github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/service_command.go (about)

     1  package v2
     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/v2/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.displayBoundApplicationsIfExists(serviceInstanceSummary)
    95  		cmd.displayManagedServiceInstanceLastOperation(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  	}...)
   126  
   127  	cmd.UI.DisplayKeyValueTable("", table, 3)
   128  
   129  	if serviceInstanceSummary.IsNotShared() && serviceInstanceSummary.IsShareable() {
   130  		cmd.UI.DisplayNewline()
   131  		cmd.UI.DisplayText("This service is not currently shared.")
   132  		return
   133  	}
   134  
   135  	if serviceInstanceSummary.IsSharedTo() {
   136  		cmd.displayManagedServiceInstanceSharedWithInformation(serviceInstanceSummary)
   137  	}
   138  }
   139  
   140  func (cmd ServiceCommand) displayManagedServiceInstanceSharedWithInformation(serviceInstanceSummary v2action.ServiceInstanceSummary) {
   141  	if !serviceInstanceSummary.ServiceInstanceSharingFeatureFlag || !serviceInstanceSummary.Service.Extra.Shareable {
   142  		cmd.UI.DisplayNewline()
   143  		cmd.UI.DisplayText(translatableerror.ServiceInstanceNotShareableError{
   144  			FeatureFlagEnabled:          serviceInstanceSummary.ServiceInstanceSharingFeatureFlag,
   145  			ServiceBrokerSharingEnabled: serviceInstanceSummary.Service.Extra.Shareable,
   146  		}.Error())
   147  	}
   148  
   149  	cmd.UI.DisplayNewline()
   150  	cmd.UI.DisplayText("shared with spaces:")
   151  
   152  	sharedTosTable := [][]string{{
   153  		cmd.UI.TranslateText("org"),
   154  		cmd.UI.TranslateText("space"),
   155  		cmd.UI.TranslateText("bindings"),
   156  	}}
   157  
   158  	for _, sharedTo := range serviceInstanceSummary.ServiceInstanceSharedTos {
   159  		sharedTosTable = append(sharedTosTable, []string{
   160  			sharedTo.OrganizationName,
   161  			sharedTo.SpaceName,
   162  			fmt.Sprintf("%d", sharedTo.BoundAppCount),
   163  		})
   164  	}
   165  
   166  	cmd.UI.DisplayTableWithHeader("", sharedTosTable, 3)
   167  }
   168  
   169  func (cmd ServiceCommand) displayManagedServiceInstanceLastOperation(serviceInstanceSummary v2action.ServiceInstanceSummary) {
   170  	cmd.UI.DisplayNewline()
   171  	cmd.UI.DisplayText("Showing status of last operation from service {{.ServiceInstanceName}}...", map[string]interface{}{"ServiceInstanceName": serviceInstanceSummary.Name})
   172  	cmd.UI.DisplayNewline()
   173  	lastOperationTable := [][]string{
   174  		{cmd.UI.TranslateText("status:"), fmt.Sprintf("%s %s", serviceInstanceSummary.ServiceInstance.LastOperation.Type, serviceInstanceSummary.ServiceInstance.LastOperation.State)},
   175  		{cmd.UI.TranslateText("message:"), serviceInstanceSummary.ServiceInstance.LastOperation.Description},
   176  		{cmd.UI.TranslateText("started:"), serviceInstanceSummary.ServiceInstance.LastOperation.CreatedAt},
   177  		{cmd.UI.TranslateText("updated:"), serviceInstanceSummary.ServiceInstance.LastOperation.UpdatedAt},
   178  	}
   179  	cmd.UI.DisplayKeyValueTable("", lastOperationTable, 3)
   180  }
   181  
   182  func (cmd ServiceCommand) displayUserProvidedServiceInstanceSummary(serviceInstanceSummary v2action.ServiceInstanceSummary) {
   183  	table := [][]string{
   184  		{cmd.UI.TranslateText("name:"), serviceInstanceSummary.Name},
   185  		{cmd.UI.TranslateText("service:"), cmd.UI.TranslateText("user-provided")},
   186  		{cmd.UI.TranslateText("tags:"), strings.Join(serviceInstanceSummary.Tags, ", ")},
   187  	}
   188  	cmd.UI.DisplayKeyValueTable("", table, 3)
   189  }
   190  
   191  func (cmd ServiceCommand) displayBoundApplicationsIfExists(serviceInstanceSummary v2action.ServiceInstanceSummary) {
   192  
   193  	cmd.UI.DisplayNewline()
   194  
   195  	if len(serviceInstanceSummary.BoundApplications) == 0 {
   196  		cmd.UI.DisplayText("There are no bound apps for this service.")
   197  		return
   198  	}
   199  
   200  	cmd.UI.DisplayText("bound apps:")
   201  	boundAppsTable := [][]string{{
   202  		cmd.UI.TranslateText("name"),
   203  		cmd.UI.TranslateText("binding name"),
   204  	}}
   205  
   206  	for _, boundApplication := range serviceInstanceSummary.BoundApplications {
   207  		boundAppsTable = append(boundAppsTable, []string{
   208  			boundApplication.AppName,
   209  			boundApplication.ServiceBindingName,
   210  		})
   211  	}
   212  
   213  	cmd.UI.DisplayTableWithHeader("", boundAppsTable, 3)
   214  }