Skip to content

Commit

Permalink
Add Tenant view telemetry and sign in button on tenant view (#979)
Browse files Browse the repository at this point in the history
* add telemetry and sign in button

* small changes

* change name

* change to plus and change name

* change to addAccount

---------

Co-authored-by: Alex Weininger <[email protected]>
  • Loading branch information
motm32 and alexweininger authored Dec 6, 2024
1 parent 5b66562 commit 66eba91
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
"title": "%azureResourceGroups.logIn%",
"category": "Azure"
},
{
"command": "azureTenantsView.addAccount",
"title": "%azureTenantsView.addAccount%",
"category": "Azure",
"icon": "$(add)"
},
{
"command": "azureResourceGroups.selectSubscriptions",
"title": "%azureResourceGroups.selectSubscriptions%",
Expand Down Expand Up @@ -392,6 +398,11 @@
"submenu": "azureWorkspaceCreate",
"when": "view == azureWorkspace",
"group": "navigation@1"
},
{
"command": "azureTenantsView.addAccount",
"when": "view == azureTenantsView",
"group": "navigation@1"
}
],
"view/item/context": [
Expand Down Expand Up @@ -511,6 +522,10 @@
{
"command": "azureResourceGroups.installExtension",
"when": "never"
},
{
"command": "azureTenantsView.addAccount",
"when": "never"
}
],
"azureResourceGroups.groupBy": [
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"azureResourceGroups.cloudShellPowerShell": "Azure Cloud Shell (PowerShell)",
"azureResourceGroups.uploadToCloudShell": "Upload to Cloud Shell",
"azureResourceGroups.logIn": "Sign In",
"azureTenantsView.addAccount": "Add account",
"azureResourceGroups.signInToTenant": "Sign in to Tenant (Directory)...",
"azureResourceGroups.selectSubscriptions": "Select Subscriptions...",
"azureResourceGroups.createResourceGroup": "Create Resource Group...",
Expand Down
1 change: 1 addition & 0 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function registerCommands(): void {
registerCommand('azureResourceGroups.unfocusGroup', unfocusGroup);

registerCommand('azureResourceGroups.logIn', (context: IActionContext) => logIn(context));
registerCommand('azureTenantsView.addAccount', (context: IActionContext) => logIn(context));
registerCommand('azureResourceGroups.selectSubscriptions', (context: IActionContext, options: SelectSubscriptionOptions) => selectSubscriptions(context, options));
registerCommand('azureResourceGroups.signInToTenant', async () => signInToTenant(await ext.subscriptionProviderFactory()));

Expand Down
57 changes: 30 additions & 27 deletions src/tree/tenants/TenantResourceTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { AzureSubscriptionProvider, getConfiguredAuthProviderId } from '@microsoft/vscode-azext-azureauth';
import { nonNullProp, nonNullValueAndProp } from '@microsoft/vscode-azext-utils';
import { callWithTelemetryAndErrorHandling, IActionContext, nonNullProp, nonNullValueAndProp } from '@microsoft/vscode-azext-utils';
import { ResourceModelBase } from 'api/src';
import * as vscode from 'vscode';
import { TenantResourceProviderManager } from '../../api/ResourceProviderManagers';
Expand Down Expand Up @@ -43,36 +43,39 @@ export class TenantResourceTreeDataProvider extends ResourceTreeDataProviderBase
}

async onGetChildren(element?: ResourceGroupsItem | undefined): Promise<ResourceGroupsItem[] | null | undefined> {
if (element) {
return await element.getChildren();
} else {
const subscriptionProvider = await getAzureSubscriptionProvider(this);
const children: ResourceGroupsItem[] = await OnGetChildrenBase(subscriptionProvider);
return await callWithTelemetryAndErrorHandling('azureTenantsView.getChildren', async (context: IActionContext) => {
if (element) {
return await element.getChildren();
} else {
const subscriptionProvider = await getAzureSubscriptionProvider(this);
const children: ResourceGroupsItem[] = await OnGetChildrenBase(subscriptionProvider);

if (children.length === 0) {
const accounts = await vscode.authentication.getAccounts(getConfiguredAuthProviderId());
for (const account of accounts) {
const tenants = await subscriptionProvider.getTenants(account);
const tenantItems: ResourceGroupsItem[] = [];
for await (const tenant of tenants) {
const isSignedIn = await subscriptionProvider.isSignedIn(nonNullProp(tenant, 'tenantId'), account);
tenantItems.push(new TenantTreeItem(tenant, account, {
contextValue: isSignedIn ? 'tenantName' : 'tenantNameNotSignedIn',
checkboxState: (!isSignedIn || isTenantFilteredOut(nonNullProp(tenant, 'tenantId'), account.id)) ?
vscode.TreeItemCheckboxState.Unchecked : vscode.TreeItemCheckboxState.Checked,
description: tenant.tenantId
if (children.length === 0) {
const accounts = await vscode.authentication.getAccounts(getConfiguredAuthProviderId());
context.telemetry.properties.accountCount = accounts.length.toString();
for (const account of accounts) {
const tenants = await subscriptionProvider.getTenants(account);
const tenantItems: ResourceGroupsItem[] = [];
for await (const tenant of tenants) {
const isSignedIn = await subscriptionProvider.isSignedIn(nonNullProp(tenant, 'tenantId'), account);
tenantItems.push(new TenantTreeItem(tenant, account, {
contextValue: isSignedIn ? 'tenantName' : 'tenantNameNotSignedIn',
checkboxState: (!isSignedIn || isTenantFilteredOut(nonNullProp(tenant, 'tenantId'), account.id)) ?
vscode.TreeItemCheckboxState.Unchecked : vscode.TreeItemCheckboxState.Checked,
description: tenant.tenantId
}));
}

children.push(new GenericItem(nonNullValueAndProp(account, 'label'), {
children: tenantItems,
iconPath: new vscode.ThemeIcon('account'),
contextValue: 'accountName',
collapsibleState: vscode.TreeItemCollapsibleState.Expanded,
}));
}

children.push(new GenericItem(nonNullValueAndProp(account, 'label'), {
children: tenantItems,
iconPath: new vscode.ThemeIcon('account'),
contextValue: 'accountName',
collapsibleState: vscode.TreeItemCollapsibleState.Expanded,
}));
}
return children;
}
return children;
}
});
}
}
4 changes: 3 additions & 1 deletion src/tree/tenants/registerTenantTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ export function registerTenantTree(context: vscode.ExtensionContext, options: Re
return tenantResourceTreeDataProvider;
}

async function updateTenantsSetting(_context: IActionContext, tenants: vscode.TreeCheckboxChangeEvent<TenantTreeItem>) {
async function updateTenantsSetting(context: IActionContext, tenants: vscode.TreeCheckboxChangeEvent<TenantTreeItem>) {
const unselectedTenants = getUnselectedTenants();
const unselectedTenantsSet = new Set(unselectedTenants);

for (const [tenantTreeItem, state] of tenants.items) {
if (state === vscode.TreeItemCheckboxState.Unchecked) {
context.telemetry.properties.uncheckedTenant = 'true';
unselectedTenantsSet.add(getKeyForTenant(tenantTreeItem.tenantId, tenantTreeItem.account.id));
} else if (state === vscode.TreeItemCheckboxState.Checked) {
context.telemetry.properties.checkedTenant = 'true';
const treeItem = await tenantTreeItem.getTreeItem();
if (treeItem?.contextValue === 'tenantNameNotSignedIn') {
await vscode.commands.executeCommand('azureTenantsView.signInToTenant', tenantTreeItem);
Expand Down

0 comments on commit 66eba91

Please sign in to comment.