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

Commit d338053

Browse files
authored
Adding Assign Azure Account Command (#683)
1 parent 2d25957 commit d338053

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 LuisApplicationAssignazureaccount extends Command {
11+
static description = 'Assign a LUIS azure accounts to an application'
12+
13+
static flags: flags.Input<any> = {
14+
help: flags.help({char: 'h'}),
15+
appId: flags.string({description: '(required) LUIS application Id (defaults to config:LUIS:appId)'}),
16+
endpoint: flags.string({description: 'LUIS endpoint hostname'}),
17+
subscriptionKey: flags.string({description: '(required) LUIS cognitive services subscription key (default: config:LUIS:subscriptionKey)'}),
18+
azureSubscriptionId: flags.string({description: 'Azure Subscription Id', required: true}),
19+
resourceGroup: flags.string({description: 'Resource Group', required: true}),
20+
accountName: flags.string({description: 'Account name', required: true}),
21+
armToken: flags.string({description: 'The bearer authorization header to use; containing the user`s ARM token used to validate azure accounts information', required: true}),
22+
json: flags.boolean({description: 'Display output as JSON'})
23+
}
24+
25+
async run() {
26+
const {flags} = this.parse(LuisApplicationAssignazureaccount)
27+
const flagLabels = Object.keys(LuisApplicationAssignazureaccount.flags)
28+
const configDir = this.config.configDir
29+
30+
const {
31+
appId,
32+
endpoint,
33+
subscriptionKey,
34+
} = await utils.processInputs(flags, flagLabels, configDir)
35+
36+
const requiredProps = {appId, endpoint, subscriptionKey}
37+
utils.validateRequiredProps(requiredProps)
38+
39+
const appJSON = {
40+
azureSubscriptionId: flags.azureSubscriptionId,
41+
resourceGroup: flags.resourceGroup,
42+
accountName: flags.accountName,
43+
}
44+
45+
try {
46+
let url = endpoint + '/luis/authoring/v3.0-preview/apps/' + appId + '/azureaccounts'
47+
const headers = {
48+
Authorization: 'Bearer ' + flags.armToken,
49+
'Content-Type': 'application/json',
50+
'Ocp-Apim-Subscription-Key': subscriptionKey
51+
}
52+
const response = await fetch(url, {method: 'POST', headers, body: JSON.stringify(appJSON)})
53+
const messageData = await response.json()
54+
55+
if (messageData.error) {
56+
throw new CLIError(messageData.error.message)
57+
}
58+
59+
const output: string = flags.json ? JSON.stringify({Status: 'Success'}, null, 2) : 'Account successfully assigned.'
60+
this.log(output)
61+
62+
} catch (err) {
63+
throw new CLIError(`Failed to assign accout: ${err}`)
64+
}
65+
}
66+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
6+
describe('luis:application:assignazureaccount', () => {
7+
8+
beforeEach(() => {
9+
sinon.stub(utils, 'processInputs').returnsArg(0)
10+
})
11+
12+
afterEach(() => {
13+
sinon.restore();
14+
});
15+
16+
test
17+
.nock('https://westus.api.cognitive.microsoft.com', api => api
18+
.post(uri => uri.includes('apps'))
19+
.reply(200, {'code': 'Success'})
20+
)
21+
.stdout()
22+
.command(['luis:application:assignazureaccount', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1(), '--azureSubscriptionId', uuidv1(), '--resourceGroup', 'adfhrkg', '--accountName', 'sfgsgdszg', '--appId', uuidv1(), '--armToken', 'ljkdsfhlrebclf'])
23+
.it('Assigns an azure account to a Luis app', ctx => {
24+
expect(ctx.stdout).to.contain('Account successfully assigned.')
25+
})
26+
27+
})

0 commit comments

Comments
 (0)