github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/create_isolation_segment_command.go (about)

     1  package v3
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     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/flag"
    13  	"code.cloudfoundry.org/cli/command/translatableerror"
    14  	"code.cloudfoundry.org/cli/command/v3/shared"
    15  )
    16  
    17  //go:generate counterfeiter . CreateIsolationSegmentActor
    18  
    19  type CreateIsolationSegmentActor interface {
    20  	CloudControllerAPIVersion() string
    21  	CreateIsolationSegmentByName(isolationSegment v3action.IsolationSegment) (v3action.Warnings, error)
    22  }
    23  
    24  type CreateIsolationSegmentCommand struct {
    25  	RequiredArgs    flag.IsolationSegmentName `positional-args:"yes"`
    26  	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."`
    27  	relatedCommands interface{}               `related_commands:"enable-org-isolation, isolation-segments"`
    28  
    29  	UI          command.UI
    30  	Config      command.Config
    31  	SharedActor command.SharedActor
    32  	Actor       CreateIsolationSegmentActor
    33  }
    34  
    35  func (cmd *CreateIsolationSegmentCommand) Setup(config command.Config, ui command.UI) error {
    36  	cmd.UI = ui
    37  	cmd.Config = config
    38  	cmd.SharedActor = sharedaction.NewActor(config)
    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(client, config, nil, nil)
    49  
    50  	return nil
    51  }
    52  
    53  func (cmd CreateIsolationSegmentCommand) 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(false, false)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	user, err := cmd.Config.CurrentUser()
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	cmd.UI.DisplayTextWithFlavor("Creating isolation segment {{.SegmentName}} as {{.CurrentUser}}...", map[string]interface{}{
    70  		"SegmentName": cmd.RequiredArgs.IsolationSegmentName,
    71  		"CurrentUser": user.Name,
    72  	})
    73  
    74  	warnings, err := cmd.Actor.CreateIsolationSegmentByName(v3action.IsolationSegment{
    75  		Name: cmd.RequiredArgs.IsolationSegmentName,
    76  	})
    77  	cmd.UI.DisplayWarnings(warnings)
    78  	if _, ok := err.(actionerror.IsolationSegmentAlreadyExistsError); ok {
    79  		cmd.UI.DisplayWarning("Isolation segment {{.IsolationSegmentName}} already exists.", map[string]interface{}{
    80  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    81  		})
    82  	} else if err != nil {
    83  		return err
    84  	}
    85  
    86  	cmd.UI.DisplayOK()
    87  
    88  	return nil
    89  }