From 273a5ab63f5fba05e8fe19d0e57ffcd09b62988d Mon Sep 17 00:00:00 2001 From: James Pogran Date: Fri, 7 Jun 2024 15:36:33 -0400 Subject: [PATCH] Store the hostname in the secret store This commit stores the hostname in the secret store when the user logs in. This ensures that the hostname is available when the user logs in again. This is necessary because the hostname is used to construct the API URL and the Web URL for a given TFE or HCP Terraform instance. --- src/api/terraformCloud/index.ts | 5 ++++- src/providers/tfc/authenticationProvider.ts | 14 ++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/api/terraformCloud/index.ts b/src/api/terraformCloud/index.ts index 005702229a..34029ebc56 100644 --- a/src/api/terraformCloud/index.ts +++ b/src/api/terraformCloud/index.ts @@ -86,7 +86,10 @@ export function earlySetupForHostname(hostname: string) { earlyApiClient.use(pluginLogger()); } -export function apiSetup() { +export function apiSetup(hostname: string) { + TerraformCloudHost = hostname; + TerraformCloudAPIUrl = `https://${TerraformCloudHost}/api/v2`; + TerraformCloudWebUrl = `https://${TerraformCloudHost}/app`; // ApiClient setup apiClient = new Zodios(TerraformCloudAPIUrl, [ ...accountEndpoints, diff --git a/src/providers/tfc/authenticationProvider.ts b/src/providers/tfc/authenticationProvider.ts index 288f8a181b..99e334220d 100644 --- a/src/providers/tfc/authenticationProvider.ts +++ b/src/providers/tfc/authenticationProvider.ts @@ -31,7 +31,11 @@ class TerraformCloudSession implements vscode.AuthenticationSession { * @param accessToken The personal access token to use for authentication * @param account The user account for the specified token */ - constructor(public readonly accessToken: string, public account: vscode.AuthenticationSessionAccountInformation) {} + constructor( + public readonly accessToken: string, + public readonly hostName: string, + public account: vscode.AuthenticationSessionAccountInformation, + ) {} } interface TerraformCloudToken { @@ -69,13 +73,13 @@ class TerraformCloudSessionHandler { }, }); - const session = new TerraformCloudSession(token, { + const session = new TerraformCloudSession(token, hostname, { label: user.data.attributes.username, id: user.data.id, }); await this.secretStorage.store(this.sessionKey, JSON.stringify(session)); - apiSetup(); + apiSetup(session.hostName); return session; } catch (error) { if (error instanceof ZodiosError) { @@ -111,7 +115,7 @@ export class TerraformCloudAuthenticationProvider implements vscode.Authenticati private sessionHandler: TerraformCloudSessionHandler; // this property is used to determine if the session has been changed in another window of VS Code // it's a promise, so we can set in the constructor where we can't execute async code - private sessionPromise: Promise; + private sessionPromise: Promise; private disposable: vscode.Disposable | undefined; private _onDidChangeSessions = @@ -160,6 +164,8 @@ export class TerraformCloudAuthenticationProvider implements vscode.Authenticati 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];