github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/command/v3/delete_isolation_segment_command.go (about)

     1  package v3
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/sharedaction"
     5  	"code.cloudfoundry.org/cli/actor/v3action"
     6  	"code.cloudfoundry.org/cli/command"
     7  	"code.cloudfoundry.org/cli/command/flag"
     8  	"code.cloudfoundry.org/cli/command/v3/shared"
     9  )
    10  
    11  //go:generate counterfeiter . DeleteIsolationSegmentActor
    12  
    13  type DeleteIsolationSegmentActor interface {
    14  	CloudControllerAPIVersion() string
    15  	DeleteIsolationSegmentByName(name string) (v3action.Warnings, error)
    16  }
    17  
    18  type DeleteIsolationSegmentCommand struct {
    19  	RequiredArgs    flag.IsolationSegmentName `positional-args:"yes"`
    20  	Force           bool                      `short:"f" description:"Force deletion without confirmation"`
    21  	usage           interface{}               `usage:"CF_NAME delete-isolation-segment SEGMENT_NAME"`
    22  	relatedCommands interface{}               `related_commands:"disable-org-isolation, isolation-segments"`
    23  
    24  	UI          command.UI
    25  	Config      command.Config
    26  	SharedActor command.SharedActor
    27  	Actor       DeleteIsolationSegmentActor
    28  }
    29  
    30  func (cmd *DeleteIsolationSegmentCommand) Setup(config command.Config, ui command.UI) error {
    31  	cmd.UI = ui
    32  	cmd.Config = config
    33  	cmd.SharedActor = sharedaction.NewActor()
    34  
    35  	client, _, err := shared.NewClients(config, ui, true)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	cmd.Actor = v3action.NewActor(client, config)
    40  
    41  	return nil
    42  }
    43  
    44  func (cmd DeleteIsolationSegmentCommand) Execute(args []string) error {
    45  	err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), command.MinVersionIsolationSegmentV3)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	err = cmd.SharedActor.CheckTarget(cmd.Config, false, false)
    51  	if err != nil {
    52  		return shared.HandleError(err)
    53  	}
    54  
    55  	if !cmd.Force {
    56  		deleteSegment, promptErr := cmd.UI.DisplayBoolPrompt(false, "Really delete the isolation segment {{.IsolationSegmentName}}?", map[string]interface{}{
    57  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    58  		})
    59  
    60  		if promptErr != nil {
    61  			return promptErr
    62  		}
    63  
    64  		if !deleteSegment {
    65  			cmd.UI.DisplayText("Delete cancelled")
    66  			return nil
    67  		}
    68  	}
    69  
    70  	user, err := cmd.Config.CurrentUser()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	cmd.UI.DisplayTextWithFlavor("Deleting isolation segment {{.SegmentName}} as {{.CurrentUser}}...", map[string]interface{}{
    76  		"SegmentName": cmd.RequiredArgs.IsolationSegmentName,
    77  		"CurrentUser": user.Name,
    78  	})
    79  
    80  	warnings, err := cmd.Actor.DeleteIsolationSegmentByName(cmd.RequiredArgs.IsolationSegmentName)
    81  	cmd.UI.DisplayWarnings(warnings)
    82  	if _, ok := err.(v3action.IsolationSegmentNotFoundError); ok {
    83  		cmd.UI.DisplayWarning("Isolation segment {{.IsolationSegmentName}} does not exist.", map[string]interface{}{
    84  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    85  		})
    86  	} else if err != nil {
    87  		return err
    88  	}
    89  
    90  	cmd.UI.DisplayOK()
    91  
    92  	return nil
    93  }