Skip to content

Commit

Permalink
Handle valid sessions without hostname
Browse files Browse the repository at this point in the history
This handles sessions created before this version without a hostname to connect to. This will set the hostname to the default Terraform Cloud API URL if it is undefined.
  • Loading branch information
jpogran committed Jun 20, 2024
1 parent 0137d57 commit 4a7a40c
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/providers/tfc/authenticationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
earlySetupForHostname,
pingClient,
setupPingClient,
TerraformCloudAPIUrl,

Check warning on line 16 in src/providers/tfc/authenticationProvider.ts

View workflow job for this annotation

GitHub Actions / lint

'TerraformCloudAPIUrl' is defined but never used
TerraformCloudHost,
TerraformCloudWebUrl,
} from '../../api/terraformCloud';
Expand Down Expand Up @@ -167,22 +168,38 @@ export class TerraformCloudAuthenticationProvider implements vscode.Authenticati
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getSessions(scopes?: string[] | undefined): Promise<readonly vscode.AuthenticationSession[]> {
try {
const session = await this.sessionPromise;
if (session) {
earlySetupForHostname(session.hostName);
apiSetup(session.hostName);
this.logger.info('Successfully fetched HCP Terraform session');
await vscode.commands.executeCommand('setContext', 'terraform.cloud.signed-in', true);
return [session];
} else {
let session = await this.sessionPromise;
if (!session) {
// no session stored, create a new one
return [];
}

if (session.hostName === '' || session.hostName === undefined) {
// we have a valid session but the hostname is not set
// this is most likely an old session that needs to be updated
// if hostname is undefined, we need to set it to the default
session = await this.sessionHandler.store(TerraformCloudHost, session.accessToken);
}

// setup the API client for getting the user info
earlySetupForHostname(session.hostName);
// setup the API client for the session
apiSetup(session.hostName);

this.logger.info('Successfully fetched HCP Terraform session');
await vscode.commands.executeCommand('setContext', 'terraform.cloud.signed-in', true);

return [session];
} catch (error) {
let message = 'Failed to get HCP Terraform session';
if (error instanceof Error) {
vscode.window.showErrorMessage(error.message);
message = error.message;
} else if (typeof error === 'string') {
vscode.window.showErrorMessage(error);
message = error;
}

this.logger.info(message);
vscode.window.showErrorMessage(message);
return [];
}
}
Expand Down

0 comments on commit 4a7a40c

Please sign in to comment.