Skip to content

Commit 807efe4

Browse files
authored
feat: include developer orgs with Runtime feature in console org commands (#257)
* feat: include developer orgs with Runtime feature in console org commands `aio console org list` and `aio console org select` previously filtered out every organization that was not `entp` (enterprise). App Builder trial orgs are provisioned with `type: developer`, so trial users could not see or select their own org from the CLI and were forced to download a workspace.json from the Console UI as a workaround.
1 parent a252c80 commit 807efe4

10 files changed

Lines changed: 181 additions & 14 deletions

File tree

‎src/commands/console/index.js‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
1818
const { CLI } = require('@adobe/aio-lib-ims/src/context')
1919
const { getCliEnv } = require('@adobe/aio-lib-env')
2020
const yaml = require('js-yaml')
21-
const { CONFIG_KEYS, API_KEYS } = require('../../config')
21+
const { CONFIG_KEYS, API_KEYS, CONSOLE_API_URLS, ORG_FEATURE_RUNTIME, ORG_TYPE_DEVELOPER, ORG_TYPE_ENTERPRISE } = require('../../config')
2222

2323
class ConsoleCommand extends Command {
2424
async run () {
@@ -36,6 +36,63 @@ class ConsoleCommand extends Command {
3636
this.consoleCLI = await LibConsoleCLI.init({ accessToken: this.accessToken, apiKey: this.apiKey, env: this.cliEnv })
3737
}
3838

39+
/**
40+
* Retrieve enabled feature flags for an org from the Developer Console web API.
41+
*
42+
* @param {string} orgId Organization AMS ID
43+
* @returns {Promise<Array<{name: string, description: string}>>} feature flags
44+
*/
45+
async getOrgFeatures (orgId) {
46+
const baseUrl = CONSOLE_API_URLS[this.cliEnv] || CONSOLE_API_URLS.prod
47+
const response = await fetch(`${baseUrl}/console/api/organizations/${orgId}/features`, {
48+
headers: {
49+
accept: 'application/json',
50+
authorization: `Bearer ${this.accessToken}`,
51+
'x-api-key': this.apiKey
52+
}
53+
})
54+
if (!response.ok) {
55+
aioConsoleLogger.debug(`getOrgFeatures: non-ok response ${response.status} for org ${orgId}`)
56+
return []
57+
}
58+
return response.json()
59+
}
60+
61+
/**
62+
* Test whether an org has the Runtime feature.
63+
*
64+
* @param {string} orgId Organization AMS ID
65+
* @returns {Promise<boolean>} true when Runtime is enabled
66+
*/
67+
async hasRuntimeFeature (orgId) {
68+
try {
69+
const features = await this.getOrgFeatures(orgId)
70+
return features.some(feature => feature.name === ORG_FEATURE_RUNTIME)
71+
} catch (err) {
72+
aioConsoleLogger.debug(err)
73+
return false
74+
}
75+
}
76+
77+
/**
78+
* Filter orgs to those that can be used by Developer Console App Builder flows.
79+
*
80+
* @param {Array<{id: string, type: string}>} orgs organizations
81+
* @returns {Promise<Array<object>>} selectable organizations
82+
*/
83+
async getSelectableOrgs (orgs) {
84+
const checks = await Promise.all(orgs.map(async org => {
85+
if (org.type === ORG_TYPE_ENTERPRISE) {
86+
return true
87+
}
88+
if (org.type === ORG_TYPE_DEVELOPER) {
89+
return this.hasRuntimeFeature(org.id)
90+
}
91+
return false
92+
}))
93+
return orgs.filter((_, i) => checks[i])
94+
}
95+
3996
/**
4097
* Output JSON data
4198
*

‎src/commands/console/org/list.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ governing permissions and limitations under the License.
1313
const aioConsoleLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-console:org:list', { provider: 'debug' })
1414
const { Flags } = require('@oclif/core')
1515
const { table } = require('../../../utils/table')
16-
const { ORG_TYPE_ENTERPRISE } = require('../../../config')
1716

1817
const ConsoleCommand = require('../index')
1918

@@ -48,9 +47,8 @@ class ListCommand extends ConsoleCommand {
4847
*/
4948
async getConsoleOrgs () {
5049
const response = await this.consoleCLI.getOrganizations()
51-
const orgs = response
52-
// Filter enterprise orgs
53-
.filter(org => org.type === ORG_TYPE_ENTERPRISE)
50+
const selectableOrgs = await this.getSelectableOrgs(response)
51+
const orgs = selectableOrgs
5452
// Omit props
5553
.map(({ id, code, name }) => ({ id, code, name }))
5654

‎src/commands/console/org/select.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SelectCommand extends ConsoleCommand {
4141
}
4242

4343
async selectOrgInteractive (preSelectedOrgIdOrCode) {
44-
const orgs = await this.consoleCLI.getOrganizations()
44+
const orgs = await this.getSelectableOrgs(await this.consoleCLI.getOrganizations())
4545
const org = await this.consoleCLI.promptForSelectOrganization(
4646
orgs,
4747
{ orgId: preSelectedOrgIdOrCode, orgCode: preSelectedOrgIdOrCode }

‎src/config.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ governing permissions and limitations under the License.
1010
*/
1111

1212
const ORG_TYPE_ENTERPRISE = 'entp'
13+
const ORG_TYPE_DEVELOPER = 'developer'
14+
const ORG_FEATURE_RUNTIME = 'RUNTIME'
1315

1416
const API_KEYS = {
1517
prod: 'aio-cli-console-auth',
@@ -28,9 +30,17 @@ const OPEN_URLS = {
2830
stage: 'https://developer-stage.adobe.com/console/projects'
2931
}
3032

33+
const CONSOLE_API_URLS = {
34+
prod: 'https://developer.adobe.com',
35+
stage: 'https://developer-stage.adobe.com'
36+
}
37+
3138
module.exports = {
3239
ORG_TYPE_ENTERPRISE,
40+
ORG_TYPE_DEVELOPER,
41+
ORG_FEATURE_RUNTIME,
3342
CONFIG_KEYS,
3443
API_KEYS,
35-
OPEN_URLS
44+
OPEN_URLS,
45+
CONSOLE_API_URLS
3646
}

‎test/__fixtures__/org/list.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@
1313
"id": 3,
1414
"code": "CODE03",
1515
"name": "ORG03"
16+
},
17+
{
18+
"id": 4,
19+
"code": "CODE04",
20+
"name": "ORGFOURXX"
1621
}
1722
]

‎test/__fixtures__/org/list.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Org ID Code Org Name
2-
────── ────── ────────
2+
────── ────── ─────────
33
1 CODE01 ORG01
44
2 CODE02 ORG02
55
3 CODE03 ORG03
6+
4 CODE04 ORGFOURXX

‎test/__fixtures__/org/list.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
- id: 3
88
code: CODE03
99
name: ORG03
10+
- id: 4
11+
code: CODE04
12+
name: ORGFOURXX
1013

‎test/commands/console/index.test.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,74 @@ describe('ConsoleCommand', () => {
226226
command.clearConfig()
227227
expect(config.delete).toHaveBeenCalledWith(CONFIG_KEYS.CONSOLE)
228228
})
229+
230+
test('getOrgFeatures', async () => {
231+
command.cliEnv = STAGE_ENV
232+
command.apiKey = API_KEYS[STAGE_ENV]
233+
command.accessToken = 'token'
234+
global.fetch = jest.fn().mockResolvedValue({
235+
ok: true,
236+
json: jest.fn().mockResolvedValue([{ name: 'RUNTIME' }])
237+
})
238+
239+
await expect(command.getOrgFeatures('304327')).resolves.toEqual([{ name: 'RUNTIME' }])
240+
expect(global.fetch).toHaveBeenCalledWith('https://developer-stage.adobe.com/console/api/organizations/304327/features', {
241+
headers: {
242+
accept: 'application/json',
243+
authorization: 'Bearer token',
244+
'x-api-key': API_KEYS[STAGE_ENV]
245+
}
246+
})
247+
})
248+
249+
test('getOrgFeatures returns empty list for non-ok response', async () => {
250+
command.cliEnv = PROD_ENV
251+
command.apiKey = API_KEYS[PROD_ENV]
252+
command.accessToken = 'token'
253+
global.fetch = jest.fn().mockResolvedValue({ ok: false })
254+
255+
await expect(command.getOrgFeatures('304327')).resolves.toEqual([])
256+
})
257+
258+
test('getOrgFeatures falls back to prod url for unknown env', async () => {
259+
command.cliEnv = 'unknown-env'
260+
command.apiKey = API_KEYS[PROD_ENV]
261+
command.accessToken = 'token'
262+
global.fetch = jest.fn().mockResolvedValue({
263+
ok: true,
264+
json: jest.fn().mockResolvedValue([])
265+
})
266+
267+
await expect(command.getOrgFeatures('304327')).resolves.toEqual([])
268+
expect(global.fetch).toHaveBeenCalledWith('https://developer.adobe.com/console/api/organizations/304327/features', expect.any(Object))
269+
})
270+
271+
test('hasRuntimeFeature', async () => {
272+
command.getOrgFeatures = jest.fn().mockResolvedValue([{ name: 'RUNTIME' }])
273+
await expect(command.hasRuntimeFeature('304327')).resolves.toBe(true)
274+
275+
command.getOrgFeatures = jest.fn().mockResolvedValue([{ name: 'OTHER' }])
276+
await expect(command.hasRuntimeFeature('304327')).resolves.toBe(false)
277+
})
278+
279+
test('hasRuntimeFeature returns false when feature lookup fails', async () => {
280+
command.getOrgFeatures = jest.fn().mockRejectedValue(new Error('bad feature lookup'))
281+
await expect(command.hasRuntimeFeature('304327')).resolves.toBe(false)
282+
})
283+
284+
test('getSelectableOrgs includes enterprise orgs and developer orgs with Runtime', async () => {
285+
command.hasRuntimeFeature = jest.fn(orgId => Promise.resolve(orgId === 'developer-runtime'))
286+
const orgs = [
287+
{ id: 'enterprise', type: 'entp' },
288+
{ id: 'developer-runtime', type: 'developer' },
289+
{ id: 'developer-no-runtime', type: 'developer' },
290+
{ id: 'other', type: 'other' }
291+
]
292+
293+
await expect(command.getSelectableOrgs(orgs)).resolves.toEqual([
294+
{ id: 'enterprise', type: 'entp' },
295+
{ id: 'developer-runtime', type: 'developer' }
296+
])
297+
})
229298
})
230299
})

‎test/commands/console/org/list.test.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ function setDefaultMockConsoleCLI () {
1818
{ id: 1, code: 'CODE01', name: 'ORG01', type: 'entp' },
1919
{ id: 2, code: 'CODE02', name: 'ORG02', type: 'entp' },
2020
{ id: 3, code: 'CODE03', name: 'ORG03', type: 'entp' },
21-
{ id: 3, code: 'CODE03', name: 'ORG03', type: 'not_entp' }
21+
{ id: 4, code: 'CODE04', name: 'ORGFOURXX', type: 'developer' },
22+
{ id: 5, code: 'CODE05', name: 'ORG05', type: 'not_entp' },
23+
{ id: 6, code: 'CODE06', name: 'ORG06', type: 'developer' }
2224
])
2325
}
2426
jest.mock('@adobe/aio-cli-lib-console', () => ({
@@ -31,6 +33,10 @@ const ListCommand = require('../../../../src/commands/console/org/list')
3133
let command
3234
beforeEach(() => {
3335
setDefaultMockConsoleCLI()
36+
global.fetch = jest.fn(url => Promise.resolve({
37+
ok: true,
38+
json: () => Promise.resolve(url.endsWith('/4/features') ? [{ name: 'RUNTIME' }] : [])
39+
}))
3440
command = new ListCommand([])
3541
})
3642

@@ -64,7 +70,7 @@ describe('console:org:list', () => {
6470
})
6571
test('should return list of orgs', async () => {
6672
await expect(command.run()).resolves.not.toThrow()
67-
expect(stdout.output).toMatchFixture('org/list.txt')
73+
expect(stdout.output).toMatchFixtureIgnoreWhite('org/list.txt')
6874
})
6975

7076
test('should return list of orgs as json', async () => {

‎test/commands/console/org/select.test.js‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ const orgs = [
1616
{ id: '1', code: 'CODE01', name: 'ORG01', type: 'entp' },
1717
{ id: '2', code: 'CODE02', name: 'ORG02', type: 'entp' },
1818
{ id: '3', code: 'CODE03', name: 'ORG03', type: 'entp' },
19-
{ id: '33', code: 'CODE03', name: 'ORG03', type: 'not_entp' }
19+
{ id: '4', code: 'CODE04', name: 'ORG04', type: 'developer' },
20+
{ id: '6', code: 'CODE06', name: 'ORG06', type: 'developer' },
21+
{ id: '33', code: 'CODE33', name: 'ORG33', type: 'not_entp' }
2022
]
23+
const selectableOrgs = orgs.slice(0, 4)
2124
const selectedOrg = { id: '1', code: 'CODE01', name: 'ORG01', type: 'entp' }
25+
const selectedDeveloperOrg = { id: '4', code: 'CODE04', name: 'ORG04', type: 'developer' }
2226
/** @private */
2327
function setDefaultMockConsoleCLI () {
2428
mockConsoleCLIInstance.getOrganizations = jest.fn().mockResolvedValue(orgs)
@@ -36,6 +40,10 @@ let command
3640
beforeEach(() => {
3741
command = new SelectCommand([])
3842
setDefaultMockConsoleCLI()
43+
global.fetch = jest.fn(url => Promise.resolve({
44+
ok: true,
45+
json: () => Promise.resolve(url.endsWith('/4/features') ? [{ name: 'RUNTIME' }] : [])
46+
}))
3947
config.set.mockReset()
4048
config.delete.mockReset()
4149
})
@@ -73,7 +81,7 @@ describe('console:org:select', () => {
7381
test('should select the provided org code', async () => {
7482
command.argv = [selectedOrg.code]
7583
await expect(command.run()).resolves.not.toThrow()
76-
expect(mockConsoleCLIInstance.promptForSelectOrganization).toHaveBeenCalledWith(orgs, { orgCode: selectedOrg.code, orgId: selectedOrg.code })
84+
expect(mockConsoleCLIInstance.promptForSelectOrganization).toHaveBeenCalledWith(selectableOrgs, { orgCode: selectedOrg.code, orgId: selectedOrg.code })
7785
// stores the org configuration
7886
expect(config.set).toHaveBeenCalledWith('console.org', { code: selectedOrg.code, id: selectedOrg.id, name: selectedOrg.name })
7987
expect(config.delete).toHaveBeenCalledWith('console.project')
@@ -82,7 +90,7 @@ describe('console:org:select', () => {
8290
test('should select the provided org id', async () => {
8391
command.argv = [selectedOrg.id]
8492
await expect(command.run()).resolves.not.toThrow()
85-
expect(mockConsoleCLIInstance.promptForSelectOrganization).toHaveBeenCalledWith(orgs, { orgCode: selectedOrg.id, orgId: selectedOrg.id })
93+
expect(mockConsoleCLIInstance.promptForSelectOrganization).toHaveBeenCalledWith(selectableOrgs, { orgCode: selectedOrg.id, orgId: selectedOrg.id })
8694
// stores the org configuration
8795
expect(config.set).toHaveBeenCalledWith('console.org', { code: selectedOrg.code, id: selectedOrg.id, name: selectedOrg.name })
8896
expect(config.delete).toHaveBeenCalledWith('console.project')
@@ -91,13 +99,23 @@ describe('console:org:select', () => {
9199
test('should prompt for selection if no org is provided', async () => {
92100
command.argv = []
93101
await expect(command.run()).resolves.not.toThrow()
94-
expect(mockConsoleCLIInstance.promptForSelectOrganization).toHaveBeenCalledWith(orgs, { orgCode: undefined, orgId: undefined })
102+
expect(mockConsoleCLIInstance.promptForSelectOrganization).toHaveBeenCalledWith(selectableOrgs, { orgCode: undefined, orgId: undefined })
95103
// stores the org configuration
96104
expect(config.set).toHaveBeenCalledWith('console.org', { code: selectedOrg.code, id: selectedOrg.id, name: selectedOrg.name })
97105
expect(config.delete).toHaveBeenCalledWith('console.project')
98106
expect(config.delete).toHaveBeenCalledWith('console.workspace')
99107
})
100108

109+
test('should select the provided developer org id', async () => {
110+
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(selectedDeveloperOrg)
111+
command.argv = [selectedDeveloperOrg.id]
112+
await expect(command.run()).resolves.not.toThrow()
113+
expect(mockConsoleCLIInstance.promptForSelectOrganization).toHaveBeenCalledWith(selectableOrgs, { orgCode: selectedDeveloperOrg.id, orgId: selectedDeveloperOrg.id })
114+
expect(config.set).toHaveBeenCalledWith('console.org', { code: selectedDeveloperOrg.code, id: selectedDeveloperOrg.id, name: selectedDeveloperOrg.name })
115+
expect(config.delete).toHaveBeenCalledWith('console.project')
116+
expect(config.delete).toHaveBeenCalledWith('console.workspace')
117+
})
118+
101119
test('throw Error retrieving Orgs', async () => {
102120
mockConsoleCLIInstance.getOrganizations.mockRejectedValue(new Error('error org'))
103121
await expect(command.run()).rejects.toThrow(new Error('error org'))

0 commit comments

Comments
 (0)