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