Skip to content

Commit d600753

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

6 files changed

Lines changed: 196 additions & 36 deletions

File tree

.github/workflows/test.yml

Lines changed: 32 additions & 1 deletion
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:
@@ -178,6 +193,22 @@ jobs:
178193
ibmcloud target | grep -q '^Region:\s\+us-east$' &&
179194
ibmcloud target | grep -q '^Resource group:\s\+manageiq$'
180195
196+
test-login-with-region-skipping-group:
197+
runs-on: ubuntu-latest
198+
steps:
199+
- uses: actions/checkout@v5
200+
- name: Set up ibmcloud CLI
201+
uses: ./
202+
with:
203+
api_key: ${{ secrets.IBMCLOUD_API_KEY }}
204+
region: us-east
205+
group: ''
206+
- name: Check logged in
207+
run: |
208+
ibmcloud target | grep -q '^User:\s\+${{ secrets.IBMCLOUD_USER }}$' &&
209+
ibmcloud target | grep -q '^Region:\s\+us-east$' &&
210+
ibmcloud target | grep -q '^Resource group:\s\+manageiq$'
211+
181212
test-login-failure:
182213
runs-on: ubuntu-latest
183214
steps:
@@ -225,7 +256,7 @@ jobs:
225256
- name: Check basic CLI function
226257
run: ibmcloud --version
227258

228-
test-all-platforms:
259+
test-os:
229260
strategy:
230261
matrix:
231262
os:

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

Comments
 (0)