Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup unselected tenant logic #963

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}