|
| 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 | +} |
0 commit comments