Skip to content

Commit 6cf5457

Browse files
committed
Detect the default group
Signed-off-by: Jason Frey <fryguy9@gmail.com>
1 parent 4fe2fb5 commit 6cf5457

6 files changed

Lines changed: 183 additions & 35 deletions

File tree

.github/workflows/test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ jobs:
162162
ibmcloud target | grep -q '^Region:\s\+us-south$' &&
163163
ibmcloud target | grep -q '^Resource group:\s\+default$'
164164
165+
test-login-with-region:
166+
runs-on: ubuntu-latest
167+
steps:
168+
- uses: actions/checkout@v5
169+
- name: Set up ibmcloud CLI
170+
uses: ./
171+
with:
172+
api_key: ${{ secrets.IBMCLOUD_API_KEY }}
173+
region: us-east
174+
- name: Check logged in
175+
run: |
176+
ibmcloud target | grep -q '^User:\s\+${{ secrets.IBMCLOUD_USER }}$' &&
177+
ibmcloud target | grep -q '^Region:\s\+us-east$' &&
178+
ibmcloud target | grep -q '^Resource group:\s\+default$'
179+
165180
test-login-with-region-and-group:
166181
runs-on: ubuntu-latest
167182
steps:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ steps:
3030

3131
- `region`: (optional - default: `us-south`) Region to access on IBM Cloud.
3232

33-
- `group`: (optional - default: `default`) Resource group to access on IBM Cloud.
33+
- `group`: (optional - default: detection of default group) Resource group to access on IBM Cloud.
3434

3535
- `api`: (optional - default: `https://cloud.ibm.com`) API endpoint to IBM Cloud.
3636

action.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ inputs:
1212
default: us-south
1313
group:
1414
description: Resource group to access on IBM Cloud.
15-
default: default
1615
api:
1716
description: API endpoint to IBM Cloud.
1817
default: https://cloud.ibm.com

dist/index.js

Lines changed: 83 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.js

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)