github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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/command"
     8  	"code.cloudfoundry.org/cli/command/flag"
     9  	"code.cloudfoundry.org/cli/command/v6/shared"
    10  )
    11  
    12  //go:generate counterfeiter . CreateIsolationSegmentActor
    13  
    14  type CreateIsolationSegmentActor interface {
    15  	CreateIsolationSegmentByName(isolationSegment v3action.IsolationSegment) (v3action.Warnings, error)
    16  }
    17  
    18  type CreateIsolationSegmentCommand struct {
    19  	RequiredArgs    flag.IsolationSegmentName `positional-args:"yes"`
    20  	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."`
    21  	relatedCommands interface{}               `related_commands:"enable-org-isolation, isolation-segments"`
    22  
    23  	UI          command.UI
    24  	Config      command.Config
    25  	SharedActor command.SharedActor
    26  	Actor       CreateIsolationSegmentActor
    27  }
    28  
    29  func (cmd *CreateIsolationSegmentCommand) Setup(config command.Config, ui command.UI) error {
    30  	cmd.UI = ui
    31  	cmd.Config = config
    32  	cmd.SharedActor = sharedaction.NewActor(config)
    33  
    34  	client, _, err := shared.NewV3BasedClients(config, ui, true)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	cmd.Actor = v3action.NewActor(client, config, nil, nil)
    39  
    40  	return nil
    41  }
    42  
    43  func (cmd CreateIsolationSegmentCommand) Execute(args []string) error {
    44  	err := cmd.SharedActor.CheckTarget(false, false)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	user, err := cmd.Config.CurrentUser()
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	cmd.UI.DisplayTextWithFlavor("Creating isolation segment {{.SegmentName}} as {{.CurrentUser}}...", map[string]interface{}{
    55  		"SegmentName": cmd.RequiredArgs.IsolationSegmentName,
    56  		"CurrentUser": user.Name,
    57  	})
    58  
    59  	warnings, err := cmd.Actor.CreateIsolationSegmentByName(v3action.IsolationSegment{
    60  		Name: cmd.RequiredArgs.IsolationSegmentName,
    61  	})
    62  	cmd.UI.DisplayWarnings(warnings)
    63  	if _, ok := err.(actionerror.IsolationSegmentAlreadyExistsError); ok {
    64  		cmd.UI.DisplayWarning("Isolation segment {{.IsolationSegmentName}} already exists.", map[string]interface{}{
    65  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    66  		})
    67  	} else if err != nil {
    68  		return err
    69  	}
    70  
    71  	cmd.UI.DisplayOK()
    72  
    73  	return nil
    74  }