diff --git a/src/commands/accounts/selectSubscriptions.ts b/src/commands/accounts/selectSubscriptions.ts index 3af153d4..68299b92 100644 --- a/src/commands/accounts/selectSubscriptions.ts +++ b/src/commands/accounts/selectSubscriptions.ts @@ -7,6 +7,7 @@ import { AzureSubscription } from "@microsoft/vscode-azext-azureauth"; import { IActionContext, IAzureQuickPickItem } from "@microsoft/vscode-azext-utils"; import * as vscode from "vscode"; import { ext } from "../../extensionVariables"; +import { isTenantFilteredOut } from "../../tree/tenants/registerTenantTree"; import { localize } from "../../utils/localize"; import { settingUtils } from "../../utils/settingUtils"; @@ -89,15 +90,8 @@ async function setSelectedTenantAndSubscriptionIds(tenantAndSubscriptionIds: str // This function is also used to filter subscription tree items in AzureResourceTreeDataProvider export function getTenantFilteredSubscriptions(allSubscriptions: AzureSubscription[]): AzureSubscription[] | undefined { - const tenants = ext.context.globalState.get('unselectedTenants'); - if (tenants && tenants.length > 0) { - allSubscriptions = allSubscriptions.filter(subscription => !tenants.includes(`${subscription.tenantId}/${subscription.account?.id}`)); - if (allSubscriptions.length > 0) { - return allSubscriptions; - } - } - - return undefined; + const filteredSubscriptions = allSubscriptions.filter(subscription => !isTenantFilteredOut(subscription.tenantId, subscription.account.id)); + return filteredSubscriptions.length > 0 ? filteredSubscriptions : allSubscriptions; } export function getDuplicateSubscriptions(subscriptions: AzureSubscription[]): AzureSubscription[] { diff --git a/src/tree/tenants/TenantResourceTreeDataProvider.ts b/src/tree/tenants/TenantResourceTreeDataProvider.ts index 289cfdc0..a79c9649 100644 --- a/src/tree/tenants/TenantResourceTreeDataProvider.ts +++ b/src/tree/tenants/TenantResourceTreeDataProvider.ts @@ -8,7 +8,6 @@ import { nonNullProp, nonNullValueAndProp } from '@microsoft/vscode-azext-utils' import { ResourceModelBase } from 'api/src'; import * as vscode from 'vscode'; import { TenantResourceProviderManager } from '../../api/ResourceProviderManagers'; -import { ext } from '../../extensionVariables'; import { BranchDataItemCache } from '../BranchDataItemCache'; import { GenericItem } from '../GenericItem'; import { getAzureSubscriptionProvider, OnGetChildrenBase } from '../OnGetChildrenBase'; @@ -16,6 +15,7 @@ import { ResourceGroupsItem } from '../ResourceGroupsItem'; import { ResourceTreeDataProviderBase } from "../ResourceTreeDataProviderBase"; import { TenantResourceBranchDataProviderManager } from "./TenantResourceBranchDataProviderManager"; import { TenantTreeItem } from './TenantTreeItem'; +import { isTenantFilteredOut } from './registerTenantTree'; export class TenantResourceTreeDataProvider extends ResourceTreeDataProviderBase { public subscriptionProvider: AzureSubscriptionProvider | undefined; @@ -58,9 +58,9 @@ export class TenantResourceTreeDataProvider extends ResourceTreeDataProviderBase const isSignedIn = await subscriptionProvider.isSignedIn(nonNullProp(tenant, 'tenantId'), account); tenantItems.push(new TenantTreeItem(nonNullProp(tenant, 'displayName'), nonNullProp(tenant, 'tenantId'), account, { contextValue: isSignedIn ? 'tenantName' : 'tenantNameNotSignedIn', - checkboxState: (!(isSignedIn) || this.checkUnselectedTenants(nonNullProp(tenant, 'tenantId'))) ? - vscode.TreeItemCheckboxState.Unchecked : vscode.TreeItemCheckboxState.Checked, // Make sure tenants which are not signed in are unchecked - description: tenant.defaultDomain + checkboxState: (!isSignedIn || isTenantFilteredOut(nonNullProp(tenant, 'tenantId'), account.id)) ? + vscode.TreeItemCheckboxState.Unchecked : vscode.TreeItemCheckboxState.Checked, + description: tenant.tenantId })); } @@ -75,14 +75,4 @@ export class TenantResourceTreeDataProvider extends ResourceTreeDataProviderBase return children; } } - - private checkUnselectedTenants(tenantId: string): boolean { - const settings = ext.context.globalState.get('unselectedTenants'); - if (settings) { - if (settings.includes(tenantId)) { - return true; - } - } - return false; - } } diff --git a/src/tree/tenants/registerTenantTree.ts b/src/tree/tenants/registerTenantTree.ts index c8a5e5b4..c49b590c 100644 --- a/src/tree/tenants/registerTenantTree.ts +++ b/src/tree/tenants/registerTenantTree.ts @@ -51,25 +51,64 @@ export function registerTenantTree(context: vscode.ExtensionContext, options: Re } async function updateTenantsSetting(_context: IActionContext, tenants: vscode.TreeCheckboxChangeEvent) { - const state = ext.context.globalState.get('unselectedTenants'); - const unselectedTenants = new Set(state ?? []); + const unselectedTenants = getUnselectedTenants(); + const unselectedTenantsSet = new Set(unselectedTenants); for (const [tenantTreeItem, state] of tenants.items) { if (state === vscode.TreeItemCheckboxState.Unchecked) { - unselectedTenants.add(getKeyForTenant(tenantTreeItem)); + unselectedTenantsSet.add(getKeyForTenant(tenantTreeItem.tenantId, tenantTreeItem.account.id)); } else if (state === vscode.TreeItemCheckboxState.Checked) { const treeItem = await tenantTreeItem.getTreeItem(); if (treeItem?.contextValue === 'tenantNameNotSignedIn') { await vscode.commands.executeCommand('azureTenant.signInToTenant', tenantTreeItem, tenantTreeItem.account); ext.actions.refreshTenantTree(); } - unselectedTenants.delete(getKeyForTenant(tenantTreeItem)); + unselectedTenantsSet.delete(getKeyForTenant(tenantTreeItem.tenantId, tenantTreeItem.account.id)); } } - await ext.context.globalState.update('unselectedTenants', Array.from(unselectedTenants)); + await setUnselectedTenants(Array.from(unselectedTenantsSet)); } -export function getKeyForTenant(tenant: { id: string, accountId: string }): string { - return `${tenant.id}/${tenant.accountId}`; +function removeDuplicates(arr: string[]): string[] { + return Array.from(new Set(arr)); +} + +export async function setUnselectedTenants(tenantIds: string[]): Promise { + printTenants(tenantIds); + await ext.context.globalState.update('unselectedTenants', removeDuplicates(tenantIds)); +} + +export function getUnselectedTenants(): string[] { + const value = ext.context.globalState.get('unselectedTenants'); + + if (!value || !Array.isArray(value)) { + return []; + } + + // remove any duplicates + return removeDuplicates(value); +} + +export function isTenantFilteredOut(tenantId: string, accountId: string): boolean { + const settings = ext.context.globalState.get('unselectedTenants'); + if (settings) { + if (settings.includes(getKeyForTenant(tenantId, accountId))) { + return true; + } + } + return false; +} + +export function getKeyForTenant(tenantId: string, accountId: string): string { + return `${tenantId}/${accountId}`; +} + +function printTenants(unselectedTenants: string[]): void { + let str = ''; + str += 'Unselected tenants:\n'; + for (const tenant of unselectedTenants) { + str += `- ${tenant}\n`; + } + ext.outputChannel.appendLine(str); }