Skip to content

Commit 9f85efb

Browse files
feat(commerce): list command executions fixes #440 (#444)
1 parent dee0b72 commit 9f85efb

File tree

3 files changed

+263
-1
lines changed

3 files changed

+263
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"author": "Adobe Inc.",
66
"bugs": "https://github.com/adobe/aio-cli-plugin-cloudmanager/issues",
77
"dependencies": {
8-
"@adobe/aio-lib-cloudmanager": "^1.4.0",
8+
"@adobe/aio-lib-cloudmanager": "^1.5.0",
99
"@adobe/aio-lib-core-config": "^2.0.0",
1010
"@adobe/aio-lib-core-errors": "^3.0.1",
1111
"@adobe/aio-lib-core-logging": "^1.2.0",
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Copyright 2021 Adobe. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
const BaseCommand = require('../../../base-command')
14+
const {
15+
initSdk,
16+
formatTime,
17+
getProgramId,
18+
} = require('../../../cloudmanager-helpers')
19+
const { cli } = require('cli-ux')
20+
const { flags } = require('@oclif/command')
21+
const commonFlags = require('../../../common-flags')
22+
23+
class ListCommandExecutionsCommand extends BaseCommand {
24+
async run () {
25+
const { args, flags } = this.parse(ListCommandExecutionsCommand)
26+
27+
const programId = getProgramId(flags)
28+
29+
const commandsRetrievalMessage = 'Getting Commerce Command Executions'
30+
const commandsRetrievedMessage = 'Retrieved Commerce Command Executions'
31+
32+
cli.action.start(commandsRetrievalMessage)
33+
34+
const result = await this.getCommands(
35+
programId,
36+
args.environmentId,
37+
flags.type,
38+
flags.status,
39+
flags.command,
40+
)
41+
42+
cli.action.start(commandsRetrievedMessage)
43+
cli.action.stop('\n')
44+
45+
cli.table(result, {
46+
id: {},
47+
type: {},
48+
command: {},
49+
startedBy: {
50+
header: 'Started By',
51+
},
52+
startedAt: {
53+
header: 'Started At',
54+
get: formatTime('startedAt'),
55+
},
56+
completedAt: {
57+
header: 'Completed At',
58+
get: formatTime('completedAt'),
59+
},
60+
status: {},
61+
})
62+
console.log('\n')
63+
64+
return result
65+
}
66+
67+
async getCommands (
68+
programId,
69+
environmentId,
70+
type,
71+
status,
72+
command,
73+
imsContextName = null,
74+
) {
75+
const sdk = await initSdk(imsContextName)
76+
return sdk.getCommerceCommandExecutions(
77+
programId,
78+
environmentId,
79+
type,
80+
status,
81+
command,
82+
)
83+
}
84+
}
85+
86+
ListCommandExecutionsCommand.description =
87+
'get status of a single commerce cli command'
88+
89+
ListCommandExecutionsCommand.flags = {
90+
...commonFlags.global,
91+
...commonFlags.programId,
92+
type: flags.string({
93+
char: 't',
94+
required: false,
95+
description: 'filter by type of command',
96+
options: ['bin/magento'],
97+
}),
98+
status: flags.string({
99+
char: 's',
100+
required: false,
101+
description: 'filter by status of command',
102+
options: [
103+
'PENDING',
104+
'RUNNING',
105+
'CANCELLED',
106+
'COMPLETED',
107+
'FAILED',
108+
'CANCELLING',
109+
'CANCEL_FAILED',
110+
'UNKNOWN',
111+
],
112+
}),
113+
command: flags.string({
114+
char: 'c',
115+
required: false,
116+
description: 'filter by command',
117+
options: [
118+
'maintenance:status',
119+
'maintenance:enable',
120+
'maintenance:disable',
121+
'indexer:reindex',
122+
'cache:clean',
123+
'cache:flush',
124+
'app:config:dump',
125+
'app:config:import',
126+
],
127+
}),
128+
}
129+
130+
ListCommandExecutionsCommand.args = [
131+
{ name: 'environmentId', required: true, description: 'the environment id' },
132+
]
133+
134+
module.exports = ListCommandExecutionsCommand
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Copyright 2021 Adobe. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
const { cli } = require('cli-ux')
14+
const { init, mockSdk } = require('@adobe/aio-lib-cloudmanager')
15+
const { resetCurrentOrgId, setCurrentOrgId } = require('@adobe/aio-lib-ims')
16+
const ListCommandExecutionsCommand = require('../../../src/commands/cloudmanager/commerce/list-command-executions')
17+
18+
beforeEach(() => {
19+
resetCurrentOrgId()
20+
})
21+
22+
test('list-command-executions (commerce) - missing arg', async () => {
23+
expect.assertions(2)
24+
25+
const runResult = ListCommandExecutionsCommand.run([])
26+
await expect(runResult instanceof Promise).toBeTruthy()
27+
await expect(runResult).rejects.toSatisfy(
28+
(err) => err.message.indexOf('Missing 1 required arg') === 0,
29+
)
30+
})
31+
32+
test('list-command-executions (commerce) - missing config', async () => {
33+
expect.assertions(2)
34+
35+
const runResult = ListCommandExecutionsCommand.run([
36+
'--programId',
37+
'5',
38+
'10',
39+
])
40+
await expect(runResult instanceof Promise).toBeTruthy()
41+
await expect(runResult).rejects.toSatisfy(
42+
(err) =>
43+
err.message ===
44+
'[CloudManagerCLI:NO_IMS_CONTEXT] Unable to find IMS context aio-cli-plugin-cloudmanager.',
45+
)
46+
})
47+
48+
test('list-command-executions (commerce)', async () => {
49+
setCurrentOrgId('good')
50+
const result = [
51+
{
52+
id: 100,
53+
type: 'bin/magento',
54+
command: 'maintenance:status',
55+
options: [],
56+
startedBy: '[email protected]',
57+
startedAt: '2021-08-17T17:03:18.858+0000',
58+
completedAt: '2021-08-17T17:03:43.000+0000',
59+
name: 'magento-cli-952',
60+
status: 'COMPLETED',
61+
environmentId: 177253,
62+
},
63+
{
64+
id: 101,
65+
type: 'bin/magento',
66+
command: 'cache:clean',
67+
options: [],
68+
startedBy: '[email protected]',
69+
startedAt: '2021-08-17T17:03:18.858+0000',
70+
completedAt: '2021-08-17T17:03:43.000+0000',
71+
name: 'magento-cli-952',
72+
status: 'COMPLETED',
73+
environmentId: 177253,
74+
},
75+
]
76+
mockSdk.getCommerceCommandExecutions = jest.fn(() => {
77+
return Promise.resolve(result)
78+
})
79+
80+
// expect.assertions(9)
81+
82+
const runResult = ListCommandExecutionsCommand.run([
83+
'--programId',
84+
'5',
85+
'10',
86+
])
87+
await expect(runResult instanceof Promise).toBeTruthy()
88+
await runResult
89+
await expect(init.mock.calls.length).toEqual(1)
90+
await expect(init).toHaveBeenCalledWith(
91+
'good',
92+
'test-client-id',
93+
'fake-token',
94+
'https://cloudmanager.adobe.io',
95+
)
96+
await expect(mockSdk.getCommerceCommandExecutions.mock.calls.length).toEqual(
97+
1,
98+
)
99+
await expect(mockSdk.getCommerceCommandExecutions).toHaveBeenCalledWith(
100+
'5',
101+
'10',
102+
undefined,
103+
undefined,
104+
undefined,
105+
)
106+
await expect(cli.action.start.mock.calls[0][0]).toEqual(
107+
'Getting Commerce Command Executions',
108+
)
109+
await expect(cli.action.start.mock.calls[1][0]).toEqual(
110+
'Retrieved Commerce Command Executions',
111+
)
112+
await expect(cli.table.mock.calls[0][0]).toEqual(result)
113+
await expect(cli.action.stop).toHaveBeenCalled()
114+
})
115+
116+
test('list-command-executions (commerce) - api error', async () => {
117+
setCurrentOrgId('good')
118+
mockSdk.getCommerceCommandExecutions = jest.fn(() =>
119+
Promise.reject(new Error('Command failed.')),
120+
)
121+
const runResult = ListCommandExecutionsCommand.run([
122+
'--programId',
123+
'5',
124+
'10',
125+
])
126+
await expect(runResult instanceof Promise).toBeTruthy()
127+
await expect(runResult).rejects.toEqual(new Error('Command failed.'))
128+
})

0 commit comments

Comments
 (0)