Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding luis:version:delete cmd #355

Merged
merged 5 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/luis/src/commands/luis/version/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*!
* 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 LuisVersionDelete extends Command {
static description = 'Deletes a version of a LUIS application'

static examples = [`
$ bf luis:version:delete --appId {APP_ID} --versionId {VERSION_ID} --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY}
`]

static flags = {
help: flags.help({char: 'h'}),
appId: flags.string({description: 'LUIS application Id'}),
versionId: flags.string({description: 'LUIS application version Id'}),
endpoint: flags.string({description: 'LUIS endpoint hostname'}),
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}),
}

async run() {
const {flags} = this.parse(LuisVersionDelete)
const flagLabels = Object.keys(LuisVersionDelete.flags)
const configDir = this.config.configDir

const {
appId,
versionId,
endpoint,
subscriptionKey,
} = await utils.processInputs(flags, flagLabels, configDir)

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

const client = utils.getLUISClient(subscriptionKey, endpoint)

try {
await client.versions.deleteMethod(appId, versionId)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the azure client 'deleteMethod' returns null, so I just await it and output a success message if there's no error. their api is a little inconsistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@christopheranderson should we open a bug against the sdk on details like this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, definitely should.

this.log(`Successfully deleted version ${versionId}`)
} catch (err) {
throw new CLIError(`Failed to delete app version: ${err}`)
}
}
}
8 changes: 0 additions & 8 deletions packages/luis/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ const getLUISClient = (subscriptionKey: string, endpoint: string) => {
return luisClient
}

const getPropFromConfig = async (prop: string, configDir: string) => {
const config = await getUserConfig(configDir)
if (config && config[prop]) {
return config[prop]
}
}

const processInputs = async (flags: any, flagLabels: string[], configDir: string) => {
const configPrefix = 'luis__'
let config = await getUserConfig(configDir)
Expand All @@ -67,6 +60,5 @@ const validateRequiredProps = (configObj: any) => {

module.exports.getLUISClient = getLUISClient
module.exports.getUserConfig = getUserConfig
module.exports.getPropFromConfig = getPropFromConfig
module.exports.processInputs = processInputs
module.exports.validateRequiredProps = validateRequiredProps
58 changes: 58 additions & 0 deletions packages/luis/test/commands/luis/version/delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {expect, test} from '@oclif/test'
const sinon = require('sinon')
const uuidv1 = require('uuid/v1')
const utils = require('../../../../src/utils/index')

describe('luis:version:delete', () => {

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

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

test
.stdout()
.command(['luis:version:delete', '--help'])
.it('should print the help contents when --help is passed as an argument', ctx => {
expect(ctx.stdout).to.contain('Deletes a version of a LUIS application')
})

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

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

test
.nock('https://westus.api.cognitive.microsoft.com', api => api
.delete(uri => uri.includes('version'))
.reply(200)
)
.stdout()
.command(['luis:version:delete', '--appId', uuidv1(), '--versionId', '0.2', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
.it('deletes a luis app and displays a success message', ctx => {
expect(ctx.stdout).to.contain('Successfully deleted version')
})

test
.stdout()
.stderr()
.command(['luis:version:delete', '--appId', uuidv1(), '--versionId', '0.2', '--endpoint', 'undefined', '--subscriptionKey', uuidv1()])
.it('fails to delete an app and displays an error message if the endpoint is undefined', ctx => {
expect(ctx.stderr).to.contain('Failed to delete app version')
})

})