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

auth: Create AzureTenant interface which includes account #1849

Merged
merged 1 commit into from
Dec 6, 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
9 changes: 7 additions & 2 deletions auth/src/AzureDevOpsSubscriptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { SubscriptionClient, TenantIdDescription } from '@azure/arm-resources-subscriptions';
import type { SubscriptionClient } from '@azure/arm-resources-subscriptions';
import type { TokenCredential } from '@azure/core-auth'; // Keep this as `import type` to avoid actually loading the package (at all, this one is dev-only)
import type { PipelineRequest } from '@azure/core-rest-pipeline';
import { Disposable, Event } from 'vscode';
import { AzureAuthentication } from './AzureAuthentication';
import { AzureSubscription } from './AzureSubscription';
import { AzureSubscriptionProvider } from './AzureSubscriptionProvider';
import { getConfiguredAzureEnv } from './utils/configuredAzureEnv';
import { AzureTenant } from './AzureTenant';

export interface AzureDevOpsSubscriptionProviderInitializer {
/**
Expand Down Expand Up @@ -102,9 +103,13 @@ export class AzureDevOpsSubscriptionProvider implements AzureSubscriptionProvide
this._tokenCredential = undefined;
}

public async getTenants(): Promise<TenantIdDescription[]> {
public async getTenants(): Promise<AzureTenant[]> {
return [{
tenantId: this._tokenCredential?.tenantId,
account: {
id: "test-account-id",
label: "test-account",
}
}];
}

Expand Down
4 changes: 2 additions & 2 deletions auth/src/AzureSubscriptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { TenantIdDescription } from '@azure/arm-resources-subscriptions';
import type * as vscode from 'vscode';
import type { AzureSubscription } from './AzureSubscription';
import type { AzureTenant } from './AzureTenant';

/**
* An interface for obtaining Azure subscription information
Expand All @@ -19,7 +19,7 @@ export interface AzureSubscriptionProvider {
*
* @returns A list of tenants.
*/
getTenants(account?: vscode.AuthenticationSessionAccountInformation): Promise<TenantIdDescription[]>;
getTenants(account?: vscode.AuthenticationSessionAccountInformation): Promise<AzureTenant[]>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically a breaking change, but it's harmless. The break would be a compile error to anyone implementing this interface. Bump to 4.0.0 or to 3.2.0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add some more changes and figure it out later


/**
* Gets a list of Azure subscriptions available to the user.
Expand Down
11 changes: 11 additions & 0 deletions auth/src/AzureTenant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TenantIdDescription } from "@azure/arm-resources-subscriptions";
import * as vscode from 'vscode';

export interface AzureTenant extends TenantIdDescription {
account: vscode.AuthenticationSessionAccountInformation;
}
10 changes: 5 additions & 5 deletions auth/src/VSCodeAzureSubscriptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { SubscriptionClient, TenantIdDescription } from '@azure/arm-resources-subscriptions'; // Keep this as `import type` to avoid actually loading the package before necessary
import type { SubscriptionClient } from '@azure/arm-resources-subscriptions'; // Keep this as `import type` to avoid actually loading the package before necessary
import type { TokenCredential } from '@azure/core-auth'; // Keep this as `import type` to avoid actually loading the package (at all, this one is dev-only)
import * as vscode from 'vscode';
import { AzureAuthentication } from './AzureAuthentication';
Expand All @@ -12,6 +12,7 @@ import { AzureSubscriptionProvider } from './AzureSubscriptionProvider';
import { getSessionFromVSCode } from './getSessionFromVSCode';
import { NotSignedInError } from './NotSignedInError';
import { getConfiguredAuthProviderId, getConfiguredAzureEnv } from './utils/configuredAzureEnv';
import { AzureTenant } from './AzureTenant';

const EventDebounce = 5 * 1000; // 5 seconds

Expand Down Expand Up @@ -60,17 +61,16 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
*
* @returns A list of tenants.
*/
public async getTenants(account?: vscode.AuthenticationSessionAccountInformation): Promise<TenantIdDescription[]> {
const results: TenantIdDescription[] = [];

public async getTenants(account?: vscode.AuthenticationSessionAccountInformation): Promise<AzureTenant[]> {
const results: AzureTenant[] = [];
for await (account of account ? [account] : await vscode.authentication.getAccounts(getConfiguredAuthProviderId())) {
// Added check. Without this the getSubscriptionClient function throws the NotSignedInError
if (await this.isSignedIn(undefined, account)) {

const { client } = await this.getSubscriptionClient(account, undefined, undefined);

for await (const tenant of client.tenants.list()) {
results.push(tenant);
results.push({ ...tenant, account });
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

export * from './AzureAuthentication';
export * from './AzureDevOpsSubscriptionProvider';
export * from './AzureTenant';
export * from './AzureSubscription';
export * from './AzureSubscriptionProvider';
export * from './NotSignedInError';
export * from './signInToTenant';
export * from './utils/configuredAzureEnv';
export * from './utils/getUnauthenticatedTenants';
export * from './VSCodeAzureSubscriptionProvider';

Loading