github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/actor/v7action/ssh.go (about) 1 package v7action 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 var allWarnings Warnings 22 23 endpoint := actor.CloudControllerClient.AppSSHEndpoint() 24 if endpoint == "" { 25 return SSHAuthentication{}, nil, actionerror.SSHEndpointNotSetError{} 26 } 27 28 fingerprint := actor.CloudControllerClient.AppSSHHostKeyFingerprint() 29 if fingerprint == "" { 30 return SSHAuthentication{}, nil, actionerror.SSHHostKeyFingerprintNotSetError{} 31 } 32 33 passcode, err := actor.UAAClient.GetSSHPasscode(actor.Config.AccessToken(), actor.Config.SSHOAuthClient()) 34 if err != nil { 35 return SSHAuthentication{}, Warnings{}, err 36 } 37 38 application, appWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 39 allWarnings = append(allWarnings, appWarnings...) 40 if err != nil { 41 return SSHAuthentication{}, allWarnings, err 42 } 43 44 if !application.Started() { 45 return SSHAuthentication{}, allWarnings, actionerror.ApplicationNotStartedError{Name: appName} 46 } 47 48 username, processWarnings, err := actor.getUsername(application, processType, processIndex) 49 allWarnings = append(allWarnings, processWarnings...) 50 if err != nil { 51 return SSHAuthentication{}, allWarnings, err 52 } 53 54 return SSHAuthentication{ 55 Endpoint: endpoint, 56 HostKeyFingerprint: fingerprint, 57 Passcode: passcode, 58 Username: username, 59 }, allWarnings, err 60 } 61 62 func (actor Actor) getUsername(application Application, processType string, processIndex uint) (string, Warnings, error) { 63 processSummaries, processWarnings, err := actor.getProcessSummariesForApp(application.GUID, false) 64 if err != nil { 65 return "", processWarnings, err 66 } 67 68 var processSummary ProcessSummary 69 for _, appProcessSummary := range processSummaries { 70 if appProcessSummary.Type == processType { 71 processSummary = appProcessSummary 72 break 73 } 74 } 75 76 if processSummary.GUID == "" { 77 return "", processWarnings, actionerror.ProcessNotFoundError{ProcessType: processType} 78 } 79 80 var processInstance ProcessInstance 81 for _, instance := range processSummary.InstanceDetails { 82 if uint(instance.Index) == processIndex { 83 processInstance = instance 84 break 85 } 86 } 87 88 if processInstance == (ProcessInstance{}) { 89 return "", processWarnings, actionerror.ProcessInstanceNotFoundError{ProcessType: processType, InstanceIndex: processIndex} 90 } 91 92 if !processInstance.Running() { 93 return "", processWarnings, actionerror.ProcessInstanceNotRunningError{ProcessType: processType, InstanceIndex: processIndex} 94 } 95 96 return fmt.Sprintf("cf:%s/%d", processSummary.GUID, processIndex), processWarnings, nil 97 }