github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/reset_org_default_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 . ResetOrgDefaultIsolationSegmentActor
    19  
    20  type ResetOrgDefaultIsolationSegmentActor interface {
    21  	CloudControllerAPIVersion() string
    22  	ResetOrganizationDefaultIsolationSegment(orgGUID string) (v3action.Warnings, error)
    23  }
    24  
    25  //go:generate counterfeiter . ResetOrgDefaultIsolationSegmentActorV2
    26  
    27  type ResetOrgDefaultIsolationSegmentActorV2 interface {
    28  	GetOrganizationByName(orgName string) (v2action.Organization, v2action.Warnings, error)
    29  }
    30  
    31  type ResetOrgDefaultIsolationSegmentCommand struct {
    32  	RequiredArgs    flag.ResetOrgDefaultIsolationArgs `positional-args:"yes"`
    33  	usage           interface{}                       `usage:"CF_NAME reset-org-default-isolation-segment ORG_NAME"`
    34  	relatedCommands interface{}                       `related_commands:"org, restart"`
    35  
    36  	UI          command.UI
    37  	Config      command.Config
    38  	SharedActor command.SharedActor
    39  	Actor       ResetOrgDefaultIsolationSegmentActor
    40  	ActorV2     ResetOrgDefaultIsolationSegmentActorV2
    41  }
    42  
    43  func (cmd *ResetOrgDefaultIsolationSegmentCommand) Setup(config command.Config, ui command.UI) error {
    44  	cmd.UI = ui
    45  	cmd.Config = config
    46  	cmd.SharedActor = sharedaction.NewActor(config)
    47  
    48  	client, _, 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(nil, client, config)
    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 ResetOrgDefaultIsolationSegmentCommand) 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(cmd.Config, true, false)
    74  	if err != nil {
    75  		return shared.HandleError(err)
    76  	}
    77  
    78  	user, err := cmd.Config.CurrentUser()
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	cmd.UI.DisplayTextWithFlavor("Resetting default isolation segment of org {{.OrgName}} as {{.CurrentUser}}...", map[string]interface{}{
    84  		"OrgName":     cmd.RequiredArgs.OrgName,
    85  		"CurrentUser": user.Name,
    86  	})
    87  
    88  	organization, v2Warnings, err := cmd.ActorV2.GetOrganizationByName(cmd.RequiredArgs.OrgName)
    89  	cmd.UI.DisplayWarnings(v2Warnings)
    90  	if err != nil {
    91  		return sharedV2.HandleError(err)
    92  	}
    93  
    94  	warnings, err := cmd.Actor.ResetOrganizationDefaultIsolationSegment(organization.GUID)
    95  	cmd.UI.DisplayWarnings(warnings)
    96  	if err != nil {
    97  		return shared.HandleError(err)
    98  	}
    99  
   100  	cmd.UI.DisplayOK()
   101  	cmd.UI.DisplayNewline()
   102  
   103  	cmd.UI.DisplayText("Applications in spaces of this org that have no isolation segment assigned will be placed in the platform default isolation segment.")
   104  	cmd.UI.DisplayText("Running applications need a restart to be moved there.")
   105  
   106  	return nil
   107  }