github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    10  	"code.cloudfoundry.org/cli/command"
    11  	"code.cloudfoundry.org/cli/command/flag"
    12  	"code.cloudfoundry.org/cli/command/translatableerror"
    13  	"code.cloudfoundry.org/cli/command/v3/shared"
    14  )
    15  
    16  //go:generate counterfeiter . DeleteIsolationSegmentActor
    17  
    18  type DeleteIsolationSegmentActor interface {
    19  	CloudControllerAPIVersion() string
    20  	DeleteIsolationSegmentByName(name string) (v3action.Warnings, error)
    21  }
    22  
    23  type DeleteIsolationSegmentCommand struct {
    24  	RequiredArgs    flag.IsolationSegmentName `positional-args:"yes"`
    25  	Force           bool                      `short:"f" description:"Force deletion without confirmation"`
    26  	usage           interface{}               `usage:"CF_NAME delete-isolation-segment SEGMENT_NAME"`
    27  	relatedCommands interface{}               `related_commands:"disable-org-isolation, isolation-segments"`
    28  
    29  	UI          command.UI
    30  	Config      command.Config
    31  	SharedActor command.SharedActor
    32  	Actor       DeleteIsolationSegmentActor
    33  }
    34  
    35  func (cmd *DeleteIsolationSegmentCommand) 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(nil, client, config)
    49  
    50  	return nil
    51  }
    52  
    53  func (cmd DeleteIsolationSegmentCommand) 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(cmd.Config, false, false)
    60  	if err != nil {
    61  		return shared.HandleError(err)
    62  	}
    63  
    64  	if !cmd.Force {
    65  		deleteSegment, promptErr := cmd.UI.DisplayBoolPrompt(false, "Really delete the isolation segment {{.IsolationSegmentName}}?", map[string]interface{}{
    66  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    67  		})
    68  
    69  		if promptErr != nil {
    70  			return promptErr
    71  		}
    72  
    73  		if !deleteSegment {
    74  			cmd.UI.DisplayText("Delete cancelled")
    75  			return nil
    76  		}
    77  	}
    78  
    79  	user, err := cmd.Config.CurrentUser()
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	cmd.UI.DisplayTextWithFlavor("Deleting isolation segment {{.SegmentName}} as {{.CurrentUser}}...", map[string]interface{}{
    85  		"SegmentName": cmd.RequiredArgs.IsolationSegmentName,
    86  		"CurrentUser": user.Name,
    87  	})
    88  
    89  	warnings, err := cmd.Actor.DeleteIsolationSegmentByName(cmd.RequiredArgs.IsolationSegmentName)
    90  	cmd.UI.DisplayWarnings(warnings)
    91  	if _, ok := err.(v3action.IsolationSegmentNotFoundError); ok {
    92  		cmd.UI.DisplayWarning("Isolation segment {{.IsolationSegmentName}} does not exist.", map[string]interface{}{
    93  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    94  		})
    95  	} else if err != nil {
    96  		return err
    97  	}
    98  
    99  	cmd.UI.DisplayOK()
   100  
   101  	return nil
   102  }