@@ -66,34 +66,101 @@ 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+ env : {
73+ 'IBMCLOUD_API_KEY' : apiKey ,
74+ ...process . env
75+ } ,
76+ listeners : { stdout : ( data ) => { output += data . toString ( ) } } ,
77+ silent : true // Intentionally suppress the output to avoid the ibmcloud target at the end
78+ } )
7579
76- core . startGroup ( 'Login to IBM Cloud' )
80+ try {
81+ const groups = JSON . parse ( output )
82+ if ( groups && groups . length > 0 && groups [ 0 ] . name ) {
83+ return groups [ 0 ] . name
84+ }
85+ } catch ( e ) {
86+ // Ignore JSON parsing errors as we will return null
87+ }
7788
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...' )
89+ return null
90+ }
91+
92+ async function loginWithParams ( apiKey , params ) {
93+ const api = core . getInput ( 'api' )
94+
95+ // Mimic the ibmcloud login output since we will run it silent later
96+ core . info ( `[command]/usr/local/bin/ibmcloud login ${ params . join ( ' ' ) } ` )
97+ core . info ( `API endpoint: ${ colorize ( api , 'cyan' ) } ` )
98+ core . info ( 'Logging in with API key from environment variable...' )
99+ core . info ( 'Authenticating...' )
100+
101+ try {
102+ await exec . exec ( 'ibmcloud' , params , {
103+ env : {
104+ 'IBMCLOUD_API_KEY' : apiKey ,
105+ ...process . env
106+ } ,
107+ silent : true // Intentionally suppress the output to avoid the ibmcloud target at the end
108+ } )
109+
110+ // Mimic the ibmcloud login results on success
111+ core . info ( colorize ( 'OK' , 'green' ) )
112+ } catch ( e ) {
113+ // Mimic the ibmcloud login results on failure
114+ core . info ( colorize ( 'FAILED' , 'red' ) )
115+ core . info ( 'Unable to authenticate' )
116+ throw e
117+ }
118+ }
119+
120+ async function loginWithRegion ( apiKey , region ) {
121+ await loginWithParams ( apiKey , [ '-r' , region ] )
122+
123+ group = await getDefaultResourceGroup ( )
124+ if ( group ) {
125+ core . info ( `Targeting resource group ${ colorize ( group , 'cyan' ) } ` )
83126
84127 try {
85- await exec . exec ( 'ibmcloud' , [ 'login' , '-r' , region , '-g' , group ] , {
128+ await exec . exec ( 'ibmcloud' , [ 'target' , '-g' , group ] , {
86129 env : {
87130 'IBMCLOUD_API_KEY' : apiKey ,
88131 ...process . env
89132 } ,
90- silent : true // Intentionally suppress the output to avoid the ibmcloud target at the end of login
133+ silent : true // Intentionally suppress the output to avoid the ibmcloud target at the end
91134 } )
92- core . info ( colorize ( 'OK' , 'green' ) )
135+ // Mimic the ibmcloud target results on success
136+ core . info ( `Targeted resource group ${ colorize ( group , 'cyan' ) } ` )
93137 } catch ( e ) {
138+ // Mimic the ibmcloud target results on failure
94139 core . info ( colorize ( 'FAILED' , 'red' ) )
95- core . info ( 'Unable to authenticate' )
96140 throw e
141+ }
142+ } else {
143+ core . warning ( 'Could not determine default resource group.' )
144+ }
145+ }
146+
147+ async function loginWithRegionAndGroup ( apiKey , region , group ) {
148+ await loginWithParams ( apiKey , [ '-r' , region , '-g' , group ] )
149+ }
150+
151+ async function login ( ) {
152+ const apiKey = core . getInput ( 'api_key' )
153+ if ( apiKey . length > 0 ) {
154+ const region = core . getInput ( 'region' )
155+ const group = core . getInput ( 'group' )
156+
157+ core . startGroup ( 'Login to IBM Cloud' )
158+ try {
159+ if ( group ) {
160+ await loginWithRegionAndGroup ( apiKey , region , group )
161+ } else {
162+ await loginWithRegion ( apiKey , region )
163+ }
97164 } finally {
98165 core . endGroup ( )
99166 }
0 commit comments