github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v6/services_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/actor/sharedaction"
     9  	"code.cloudfoundry.org/cli/actor/v2action"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    11  	"code.cloudfoundry.org/cli/command"
    12  	"code.cloudfoundry.org/cli/command/v6/shared"
    13  	"code.cloudfoundry.org/cli/util/sorting"
    14  )
    15  
    16  //go:generate counterfeiter . ServiceInstancesActor
    17  
    18  type ServiceInstancesActor interface {
    19  	GetServiceInstancesSummaryBySpace(spaceGUID string) ([]v2action.ServiceInstanceSummary, v2action.Warnings, error)
    20  }
    21  
    22  type ServicesCommand struct {
    23  	usage           interface{} `usage:"CF_NAME services"`
    24  	relatedCommands interface{} `related_commands:"create-service, marketplace"`
    25  
    26  	UI          command.UI
    27  	Config      command.Config
    28  	SharedActor command.SharedActor
    29  	Actor       ServiceInstancesActor
    30  }
    31  
    32  func (cmd *ServicesCommand) Setup(config command.Config, ui command.UI) error {
    33  	cmd.Config = config
    34  	cmd.UI = ui
    35  	cmd.SharedActor = sharedaction.NewActor(config)
    36  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    41  
    42  	return nil
    43  }
    44  
    45  func (cmd ServicesCommand) Execute(args []string) error {
    46  	err := cmd.SharedActor.CheckTarget(true, true)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	user, err := cmd.Config.CurrentUser()
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	cmd.UI.DisplayTextWithFlavor("Getting services in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    57  		map[string]interface{}{
    58  			"OrgName":     cmd.Config.TargetedOrganization().Name,
    59  			"SpaceName":   cmd.Config.TargetedSpace().Name,
    60  			"CurrentUser": user.Name,
    61  		})
    62  	cmd.UI.DisplayNewline()
    63  
    64  	instanceSummaries, warnings, err := cmd.Actor.GetServiceInstancesSummaryBySpace(cmd.Config.TargetedSpace().GUID)
    65  	cmd.UI.DisplayWarnings(warnings)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	if len(instanceSummaries) == 0 {
    71  		cmd.UI.DisplayText("No services found")
    72  		return nil
    73  	}
    74  
    75  	sortServiceInstances(instanceSummaries)
    76  
    77  	table := [][]string{{
    78  		cmd.UI.TranslateText("name"),
    79  		cmd.UI.TranslateText("service"),
    80  		cmd.UI.TranslateText("plan"),
    81  		cmd.UI.TranslateText("bound apps"),
    82  		cmd.UI.TranslateText("last operation"),
    83  		cmd.UI.TranslateText("broker"),
    84  		cmd.UI.TranslateText("upgrade available"),
    85  	}}
    86  
    87  	var boundAppNames []string
    88  
    89  	for _, summary := range instanceSummaries {
    90  		serviceLabel := summary.Service.Label
    91  		if summary.ServiceInstance.Type == constant.UserProvidedService {
    92  			serviceLabel = "user-provided"
    93  		}
    94  
    95  		boundAppNames = []string{}
    96  		for _, boundApplication := range summary.BoundApplications {
    97  			boundAppNames = append(boundAppNames, boundApplication.AppName)
    98  		}
    99  
   100  		table = append(
   101  			table,
   102  			[]string{
   103  				summary.Name,
   104  				serviceLabel,
   105  				summary.ServicePlan.Name,
   106  				strings.Join(boundAppNames, ", "),
   107  				fmt.Sprintf("%s %s", summary.LastOperation.Type, summary.LastOperation.State),
   108  				summary.Service.ServiceBrokerName,
   109  				summary.UpgradeAvailable(),
   110  			},
   111  		)
   112  	}
   113  	cmd.UI.DisplayTableWithHeader("", table, 3)
   114  
   115  	return nil
   116  }
   117  
   118  func sortServiceInstances(instanceSummaries []v2action.ServiceInstanceSummary) {
   119  	sort.Slice(instanceSummaries, func(i, j int) bool {
   120  		return sorting.LessIgnoreCase(instanceSummaries[i].Name, instanceSummaries[j].Name)
   121  	})
   122  
   123  	for _, instance := range instanceSummaries {
   124  		sortBoundApps(instance)
   125  	}
   126  }
   127  
   128  func sortBoundApps(serviceInstance v2action.ServiceInstanceSummary) {
   129  	sort.Slice(
   130  		serviceInstance.BoundApplications,
   131  		func(i, j int) bool {
   132  			return sorting.LessIgnoreCase(serviceInstance.BoundApplications[i].AppName, serviceInstance.BoundApplications[j].AppName)
   133  		})
   134  }