github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/reset_space_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/v2action"
     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  	sharedV2 "code.cloudfoundry.org/cli/command/v2/shared"
    15  	"code.cloudfoundry.org/cli/command/v3/shared"
    16  )
    17  
    18  //go:generate counterfeiter . ResetSpaceIsolationSegmentActor
    19  
    20  type ResetSpaceIsolationSegmentActor interface {
    21  	CloudControllerAPIVersion() string
    22  	ResetSpaceIsolationSegment(orgGUID string, spaceGUID string) (string, v3action.Warnings, error)
    23  }
    24  
    25  //go:generate counterfeiter . ResetSpaceIsolationSegmentActorV2
    26  
    27  type ResetSpaceIsolationSegmentActorV2 interface {
    28  	GetSpaceByOrganizationAndName(orgGUID string, spaceName string) (v2action.Space, v2action.Warnings, error)
    29  }
    30  
    31  type ResetSpaceIsolationSegmentCommand struct {
    32  	RequiredArgs    flag.ResetSpaceIsolationArgs `positional-args:"yes"`
    33  	usage           interface{}                  `usage:"CF_NAME reset-space-isolation-segment SPACE_NAME"`
    34  	relatedCommands interface{}                  `related_commands:"org, restart, space"`
    35  
    36  	UI          command.UI
    37  	Config      command.Config
    38  	SharedActor command.SharedActor
    39  	Actor       ResetSpaceIsolationSegmentActor
    40  	ActorV2     ResetSpaceIsolationSegmentActorV2
    41  }
    42  
    43  func (cmd *ResetSpaceIsolationSegmentCommand) Setup(config command.Config, ui command.UI) error {
    44  	cmd.UI = ui
    45  	cmd.Config = config
    46  	cmd.SharedActor = sharedaction.NewActor(config)
    47  
    48  	ccClient, _, err := shared.NewClients(config, ui, true)
    49  	if err != nil {
    50  		if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound {
    51  			return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionIsolationSegmentV3}
    52  		}
    53  
    54  		return err
    55  	}
    56  	cmd.Actor = v3action.NewActor(ccClient, config, nil, nil)
    57  
    58  	ccClientV2, uaaClientV2, err := sharedV2.NewClients(config, ui, true)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	cmd.ActorV2 = v2action.NewActor(ccClientV2, uaaClientV2, config)
    63  
    64  	return nil
    65  }
    66  
    67  func (cmd ResetSpaceIsolationSegmentCommand) Execute(args []string) error {
    68  	err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionIsolationSegmentV3)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	err = cmd.SharedActor.CheckTarget(true, false)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	user, err := cmd.Config.CurrentUser()
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	cmd.UI.DisplayTextWithFlavor("Resetting isolation segment assignment of space {{.SpaceName}} in org {{.OrgName}} as {{.CurrentUser}}...", map[string]interface{}{
    84  		"SpaceName":   cmd.RequiredArgs.SpaceName,
    85  		"OrgName":     cmd.Config.TargetedOrganization().Name,
    86  		"CurrentUser": user.Name,
    87  	})
    88  
    89  	space, v2Warnings, err := cmd.ActorV2.GetSpaceByOrganizationAndName(cmd.Config.TargetedOrganization().GUID, cmd.RequiredArgs.SpaceName)
    90  	cmd.UI.DisplayWarnings(v2Warnings)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	newIsolationSegmentName, warnings, err := cmd.Actor.ResetSpaceIsolationSegment(cmd.Config.TargetedOrganization().GUID, space.GUID)
    96  	cmd.UI.DisplayWarnings(warnings)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	cmd.UI.DisplayOK()
   102  	cmd.UI.DisplayNewline()
   103  
   104  	if newIsolationSegmentName == "" {
   105  		cmd.UI.DisplayText("Applications in this space will be placed in the platform default isolation segment.")
   106  	} else {
   107  		cmd.UI.DisplayText("Applications in this space will be placed in isolation segment {{.orgIsolationSegment}}.", map[string]interface{}{
   108  			"orgIsolationSegment": newIsolationSegmentName,
   109  		})
   110  	}
   111  	cmd.UI.DisplayText("Running applications need a restart to be moved there.")
   112  
   113  	return nil
   114  }