github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/isolation_segments_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"strings"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     9  	"code.cloudfoundry.org/cli/command"
    10  	"code.cloudfoundry.org/cli/command/v6/shared"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  )
    13  
    14  //go:generate counterfeiter . IsolationSegmentsActor
    15  
    16  type IsolationSegmentsActor interface {
    17  	CloudControllerAPIVersion() string
    18  	GetIsolationSegmentSummaries() ([]v3action.IsolationSegmentSummary, v3action.Warnings, error)
    19  }
    20  
    21  type IsolationSegmentsCommand struct {
    22  	usage           interface{} `usage:"CF_NAME isolation-segments"`
    23  	relatedCommands interface{} `related_commands:"enable-org-isolation, create-isolation-segment"`
    24  
    25  	UI          command.UI
    26  	Config      command.Config
    27  	SharedActor command.SharedActor
    28  	Actor       IsolationSegmentsActor
    29  }
    30  
    31  func (cmd *IsolationSegmentsCommand) Setup(config command.Config, ui command.UI) error {
    32  	cmd.UI = ui
    33  	cmd.Config = config
    34  	sharedActor := sharedaction.NewActor(config)
    35  	cmd.SharedActor = sharedActor
    36  
    37  	client, _, err := shared.NewV3BasedClients(config, ui, true, "")
    38  	if err != nil {
    39  		return err
    40  	}
    41  	cmd.Actor = v3action.NewActor(client, config, sharedActor, nil)
    42  
    43  	return nil
    44  }
    45  
    46  func (cmd IsolationSegmentsCommand) Execute(args []string) error {
    47  	err := command.MinimumCCAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionIsolationSegmentV3)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	err = cmd.SharedActor.CheckTarget(false, false)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	user, err := cmd.Config.CurrentUser()
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	cmd.UI.DisplayTextWithFlavor("Getting isolation segments as {{.CurrentUser}}...", map[string]interface{}{
    63  		"CurrentUser": user.Name,
    64  	})
    65  
    66  	summaries, warnings, err := cmd.Actor.GetIsolationSegmentSummaries()
    67  	cmd.UI.DisplayWarnings(warnings)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	cmd.UI.DisplayOK()
    72  	cmd.UI.DisplayNewline()
    73  
    74  	table := [][]string{
    75  		{
    76  			cmd.UI.TranslateText("name"),
    77  			cmd.UI.TranslateText("orgs"),
    78  		},
    79  	}
    80  
    81  	for _, summary := range summaries {
    82  		table = append(
    83  			table,
    84  			[]string{
    85  				summary.Name,
    86  				strings.Join(summary.EntitledOrgs, ", "),
    87  			},
    88  		)
    89  	}
    90  
    91  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
    92  	return nil
    93  }