@@ -66,34 +66,99 @@ async function setApiEndpoint() {
6666 core . endGroup ( )
6767}
6868
69- async function login ( ) {
70- const apiKey = core . getInput ( 'api_key' )
71- if ( apiKey . length > 0 ) {
72- const api = core . getInput ( 'api' )
73- const region = core . getInput ( 'region' )
74- const group = core . getInput ( 'group' )
69+ async function getDefaultResourceGroup ( ) {
70+ let output = ''
71+ await exec . exec ( 'ibmcloud' , [ 'resource' , 'groups' , '--default' , '--output' , 'json' ] , {
72+ listeners : { stdout : ( data ) => { output += data . toString ( ) } } ,
73+ silent : true // Intentionally suppress the output to avoid the ibmcloud target at the end
74+ } )
7575
76- core . startGroup ( 'Login to IBM Cloud' )
76+ try {
77+ const groups = JSON . parse ( output )
78+ if ( groups && groups . length > 0 && groups [ 0 ] . name ) {
79+ return groups [ 0 ] . name
80+ }
81+ } catch ( e ) {
82+ // Ignore JSON parsing errors as we will return null
83+ }
7784
78- // Mimic the ibmcloud login output since we will run it silent later
79- core . info ( `[command]/usr/local/bin/ibmcloud login -r ${ region } -g ${ group } ` )
80- core . info ( `API endpoint: ${ colorize ( api , 'cyan' ) } ` )
81- core . info ( 'Logging in with API key from environment variable...' )
82- core . info ( 'Authenticating...' )
85+ return null
86+ }
87+
88+ async function loginWithParams ( apiKey , params ) {
89+ const api = core . getInput ( 'api' )
90+
91+ // Mimic the ibmcloud login output since we will run it silent later
92+ core . info ( `[command]/usr/local/bin/ibmcloud login ${ params . join ( ' ' ) } ` )
93+ core . info ( `API endpoint: ${ colorize ( api , 'cyan' ) } ` )
94+ core . info ( 'Logging in with API key from environment variable...' )
95+ core . info ( 'Authenticating...' )
96+
97+ try {
98+ await exec . exec ( 'ibmcloud' , params , {
99+ env : {
100+ 'IBMCLOUD_API_KEY' : apiKey ,
101+ ...process . env
102+ } ,
103+ silent : true // Intentionally suppress the output to avoid the ibmcloud target at the end
104+ } )
105+
106+ // Mimic the ibmcloud login results on success
107+ core . info ( colorize ( 'OK' , 'green' ) )
108+ } catch ( e ) {
109+ // Mimic the ibmcloud login results on failure
110+ core . info ( colorize ( 'FAILED' , 'red' ) )
111+ core . info ( 'Unable to authenticate' )
112+ throw e
113+ }
114+ }
115+
116+ async function loginWithRegion ( apiKey , region ) {
117+ await loginWithParams ( apiKey , [ 'login' , '-r' , region ] )
118+
119+ core . info ( 'Detecting default resource group...' )
120+ const group = await getDefaultResourceGroup ( )
121+ if ( group ) {
122+ core . info ( `Detected default resource group ${ colorize ( group , 'cyan' ) } ` )
123+ core . info ( `Targeting resource group ${ colorize ( group , 'cyan' ) } ...` )
83124
84125 try {
85- await exec . exec ( 'ibmcloud' , [ 'login' , '-r' , region , '-g' , group ] , {
126+ await exec . exec ( 'ibmcloud' , [ 'target' , '-g' , group ] , {
86127 env : {
87128 'IBMCLOUD_API_KEY' : apiKey ,
88129 ...process . env
89130 } ,
90- silent : true // Intentionally suppress the output to avoid the ibmcloud target at the end of login
131+ silent : true // Intentionally suppress the output to avoid the ibmcloud target at the end
91132 } )
92- core . info ( colorize ( 'OK' , 'green' ) )
133+ // Mimic the ibmcloud target results on success
134+ core . info ( `Targeted resource group ${ colorize ( group , 'cyan' ) } ` )
93135 } catch ( e ) {
136+ // Mimic the ibmcloud target results on failure
94137 core . info ( colorize ( 'FAILED' , 'red' ) )
95- core . info ( 'Unable to authenticate' )
96138 throw e
139+ }
140+ } else {
141+ core . warning ( 'Could not determine default resource group.' )
142+ }
143+ }
144+
145+ async function loginWithRegionAndGroup ( apiKey , region , group ) {
146+ await loginWithParams ( apiKey , [ 'login' , '-r' , region , '-g' , group ] )
147+ }
148+
149+ async function login ( ) {
150+ const apiKey = core . getInput ( 'api_key' )
151+ if ( apiKey . length > 0 ) {
152+ const region = core . getInput ( 'region' )
153+ const group = core . getInput ( 'group' )
154+
155+ core . startGroup ( 'Login to IBM Cloud' )
156+ try {
157+ if ( group ) {
158+ await loginWithRegionAndGroup ( apiKey , region , group )
159+ } else {
160+ await loginWithRegion ( apiKey , region )
161+ }
97162 } finally {
98163 core . endGroup ( )
99164 }
0 commit comments