-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: list tenant when user clicks switch tenant button (#12580)
* feat: add switch tenant command in context item * feat: add switch tenant context button for Azure account * refactor: use dynamic option item instead of static to avoid silent long time loading * test: add ut * refactor: update error and related msg * test: add ut to raise code coverage
- Loading branch information
1 parent
d1fdfb5
commit 57c8910
Showing
6 changed files
with
338 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/vscode-extension/src/handlers/accounts/switchTenantHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { listAllTenants } from "@microsoft/teamsfx-core/build/common/tools"; | ||
import { ExtTelemetry } from "../../telemetry/extTelemetry"; | ||
import { AccountType, TelemetryEvent, TelemetryProperty } from "../../telemetry/extTelemetryEvents"; | ||
import { getTriggerFromProperty } from "../../utils/telemetryUtils"; | ||
import M365TokenInstance from "../../commonlib/m365Login"; | ||
import azureAccountManager from "../../commonlib/azureLogin"; | ||
import { AzureScopes, isUserCancelError } from "@microsoft/teamsfx-core"; | ||
import { FxError, SingleSelectConfig, SystemError } from "@microsoft/teamsfx-api"; | ||
import { localize } from "../../utils/localizeUtils"; | ||
import { VS_CODE_UI } from "../../qm/vsc_ui"; | ||
import { ExtensionSource } from "../../error/error"; | ||
import { showError } from "../../error/common"; | ||
|
||
export async function onSwitchM365Tenant(...args: unknown[]): Promise<void> { | ||
ExtTelemetry.sendTelemetryEvent(TelemetryEvent.SwitchTenantStart, { | ||
[TelemetryProperty.AccountType]: AccountType.M365, | ||
...getTriggerFromProperty(args), | ||
}); | ||
|
||
let error: FxError | undefined = undefined; | ||
const tokenRes = await M365TokenInstance.getAccessToken({ | ||
scopes: AzureScopes, | ||
}); | ||
if (tokenRes.isOk()) { | ||
const config: SingleSelectConfig = { | ||
name: "SwitchTenant", | ||
title: localize("teamstoolkit.handlers.switchtenant.quickpick.title"), | ||
options: async () => { | ||
const tenants = await listAllTenants(tokenRes.value); | ||
return tenants.map((tenant: any) => { | ||
return { | ||
id: tenant.tenantId, | ||
label: tenant.displayName, | ||
description: tenant.defaultDomain, | ||
}; | ||
}); | ||
}, | ||
}; | ||
const result = await VS_CODE_UI.selectOption(config); | ||
if (result.isOk()) { | ||
// TODO: set tenant | ||
ExtTelemetry.sendTelemetryEvent(TelemetryEvent.SwitchTenant, { | ||
[TelemetryProperty.AccountType]: AccountType.M365, | ||
...getTriggerFromProperty(args), | ||
}); | ||
return; | ||
} else { | ||
error = result.error; | ||
} | ||
} else { | ||
error = tokenRes.error; | ||
} | ||
|
||
if (!isUserCancelError(error)) { | ||
void showError(error); | ||
} | ||
ExtTelemetry.sendTelemetryErrorEvent(TelemetryEvent.SwitchTenant, error, { | ||
[TelemetryProperty.AccountType]: AccountType.M365, | ||
...getTriggerFromProperty(args), | ||
}); | ||
} | ||
|
||
export async function onSwitchAzureTenant(...args: unknown[]): Promise<void> { | ||
ExtTelemetry.sendTelemetryEvent(TelemetryEvent.SwitchTenantStart, { | ||
[TelemetryProperty.AccountType]: AccountType.Azure, | ||
...getTriggerFromProperty(args), | ||
}); | ||
|
||
const config: SingleSelectConfig = { | ||
name: "SwitchTenant", | ||
title: localize("teamstoolkit.handlers.switchtenant.quickpick.title"), | ||
options: async () => { | ||
const tokenCredential = await azureAccountManager.getIdentityCredentialAsync(false); | ||
const token = tokenCredential ? await tokenCredential.getToken(AzureScopes) : undefined; | ||
if (token && token.token) { | ||
const tenants = await listAllTenants(token.token); | ||
return tenants.map((tenant: any) => { | ||
return { | ||
id: tenant.tenantId, | ||
label: tenant.displayName, | ||
description: tenant.defaultDomain, | ||
}; | ||
}); | ||
} else { | ||
throw new SystemError( | ||
ExtensionSource, | ||
"SwitchTenantFailed", | ||
localize("teamstoolkit.handlers.switchtenant.error") | ||
); | ||
} | ||
}, | ||
}; | ||
const result = await VS_CODE_UI.selectOption(config); | ||
if (result.isOk()) { | ||
// TODO: set tenant | ||
ExtTelemetry.sendTelemetryEvent(TelemetryEvent.SwitchTenant, { | ||
[TelemetryProperty.AccountType]: AccountType.Azure, | ||
...getTriggerFromProperty(args), | ||
}); | ||
return; | ||
} else { | ||
if (!isUserCancelError(result.error)) { | ||
void showError(result.error); | ||
} | ||
ExtTelemetry.sendTelemetryErrorEvent(TelemetryEvent.SwitchTenant, result.error, { | ||
[TelemetryProperty.AccountType]: AccountType.Azure, | ||
...getTriggerFromProperty(args), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.