github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/actor/v3action/ssh.go (about)

     1  package v3action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  )
     8  
     9  type SSHAuthentication struct {
    10  	Endpoint           string
    11  	HostKeyFingerprint string
    12  	Passcode           string
    13  	Username           string
    14  }
    15  
    16  // GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex returns
    17  // back the SSH authentication information for the SSH session.
    18  func (actor Actor) GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex(
    19  	appName string, spaceGUID string, processType string, processIndex uint,
    20  ) (SSHAuthentication, Warnings, error) {
    21  	endpoint := actor.CloudControllerClient.AppSSHEndpoint()
    22  	if endpoint == "" {
    23  		return SSHAuthentication{}, nil, actionerror.SSHEndpointNotSetError{}
    24  	}
    25  
    26  	fingerprint := actor.CloudControllerClient.AppSSHHostKeyFingerprint()
    27  	if fingerprint == "" {
    28  		return SSHAuthentication{}, nil, actionerror.SSHHostKeyFingerprintNotSetError{}
    29  	}
    30  
    31  	passcode, err := actor.UAAClient.GetSSHPasscode(actor.Config.AccessToken(), actor.Config.SSHOAuthClient())
    32  	if err != nil {
    33  		return SSHAuthentication{}, Warnings{}, err
    34  	}
    35  
    36  	appSummary, warnings, err := actor.GetApplicationSummaryByNameAndSpace(appName, spaceGUID)
    37  	if err != nil {
    38  		return SSHAuthentication{}, warnings, err
    39  	}
    40  
    41  	var processSummary ProcessSummary
    42  	for _, appProcessSummary := range appSummary.ProcessSummaries {
    43  		if appProcessSummary.Type == processType {
    44  			processSummary = appProcessSummary
    45  			break
    46  		}
    47  	}
    48  	if processSummary.GUID == "" {
    49  		return SSHAuthentication{}, warnings, actionerror.ProcessNotFoundError{ProcessType: processType}
    50  	}
    51  
    52  	if !appSummary.Application.Started() {
    53  		return SSHAuthentication{}, warnings, actionerror.ApplicationNotStartedError{Name: appName}
    54  	}
    55  
    56  	var processInstance ProcessInstance
    57  	for _, instance := range processSummary.InstanceDetails {
    58  		if uint(instance.Index) == processIndex {
    59  			processInstance = instance
    60  			break
    61  		}
    62  	}
    63  
    64  	if processInstance == (ProcessInstance{}) {
    65  		return SSHAuthentication{}, warnings, actionerror.ProcessInstanceNotFoundError{ProcessType: processType, InstanceIndex: processIndex}
    66  	}
    67  
    68  	if !processInstance.Running() {
    69  		return SSHAuthentication{}, warnings, actionerror.ProcessInstanceNotRunningError{ProcessType: processType,
    70  			InstanceIndex: processIndex}
    71  	}
    72  
    73  	return SSHAuthentication{
    74  		Endpoint:           endpoint,
    75  		HostKeyFingerprint: fingerprint,
    76  		Passcode:           passcode,
    77  		Username:           fmt.Sprintf("cf:%s/%d", processSummary.GUID, processIndex),
    78  	}, warnings, err
    79  }