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

     1  package v6
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/sharedaction"
     6  	"code.cloudfoundry.org/cli/actor/v3action"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     8  	"code.cloudfoundry.org/cli/command"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	"code.cloudfoundry.org/cli/command/v6/shared"
    11  )
    12  
    13  //go:generate counterfeiter . CreateIsolationSegmentActor
    14  
    15  type CreateIsolationSegmentActor interface {
    16  	CloudControllerAPIVersion() string
    17  	CreateIsolationSegmentByName(isolationSegment v3action.IsolationSegment) (v3action.Warnings, error)
    18  }
    19  
    20  type CreateIsolationSegmentCommand struct {
    21  	RequiredArgs    flag.IsolationSegmentName `positional-args:"yes"`
    22  	usage           interface{}               `usage:"CF_NAME create-isolation-segment SEGMENT_NAME\n\nNOTES:\n   The isolation segment name must match the placement tag applied to the Diego cell."`
    23  	relatedCommands interface{}               `related_commands:"enable-org-isolation, isolation-segments"`
    24  
    25  	UI          command.UI
    26  	Config      command.Config
    27  	SharedActor command.SharedActor
    28  	Actor       CreateIsolationSegmentActor
    29  }
    30  
    31  func (cmd *CreateIsolationSegmentCommand) Setup(config command.Config, ui command.UI) error {
    32  	cmd.UI = ui
    33  	cmd.Config = config
    34  	cmd.SharedActor = sharedaction.NewActor(config)
    35  
    36  	client, _, err := shared.NewV3BasedClients(config, ui, true, "")
    37  	if err != nil {
    38  		return err
    39  	}
    40  	cmd.Actor = v3action.NewActor(client, config, nil, nil)
    41  
    42  	return nil
    43  }
    44  
    45  func (cmd CreateIsolationSegmentCommand) Execute(args []string) error {
    46  	err := command.MinimumCCAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionIsolationSegmentV3)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	err = cmd.SharedActor.CheckTarget(false, false)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	user, err := cmd.Config.CurrentUser()
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	cmd.UI.DisplayTextWithFlavor("Creating isolation segment {{.SegmentName}} as {{.CurrentUser}}...", map[string]interface{}{
    62  		"SegmentName": cmd.RequiredArgs.IsolationSegmentName,
    63  		"CurrentUser": user.Name,
    64  	})
    65  
    66  	warnings, err := cmd.Actor.CreateIsolationSegmentByName(v3action.IsolationSegment{
    67  		Name: cmd.RequiredArgs.IsolationSegmentName,
    68  	})
    69  	cmd.UI.DisplayWarnings(warnings)
    70  	if _, ok := err.(actionerror.IsolationSegmentAlreadyExistsError); ok {
    71  		cmd.UI.DisplayWarning("Isolation segment {{.IsolationSegmentName}} already exists.", map[string]interface{}{
    72  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    73  		})
    74  	} else if err != nil {
    75  		return err
    76  	}
    77  
    78  	cmd.UI.DisplayOK()
    79  
    80  	return nil
    81  }