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

perf: store selected tenant #213

Merged
merged 3 commits into from
Jul 25, 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
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@
"items": {
"type": "string"
}
},
"azure-api-center.tenant": {
"type": "object",
"description": "A specific tenant to sign in to",
"properties": {
"name": {
"type": "string",
"description": "tenant name"
},
"id": {
"type": "string",
"description": "tenant id"
}
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/azure/azureLogin/azureAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Licensed under the MIT license.
import { SubscriptionClient, TenantIdDescription } from "@azure/arm-resources-subscriptions";
import { TokenCredential } from "@azure/core-auth";
import { AuthenticationSession, QuickPickItem, Uri, env, window } from "vscode";
import { AuthenticationSession, ConfigurationTarget, QuickPickItem, Uri, env, window, workspace } from "vscode";
import { extensionName, tenantSetting } from "../../constants";
import { UiStrings } from "../../uiStrings";
import { GeneralUtils } from "../../utils/generalUtils";
import { SelectionType, SignInStatus, SubscriptionFilter, Tenant } from "./authTypes";
Expand All @@ -14,6 +15,14 @@
await AzureSessionProviderHelper.getSessionProvider().signIn();
}

export function getSelectedTenant(): Tenant | undefined {
return workspace.getConfiguration(extensionName).get<Tenant>(tenantSetting);
}

export async function updateSelectedTenant(value?: Tenant): Promise<void> {
await workspace.getConfiguration(extensionName).update(tenantSetting, value, ConfigurationTarget.Global, true);
}

Check warning on line 24 in src/azure/azureLogin/azureAccount.ts

View check run for this annotation

Codecov / codecov/patch

src/azure/azureLogin/azureAccount.ts#L23-L24

Added lines #L23 - L24 were not covered by tests

export async function selectTenant(): Promise<void> {
const sessionProvider = AzureSessionProviderHelper.getSessionProvider();
if (sessionProvider.signInStatus !== SignInStatus.SignedIn) {
Expand Down Expand Up @@ -42,6 +51,7 @@
}

sessionProvider.selectedTenant = selectedTenant;
await updateSelectedTenant(selectedTenant);

Check warning on line 54 in src/azure/azureLogin/azureAccount.ts

View check run for this annotation

Codecov / codecov/patch

src/azure/azureLogin/azureAccount.ts#L54

Added line #L54 was not covered by tests
}

type SubscriptionQuickPickItem = QuickPickItem & { subscription: SubscriptionFilter };
Expand Down
9 changes: 8 additions & 1 deletion src/azure/azureLogin/azureSessionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
this.tenants = newTenants;
this.signInStatusValue = newSignInStatus;
if (signInStatusChanged || tenantsChanged || selectedTenantChanged) {
if (newSignInStatus === SignInStatus.SignedOut) {
await AzureAccount.updateSelectedTenant();
}

Check warning on line 148 in src/azure/azureLogin/azureSessionProvider.ts

View check run for this annotation

Codecov / codecov/patch

src/azure/azureLogin/azureSessionProvider.ts#L146-L148

Added lines #L146 - L148 were not covered by tests
this.onSignInStatusChangeEmitter.fire(this.signInStatusValue);
}
}
Expand Down Expand Up @@ -220,7 +223,11 @@
);
const results = await Promise.all(getSessionPromises);
const accessibleTenants = results.filter(GeneralUtils.succeeded).map((r) => r.result);
return accessibleTenants.length === 1 ? AzureAccount.findTenant(tenants, accessibleTenants[0].tenantId) : null;
if (accessibleTenants.length === 1) {
return AzureAccount.findTenant(tenants, accessibleTenants[0].tenantId);
}
const lastTenant = AzureAccount.getSelectedTenant();
return lastTenant && accessibleTenants.some(item => item.tenantId === lastTenant.id) ? lastTenant : null;

Check warning on line 230 in src/azure/azureLogin/azureSessionProvider.ts

View check run for this annotation

Codecov / codecov/patch

src/azure/azureLogin/azureSessionProvider.ts#L227-L230

Added lines #L227 - L230 were not covered by tests
}

private async getArmSession(
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const azureApiGuidelineRulesetFile = "https://raw.githubusercontent.com/a
export const spectralOwaspRulesetFile = "https://unpkg.com/@stoplight/spectral-owasp-ruleset/dist/ruleset.mjs";
export const MODEL_SELECTOR: vscode.LanguageModelChatSelector = { vendor: 'copilot', family: 'gpt-4' };
export const ExceedTokenLimit = "Message exceeds token limit";
export const tenantSetting: string = 'tenant';

export const AzureAccountType = {
createAzureAccount: "azureapicenterCreateAzureAccount",
Expand Down
26 changes: 26 additions & 0 deletions src/test/unit/azure/azureLogin/azureAccount.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as assert from "assert";
import * as sinon from "sinon";
import * as vscode from "vscode";
import { AzureAccount } from "../../../../azure/azureLogin/azureAccount";

describe("Azure Account test case", () => {
let sandbox = null as any;
before(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
it("getSelectedTenant happy path", async () => {
let spyConf = sandbox.stub(vscode.workspace, "getConfiguration").returns({
get: () => {
return "test";
},
} as any);
let res = await AzureAccount.getSelectedTenant();
sandbox.assert.calledOnce(spyConf);
assert.strictEqual(res, "test");
});
});
Loading