github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/enable_org_isolation_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 . EnableOrgIsolationActor
    17  
    18  type EnableOrgIsolationActor interface {
    19  	CloudControllerAPIVersion() string
    20  	EntitleIsolationSegmentToOrganizationByName(isolationSegmentName string, orgName string) (v3action.Warnings, error)
    21  }
    22  
    23  type EnableOrgIsolationCommand struct {
    24  	RequiredArgs    flag.OrgIsolationArgs `positional-args:"yes"`
    25  	usage           interface{}           `usage:"CF_NAME enable-org-isolation ORG_NAME SEGMENT_NAME"`
    26  	relatedCommands interface{}           `related_commands:"create-isolation-segment, isolation-segments, set-org-default-isolation-segment, set-space-isolation-segment"`
    27  
    28  	UI          command.UI
    29  	Config      command.Config
    30  	SharedActor command.SharedActor
    31  	Actor       EnableOrgIsolationActor
    32  }
    33  
    34  func (cmd *EnableOrgIsolationCommand) Setup(config command.Config, ui command.UI) error {
    35  	cmd.UI = ui
    36  	cmd.Config = config
    37  	cmd.SharedActor = sharedaction.NewActor(config)
    38  
    39  	client, _, err := shared.NewClients(config, ui, true)
    40  	if err != nil {
    41  		if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound {
    42  			return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionIsolationSegmentV3}
    43  		}
    44  
    45  		return err
    46  	}
    47  	cmd.Actor = v3action.NewActor(client, config, nil, nil)
    48  
    49  	return nil
    50  }
    51  
    52  func (cmd EnableOrgIsolationCommand) Execute(args []string) error {
    53  	err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionIsolationSegmentV3)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	err = cmd.SharedActor.CheckTarget(false, false)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	user, err := cmd.Config.CurrentUser()
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	cmd.UI.DisplayTextWithFlavor("Enabling isolation segment {{.SegmentName}} for org {{.OrgName}} as {{.CurrentUser}}...", map[string]interface{}{
    69  		"SegmentName": cmd.RequiredArgs.IsolationSegmentName,
    70  		"OrgName":     cmd.RequiredArgs.OrganizationName,
    71  		"CurrentUser": user.Name,
    72  	})
    73  
    74  	warnings, err := cmd.Actor.EntitleIsolationSegmentToOrganizationByName(cmd.RequiredArgs.IsolationSegmentName, cmd.RequiredArgs.OrganizationName)
    75  	cmd.UI.DisplayWarnings(warnings)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	cmd.UI.DisplayOK()
    81  
    82  	return nil
    83  }