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

Add feature to scope select subscriptions prompt by tenant #934

Merged
merged 4 commits into from
Oct 3, 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
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
Loading