github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/cf/commands/space/allow_space_ssh.go (about)

     1  package space
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/spaces"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type AllowSpaceSSH struct {
    17  	ui        terminal.UI
    18  	config    coreconfig.Reader
    19  	spaceReq  requirements.SpaceRequirement
    20  	spaceRepo spaces.SpaceRepository
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&AllowSpaceSSH{})
    25  }
    26  
    27  func (cmd *AllowSpaceSSH) MetaData() commandregistry.CommandMetadata {
    28  	return commandregistry.CommandMetadata{
    29  		Name:        "allow-space-ssh",
    30  		Description: T("Allow SSH access for the space"),
    31  		Usage: []string{
    32  			T("CF_NAME allow-space-ssh SPACE_NAME"),
    33  		},
    34  	}
    35  }
    36  
    37  func (cmd *AllowSpaceSSH) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    38  	if len(fc.Args()) != 1 {
    39  		cmd.ui.Failed(T("Incorrect Usage. Requires SPACE_NAME as argument\n\n") + commandregistry.Commands.CommandUsage("allow-space-ssh"))
    40  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    41  	}
    42  
    43  	cmd.spaceReq = requirementsFactory.NewSpaceRequirement(fc.Args()[0])
    44  
    45  	reqs := []requirements.Requirement{
    46  		requirementsFactory.NewLoginRequirement(),
    47  		requirementsFactory.NewTargetedOrgRequirement(),
    48  		cmd.spaceReq,
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *AllowSpaceSSH) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *AllowSpaceSSH) Execute(fc flags.FlagContext) error {
    62  	space := cmd.spaceReq.GetSpace()
    63  
    64  	if space.AllowSSH {
    65  		cmd.ui.Say(T("ssh support is already enabled in space '{{.SpaceName}}'",
    66  			map[string]interface{}{
    67  				"SpaceName": space.Name,
    68  			},
    69  		))
    70  		return nil
    71  	}
    72  
    73  	cmd.ui.Say(T("Enabling ssh support for space '{{.SpaceName}}'...",
    74  		map[string]interface{}{
    75  			"SpaceName": space.Name,
    76  		},
    77  	))
    78  	cmd.ui.Say("")
    79  
    80  	err := cmd.spaceRepo.SetAllowSSH(space.GUID, true)
    81  	if err != nil {
    82  		return errors.New(T("Error enabling ssh support for space ") + space.Name + ": " + err.Error())
    83  	}
    84  
    85  	cmd.ui.Ok()
    86  	return nil
    87  }