Skip to content

Commit

Permalink
Add feature to scope select subscriptions prompt by tenant (#934)
Browse files Browse the repository at this point in the history
* Add feature to scope select subscriptions prompt by tenant

* Fixup

* Fixup

* Fixup
  • Loading branch information
alexweininger authored Oct 3, 2024
1 parent 2bda73d commit d526677
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
40 changes: 31 additions & 9 deletions src/commands/accounts/selectSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,33 @@ import { ext } from "../../extensionVariables";
import { localize } from "../../utils/localize";
import { settingUtils } from "../../utils/settingUtils";

export async function selectSubscriptions(context: IActionContext): Promise<void> {
export interface SelectSubscriptionOptions {
/**
* If provided, only subscriptions in this tenant will be shown in the picker. Only subscriptions shown in the picker will be removed or added to the selected subscriptions setting.
*/
tenantId?: string;
/**
* TODO: implement filtering at the account level
*/
account?: vscode.AuthenticationSessionAccountInformation;
}

export async function selectSubscriptions(context: IActionContext, options?: SelectSubscriptionOptions): Promise<void> {
const provider = await ext.subscriptionProviderFactory();
if (await provider.isSignedIn()) {

const selectedSubscriptionIds = await getSelectedSubscriptionIds();
const selectedSubscriptionsWithFullId = await getSelectedTenantAndSubscriptionIds();
const selectedSubscriptionIds = selectedSubscriptionsWithFullId.map(id => id.split('/')[1]);

let subscriptionsShownInPicker: string[] = [];

const subscriptionQuickPickItems: () => Promise<IAzureQuickPickItem<AzureSubscription>[]> = async () => {

const allSubscriptions = await provider.getSubscriptions(false);
const subscriptionsFilteredByTenant = options?.tenantId ? allSubscriptions.filter(subscription => subscription.tenantId === options.tenantId) : allSubscriptions;

return allSubscriptions
subscriptionsShownInPicker = subscriptionsFilteredByTenant.map(sub => `${sub.tenantId}/${sub.subscriptionId}`);
return subscriptionsFilteredByTenant
.map(subscription => ({
label: subscription.name,
description: subscription.subscriptionId,
Expand All @@ -39,7 +56,17 @@ export async function selectSubscriptions(context: IActionContext): Promise<void
});

if (picks) {
await setSelectedTenantAndSubscriptionIds(picks.map(s => `${s.data.tenantId}/${s.data.subscriptionId}`));
// get previously selected subscriptions
const previouslySelectedSubscriptionsSettingValue = new Set(selectedSubscriptionsWithFullId);

// remove any that were shown in the picker
subscriptionsShownInPicker.forEach(pick => previouslySelectedSubscriptionsSettingValue.delete(pick));

// add any that were selected in the picker
picks.forEach(pick => previouslySelectedSubscriptionsSettingValue.add(`${pick.data.tenantId}/${pick.data.subscriptionId}`));

// update the setting
await setSelectedTenantAndSubscriptionIds(Array.from(previouslySelectedSubscriptionsSettingValue));
}

ext.actions.refreshAzureTree();
Expand All @@ -53,11 +80,6 @@ export async function selectSubscriptions(context: IActionContext): Promise<void
}
}

async function getSelectedSubscriptionIds(): Promise<string[]> {
const selectedTenantAndSubscriptionIds = await getSelectedTenantAndSubscriptionIds();
return selectedTenantAndSubscriptionIds.map(id => id.split('/')[1]);
}

export async function getSelectedTenantAndSubscriptionIds(): Promise<string[]> {
// clear setting value if there's a value that doesn't include the tenant id
// see https://github.com/microsoft/vscode-azureresourcegroups/pull/684
Expand Down
4 changes: 2 additions & 2 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { BranchDataItemWrapper } from '../tree/BranchDataItemWrapper';
import { ResourceGroupsItem } from '../tree/ResourceGroupsItem';
import { GroupingItem } from '../tree/azure/grouping/GroupingItem';
import { logIn } from './accounts/logIn';
import { selectSubscriptions } from './accounts/selectSubscriptions';
import { SelectSubscriptionOptions, selectSubscriptions } from './accounts/selectSubscriptions';
import { clearActivities } from './activities/clearActivities';
import { maintainCloudShellConnection } from './cloudShell';
import { createResource } from './createResource';
Expand Down Expand Up @@ -67,7 +67,7 @@ export function registerCommands(): void {
registerCommand('azureResourceGroups.unfocusGroup', unfocusGroup);

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

registerCommand('azureResourceGroups.createResourceGroup', createResourceGroup);
Expand Down

0 comments on commit d526677

Please sign in to comment.