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

Commit a9c258f

Browse files
benbrowncwhitten
andauthored
fix: allow tenantId to be specified in provisioning script (#3084)
* allow tenantId to be specified * use default tenantid Co-authored-by: Chris Whitten <[email protected]>
1 parent 4c27f68 commit a9c258f

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

Composer/plugins/samples/assets/shared/scripts/provisionComposer.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const logger = msg => {
2121
console.log(chalk.green(msg.message));
2222
}
2323
};
24-
var tenantId = '';
2524

2625
const usage = () => {
2726
const options = [
@@ -33,6 +32,7 @@ const usage = () => {
3332
['appId', 'Microsoft App ID (Will create if absent)'],
3433
['luisAuthoringKey', 'LUIS Authoring Key to use when publishing to LUIS'],
3534
['luisAuthoringRegion', 'Azure Region used with LUIS (defaults to westus)'],
35+
['tenantId','ID of your tenant if required (will choose first in list by default)'],
3636
['createLuisResource', 'Create a LUIS resource? Default true'],
3737
['createLuisAuthoringResource', 'Create a LUIS authoring resource? Default true'],
3838
['createCosmosDb', 'Create a CosmosDB? Default true'],
@@ -85,15 +85,16 @@ const appPassword = argv.appPassword;
8585
const environment = argv.environment || 'dev';
8686
const location = argv.location || 'westus';
8787
const appId = argv.appId; // MicrosoftAppId - generated if left blank
88-
const luisAuthoringKey = argv.luisAuthoringKey;
89-
const luisAuthoringRegion = argv.luisAuthoringRegion || 'westus';
88+
const luisAuthoringKey = argv.luisAuthoringKey; // not currently used
89+
const luisAuthoringRegion = argv.luisAuthoringRegion || 'westus'; // not currently used
9090

9191
// Get option flags
9292
const createLuisResource = argv.createLuisResource == 'false' ? false : true;
9393
const createLuisAuthoringResource = argv.createLuisAuthoringResource == 'false' ? false : true;
9494
const createCosmosDb = argv.createCosmosDb == 'false' ? false : true;
9595
const createStorage = argv.createStorage == 'false' ? false : true;
9696
const createAppInsignts = argv.createAppInsignts == 'false' ? false : true;
97+
var tenantId = argv.tenantId ? argv.tenantId : '';
9798

9899
const templatePath =
99100
argv.customArmTemplate || path.join(__dirname, 'DeploymentTemplates', 'template-with-preexisting-rg.json');
@@ -194,7 +195,27 @@ const getTenantId = async (accessToken) => {
194195
if (jsonRes.value === undefined || (jsonRes.value && jsonRes.value.length === 0) || (jsonRes.value && jsonRes.value.length > 0 && jsonRes.value[0].tenantId === undefined)) {
195196
throw new Error(`No tenants found in the account.`);
196197
}
197-
return jsonRes.value[0].tenantId;
198+
const selectedTenant = jsonRes.value.shift();
199+
logger({
200+
status: BotProjectDeployLoggerType.PROVISION_INFO,
201+
message: `> Using Tenant ${ selectedTenant.displayName } - ID: ${ selectedTenant.tenantId }`,
202+
});
203+
// if alternatives exist, list htem
204+
if (jsonRes.value.length > 0) {
205+
logger({
206+
status: BotProjectDeployLoggerType.PROVISION_INFO,
207+
message: chalk.yellow(` Note: You have access to multiple tenants. To specify an alternative, specify --tenantId=<desired tenant ID>`),
208+
});
209+
// list all available tenants
210+
jsonRes.value.forEach((tenant) => {
211+
logger({
212+
status: BotProjectDeployLoggerType.PROVISION_INFO,
213+
message: chalk.yellow(` * ${ tenant.displayName } - ID: ${ tenant.tenantId }`),
214+
});
215+
});
216+
}
217+
218+
return selectedTenant.tenantId;
198219
} catch (err) {
199220
throw new Error(`Get Tenant Id Failed, details: ${getErrorMesssage(err)}`);
200221
}
@@ -345,7 +366,18 @@ const create = async (
345366
if (!tenantId) {
346367
const token = await creds.getToken();
347368
const accessToken = token.accessToken;
348-
tenantId = await getTenantId(accessToken);
369+
// the returned access token will almost surely have a tenantId.
370+
// use this as the default if one isn't specified.
371+
// otherwise, fetch a list and use the first, but print available options.
372+
if (token.tenantId) {
373+
tenantId = token.tenantId;
374+
logger({
375+
status: BotProjectDeployLoggerType.PROVISION_INFO,
376+
message: `> Using Tenant ID: ${ tenantId }`,
377+
});
378+
} else {
379+
tenantId = await getTenantId(accessToken);
380+
}
349381
}
350382

351383
const graphCreds = new msRestNodeAuth.DeviceTokenCredentials(

0 commit comments

Comments
 (0)