1212const request = require ( 'request' ) ;
1313const rest = require ( './rest.js' ) ;
1414const fs = require ( 'fs' ) ;
15+ const { findSourceMap } = require ( 'module' ) ;
1516
1617
1718var domainName , username , password , timeout ;
@@ -22,6 +23,7 @@ var authtoken="";
2223var uid = "" ;
2324var csrf = "" ;
2425var projectId ;
26+ var projectName ;
2527var nextUrl , formUrl ;
2628var finalCall ;
2729var loginStageCounter = 0 ;
@@ -84,6 +86,50 @@ function getProjectAccountConfig(inProjectId)
8486 loginPhase1 ( ) ;
8587}
8688
89+ function projectDeployments ( inProjectId )
90+ {
91+ debug ( "Listing project deployments for projectId [" + inProjectId + "]" ) ;
92+ projectId = inProjectId ;
93+ finalCall = getProjectDeployments ;
94+ loginPhase1 ( ) ;
95+ }
96+
97+ function searchProject ( inProjectName )
98+ {
99+ projectName = inProjectName ;
100+ finalCall = searchProjectsByName ;
101+ loginPhase1 ( ) ;
102+ }
103+
104+
105+ function searchProjectsByName ( )
106+ {
107+ debug ( "Search Projects By Name [" + projectName + "]" ) ;
108+ var endPoint = "https://" + domainName + "/enterprise/v1/projects?limit=25&skip=0&q=" + projectName ;
109+ debug ( "Next URL [" + endPoint + "]" ) ;
110+
111+ var headers = [
112+ { name :"authtoken" , value :authtoken } ,
113+ { name :"accept" , value :"application/json" } ,
114+ { name :"x-csrf-token" , value :csrf } ,
115+ ] ;
116+ rest . custom ( endPoint , undefined , undefined , 60 , undefined , undefined , "GET" , processResponse , undefined , headers , true , false ) ;
117+ }
118+
119+ function getProjectDeployments ( )
120+ {
121+ debug ( "Executing Project Deployments call" ) ;
122+ var endPoint = "https://" + domainName + "/enterprise/v1/deployments" ;
123+ debug ( "Next URL [" + endPoint + "]" ) ;
124+
125+ var headers = [
126+ { name :"authtoken" , value :authtoken } ,
127+ { name :"accept" , value :"application/json" } ,
128+ { name :"project_uid" , value :projectId } ,
129+ { name :"x-csrf-token" , value :csrf } ,
130+ ] ;
131+ rest . custom ( endPoint , undefined , undefined , 60 , undefined , undefined , "GET" , processResponse , undefined , headers , true , false ) ;
132+ }
87133
88134function stageInfo ( )
89135{
@@ -101,7 +147,7 @@ function stageInfo()
101147
102148function getProjectAccountConfigInfo ( )
103149{
104- debug ( "PRojet Account Config Info" ) ;
150+ debug ( "Project Account Config Info" ) ;
105151 var endPoint = "https://" + domainName + "/enterprise/v1/configdata" ;
106152 debug ( "Next URL [" + endPoint + "]" ) ;
107153
@@ -245,7 +291,7 @@ function checkResponse(res,body)
245291 }
246292
247293 if ( res . statusCode == 200 ) {
248- // debug(body);
294+ debug ( body ) ;
249295 }
250296
251297 if ( res . statusCode == 400 || res . statusCode == 404 || res . statusCode == 500 || res . statusCode == 502 || res . statusCode == 403 || res . statusCode == 401 )
@@ -254,9 +300,40 @@ function checkResponse(res,body)
254300 console . log ( res ) ;
255301 process . exit ( 99 ) ;
256302 }
303+ }
304+
305+ function checkPhase3Response ( res , body )
306+ {
307+ debug ( "Response Status Code =" + res . statusCode ) ;
308+ if ( res . statusCode == 302 ) {
309+ nextUrl = res . headers . location ;
310+ debug ( "Redirect URL [" + nextUrl + "]" ) ;
311+ }
312+
313+ if ( res . statusCode == 200 ) {
314+ //debug(body);
315+ //find <span class="kc-feedback-text"> .... </span>
316+ //extract the error and return error message
317+ err = body . split ( '<span class="kc-feedback-text">' ) [ 1 ] ;
318+ err = err . split ( '</span>' ) [ 0 ] ;
319+
320+ error = { }
321+ error . message = err ;
322+ debug ( "Failed to login via Software AG Cloud [" + err + "] - exiting" )
323+ console . log ( JSON . stringify ( error ) ) ;
324+
325+ process . exit ( 401 ) ;
326+ }
257327
328+ if ( res . statusCode == 400 || res . statusCode == 404 || res . statusCode == 500 || res . statusCode == 502 || res . statusCode == 403 || res . statusCode == 401 )
329+ {
330+ debug ( "Failed to login via Software AG Cloud - exiting" )
331+ console . log ( res ) ;
332+ process . exit ( 99 ) ;
333+ }
258334}
259335
336+
260337function processProjectsResponse ( url , err , body , res ) {
261338 //console.log(body);
262339
@@ -370,7 +447,7 @@ function loginResponse(url,err,body,res){
370447 break ;
371448
372449 case 3 :
373- checkResponse ( res , body ) ;
450+ checkPhase3Response ( res , body ) ;
374451 nextCall = loginRedirectPhase ( 4 ) ;
375452 break ;
376453
@@ -398,10 +475,13 @@ function loginResponse(url,err,body,res){
398475
399476
400477 //Invoke next call in chain
401- if ( nextCall !== undefined ) nextCall ( ) ;
402-
478+ if ( nextCall !== undefined )
479+ {
480+ debug ( "Found target call... initiating" ) ;
481+ nextCall ( ) ;
482+ }
403483
404484}
405485
406486
407- module . exports = { init, user, stages, projectWorkflows, projectFlowservices, connectorAccounts, getProjectAccountConfig} ;
487+ module . exports = { init, user, stages, projectWorkflows, projectFlowservices, connectorAccounts, getProjectAccountConfig, searchProject , projectDeployments } ;
0 commit comments