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