Skip to content

Commit

Permalink
Fixup unselected tenant logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweininger committed Nov 26, 2024
1 parent 380ded2 commit 15f7fb8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
12 changes: 3 additions & 9 deletions src/commands/accounts/selectSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<string[]>('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[] {
Expand Down
18 changes: 4 additions & 14 deletions src/tree/tenants/TenantResourceTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ 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';
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;
Expand Down Expand Up @@ -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
}));
}

Expand All @@ -75,14 +75,4 @@ export class TenantResourceTreeDataProvider extends ResourceTreeDataProviderBase
return children;
}
}

private checkUnselectedTenants(tenantId: string): boolean {
const settings = ext.context.globalState.get<string[]>('unselectedTenants');
if (settings) {
if (settings.includes(tenantId)) {
return true;
}
}
return false;
}
}
53 changes: 46 additions & 7 deletions src/tree/tenants/registerTenantTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,64 @@ export function registerTenantTree(context: vscode.ExtensionContext, options: Re
}

async function updateTenantsSetting(_context: IActionContext, tenants: vscode.TreeCheckboxChangeEvent<TenantTreeItem>) {
const state = ext.context.globalState.get<string[]>('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<void> {
printTenants(tenantIds);
await ext.context.globalState.update('unselectedTenants', removeDuplicates(tenantIds));
}

export function getUnselectedTenants(): string[] {
const value = ext.context.globalState.get<string[]>('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<string[]>('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);
}

0 comments on commit 15f7fb8

Please sign in to comment.