Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit a95f65a

Browse files
author
JSpru
authored
Adding luis:endpoints:list cmd (#374)
* Adding luis:endpoints:list cmd * Update var name * Update description * Increase mocha timeout * Fix test
1 parent 0157d27 commit a95f65a

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*!
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
import {CLIError, Command, flags} from '@microsoft/bf-cli-command'
7+
8+
const utils = require('../../../utils/index')
9+
10+
export default class LuisEndpointsList extends Command {
11+
static description = 'Returns available deployment endpoints'
12+
13+
static examples = [`
14+
$ bf luis:endpoints:list --appId {APPLICATION_ID} --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --out {PATH_TO_JSON_FILE}
15+
`]
16+
17+
static flags = {
18+
help: flags.help({char: 'h'}),
19+
endpoint: flags.string({description: 'LUIS endpoint hostname'}),
20+
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}),
21+
appId: flags.string({description: 'LUIS application Id'}),
22+
out: flags.string({char: 'o', description: 'Path to the directory where the exported file will be placed.'}),
23+
force: flags.boolean({char: 'f', description: 'If --out flag is provided with the path to an existing file, overwrites that file', default: false}),
24+
}
25+
26+
async run() {
27+
const {flags} = this.parse(LuisEndpointsList)
28+
const flagLabels = Object.keys(LuisEndpointsList.flags)
29+
const configDir = this.config.configDir
30+
const options: any = {}
31+
32+
let {endpoint, subscriptionKey, appId, force, out} = await utils.processInputs(flags, flagLabels, configDir)
33+
34+
const requiredProps = {endpoint, subscriptionKey}
35+
utils.validateRequiredProps(requiredProps)
36+
37+
const client = utils.getLUISClient(subscriptionKey, endpoint)
38+
39+
try {
40+
const endpointsList = await client.apps.listEndpoints(appId, options)
41+
if (out) {
42+
const writtenFilePath: string = await utils.writeToFile(out, endpointsList, force)
43+
this.log(`\nList successfully written to file: ${writtenFilePath}`)
44+
} else {
45+
await utils.writeToConsole(endpointsList)
46+
this.log('\nList successfully output to console')
47+
}
48+
} catch (err) {
49+
throw new CLIError(`Failed to export endpoints list: ${err}`)
50+
}
51+
}
52+
53+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {expect, test} from '@oclif/test'
2+
const sinon = require('sinon')
3+
const uuidv1 = require('uuid/v1')
4+
const utils = require('../../../../src/utils/index')
5+
const fs = require('fs-extra')
6+
import * as rimraf from 'rimraf'
7+
8+
describe('luis:endpoints:list', () => {
9+
10+
before(() => {
11+
fs.mkdirSync('./testout');
12+
});
13+
14+
after(() => {
15+
rimraf('./testout', (err) => {
16+
if (err) console.log(err);
17+
})
18+
});
19+
20+
beforeEach(() => {
21+
sinon.stub(utils, 'processInputs').returnsArg(0)
22+
})
23+
24+
afterEach(() => {
25+
sinon.restore();
26+
});
27+
28+
test
29+
.stdout()
30+
.command(['luis:endpoints:list', '--help'])
31+
.it('should print the help contents when --help is passed as an argument', ctx => {
32+
expect(ctx.stdout).to.contain('Returns available deployment endpoints')
33+
})
34+
35+
test
36+
.stdout()
37+
.stderr()
38+
.command(['luis:endpoints:list', '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
39+
.it('displays an error if any required input parameters are missing', ctx => {
40+
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`)
41+
})
42+
43+
test
44+
.nock('https://westus.api.cognitive.microsoft.com', api => api
45+
.get(uri => uri.includes('endpoints'))
46+
.reply(200, {"westus": "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/99999"})
47+
)
48+
.stdout()
49+
.command(['luis:endpoints:list', '--appId', uuidv1(), '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
50+
.it('displays a list of endpoints', ctx => {
51+
expect(ctx.stdout).to.contain('westus')
52+
})
53+
54+
test
55+
.nock('https://westus.api.cognitive.microsoft.com', api => api
56+
.get(uri => uri.includes('endpoints'))
57+
.reply(200, {"westus": "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/99999"})
58+
)
59+
.stdout()
60+
.command(['luis:endpoints:list', '--out', './testout/test.json', '--appId', uuidv1(), '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
61+
.it('export a list of endpoints to the specified file', ctx => {
62+
expect(ctx.stdout).to.contain('List successfully written to file')
63+
expect(ctx.stdout).to.contain('test.json')
64+
})
65+
66+
test
67+
.nock('https://westus.api.cognitive.microsoft.com', api => api
68+
.get(uri => uri.includes('endpoints'))
69+
.reply(200, {"westus": "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/99999"})
70+
)
71+
.stdout()
72+
.stderr()
73+
.command(['luis:endpoints:list', '--appId', uuidv1(), '--out', 'xyz', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
74+
.it('displays a list of endpoints and a success message in the console (since the target path provided is invalid)', ctx => {
75+
expect(ctx.stderr).to.contain('Target directory path doesn\'t exist:')
76+
})
77+
78+
})

packages/luis/test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
--watch-extensions ts
33
--recursive
44
--reporter spec
5-
--timeout 5000
5+
--timeout 10000

0 commit comments

Comments
 (0)