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