github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/isolation_segments_command.go (about)

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