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

Adding luis:endpoints:list cmd #374

Merged
merged 11 commits into from
Nov 22, 2019
53 changes: 53 additions & 0 deletions packages/luis/src/commands/luis/endpoints/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import {CLIError, Command, flags} from '@microsoft/bf-cli-command'

const utils = require('../../../utils/index')

export default class LuisEndpointsList extends Command {
static description = 'Returns available deployment endpoints'

static examples = [`
$ bf luis:endpoints:list --appId {APPLICATION_ID} --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --out {PATH_TO_JSON_FILE}
`]

static flags = {
help: flags.help({char: 'h'}),
endpoint: flags.string({description: 'LUIS endpoint hostname'}),
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}),
appId: flags.string({description: 'LUIS application Id'}),
out: flags.string({char: 'o', description: 'Path to the directory where the exported file will be placed.'}),
force: flags.boolean({char: 'f', description: 'If --out flag is provided with the path to an existing file, overwrites that file', default: false}),
}

async run() {
const {flags} = this.parse(LuisEndpointsList)
const flagLabels = Object.keys(LuisEndpointsList.flags)
const configDir = this.config.configDir
const options: any = {}

let {endpoint, subscriptionKey, appId, force, out} = await utils.processInputs(flags, flagLabels, configDir)

const requiredProps = {endpoint, subscriptionKey}
utils.validateRequiredProps(requiredProps)

const client = utils.getLUISClient(subscriptionKey, endpoint)

try {
const endpointsList = await client.apps.listEndpoints(appId, options)
if (out) {
const writtenFilePath: string = await utils.writeToFile(out, endpointsList, force)
this.log(`\nList successfully written to file: ${writtenFilePath}`)
} else {
await utils.writeToConsole(endpointsList)
this.log('\nList successfully output to console')
}
} catch (err) {
throw new CLIError(`Failed to export endpoints list: ${err}`)
}
}

}
78 changes: 78 additions & 0 deletions packages/luis/test/commands/luis/endpoints/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {expect, test} from '@oclif/test'
const sinon = require('sinon')
const uuidv1 = require('uuid/v1')
const utils = require('../../../../src/utils/index')
const fs = require('fs-extra')
import * as rimraf from 'rimraf'

describe('luis:endpoints:list', () => {

before(() => {
fs.mkdirSync('./testout');
});

after(() => {
rimraf('./testout', (err) => {
if (err) console.log(err);
})
});

beforeEach(() => {
sinon.stub(utils, 'processInputs').returnsArg(0)
})

afterEach(() => {
sinon.restore();
});

test
.stdout()
.command(['luis:endpoints:list', '--help'])
.it('should print the help contents when --help is passed as an argument', ctx => {
expect(ctx.stdout).to.contain('Returns available deployment endpoints')
})

test
.stdout()
.stderr()
.command(['luis:endpoints:list', '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('displays an error if any required input parameters are missing', ctx => {
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`)
})

test
.nock('https://westus.api.cognitive.microsoft.com', api => api
.get(uri => uri.includes('endpoints'))
.reply(200, {"westus": "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/99999"})
)
.stdout()
.command(['luis:endpoints:list', '--appId', uuidv1(), '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('displays a list of endpoints', ctx => {
expect(ctx.stdout).to.contain('westus')
})

test
.nock('https://westus.api.cognitive.microsoft.com', api => api
.get(uri => uri.includes('endpoints'))
.reply(200, {"westus": "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/99999"})
)
.stdout()
.command(['luis:endpoints:list', '--out', './testout/test.json', '--appId', uuidv1(), '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('export a list of endpoints to the specified file', ctx => {
expect(ctx.stdout).to.contain('List successfully written to file')
expect(ctx.stdout).to.contain('test.json')
})

test
.nock('https://westus.api.cognitive.microsoft.com', api => api
.get(uri => uri.includes('endpoints'))
.reply(200, {"westus": "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/99999"})
)
.stdout()
.stderr()
.command(['luis:endpoints:list', '--appId', uuidv1(), '--out', 'xyz', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('displays a list of endpoints and a success message in the console (since the target path provided is invalid)', ctx => {
expect(ctx.stderr).to.contain('Target directory path doesn\'t exist:')
})

})
2 changes: 1 addition & 1 deletion packages/luis/test/mocha.opts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
--watch-extensions ts
--recursive
--reporter spec
--timeout 5000
--timeout 10000