Skip to content

Commit d17e205

Browse files
committed
add request client header into tf request for all terminals.
1 parent 9400867 commit d17e205

File tree

7 files changed

+76
-23
lines changed

7 files changed

+76
-23
lines changed

src/client/runner/terraformRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class TerraformRunner extends BaseRunner {
3232
public async init(): Promise<any> {
3333
console.debug("[DEBUG]#### TerraformRunner init begin.");
3434

35-
this.setAKSK();
35+
// this.setAKSK();
3636

3737
terraformShellManager.getShell().runTerraformCmd(TerraformCommand.Version);
3838
terraformShellManager.getShell().runTerraformCmd(TerraformCommand.Init);

src/client/terminal/integratedShell.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ import { BaseRunner } from "../runner/baseRunner";
2323
import { TerraformerRunner, CommandType, FlagType, FlagsMap, defaultProduct } from "../../client/runner/terraformerRunner";
2424
import { TerraformRunner } from "../runner/terraformRunner";
2525

26-
// import stripAnsi from 'strip-ansi';
2726

2827
export class IntegratedShell extends BaseShell {
2928
// eslint-disable-next-line @typescript-eslint/naming-convention
3029
private static readonly GRAPH_FILE_NAME = "graph.png";
3130
private readonly runner: BaseRunner;
31+
private readonly env: { [key: string]: string | null | undefined };
3232

33-
constructor(rr: BaseRunner) {
33+
constructor(rr: BaseRunner, ee: any) {
3434
super();
3535
// terraform or terraformer?
3636
this.runner = rr;
37+
this.env = ee;
3738
}
3839

3940
// Creates a png of terraform resource graph to visualize the resources under management.
@@ -179,7 +180,7 @@ export class IntegratedShell extends BaseShell {
179180
}
180181

181182
public async runTerraformCmd(tfCommand: string, args?: string[]) {
182-
this.checkCreateTerminal();
183+
this.createTerminal(Constants.TerraformTerminalName);
183184
this.terminal.show();
184185

185186
// const cmd= [tfCommand, args.values].join(' ');
@@ -193,7 +194,7 @@ export class IntegratedShell extends BaseShell {
193194
}
194195

195196
public async runNormalCmd(tfCommand: string, newLine = true) {
196-
this.checkCreateTerminal();
197+
this.createTerminal(Constants.TerraformTerminalName);
197198
this.terminal.show();
198199
this.terminal.sendText(tfCommand, newLine);
199200
}
@@ -220,9 +221,13 @@ export class IntegratedShell extends BaseShell {
220221
}
221222
}
222223

223-
private checkCreateTerminal(): void {
224+
private createTerminal(name: string): void {
224225
if (!this.terminal) {
225-
this.terminal = vscode.window.createTerminal(Constants.TerraformTerminalName);
226+
const options: vscode.TerminalOptions = {
227+
name: name,
228+
env: this.env,
229+
};
230+
this.terminal = vscode.window.createTerminal(options);
226231
}
227232
}
228233
}

src/client/terminal/terraformShellManager.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import { TencentCloudShell } from "./cloudShell";
1111
import { IntegratedShell } from "./integratedShell";
1212
import { isTerminalSetToCloudShell } from "../../utils/settingUtils";
1313
import { TerraformerRunner } from "../runner/terraformerRunner";
14+
import * as context from "../../commons/context";
15+
import * as settingUtils from "../../utils/settingUtils";
16+
import { Constants } from "../../commons/constants";
1417

1518
export interface ITerraformShellManager {
1619
getShell(): BaseShell;
@@ -19,6 +22,8 @@ export interface ITerraformShellManager {
1922
dispose(): void;
2023
}
2124

25+
26+
2227
class TerraformShellManager implements ITerraformShellManager {
2328
private static cloudShell = new TencentCloudShell();
2429
private static integratedShell: IntegratedShell;
@@ -39,11 +44,19 @@ class TerraformShellManager implements ITerraformShellManager {
3944

4045
public getIntegratedShell(runner?: any): IntegratedShell {
4146
if (!TerraformShellManager.integratedShell) {
47+
// set TencentCloud AKSK, Region and Client info
48+
const [ak, sk, region] = settingUtils.getAKSKandRegion();
49+
const tfEnv = {
50+
[Constants.REQUEST_CLIENT]: context.genRequestClient(),
51+
[Constants.TENCENTCLOUD_SECRET_ID]: ak,
52+
[Constants.TENCENTCLOUD_SECRET_KEY]: sk,
53+
[Constants.TENCENTCLOUD_REGION]: region,
54+
};
4255
// default runner is Terraformer
43-
TerraformShellManager.integratedShell = new IntegratedShell(TerraformerRunner.getInstance());
56+
TerraformShellManager.integratedShell = new IntegratedShell(TerraformerRunner.getInstance(), tfEnv);
4457
// specify runner
4558
if (runner) {
46-
TerraformShellManager.integratedShell = new IntegratedShell(runner);
59+
TerraformShellManager.integratedShell = new IntegratedShell(runner, tfEnv);
4760
}
4861
}
4962
return TerraformShellManager.integratedShell;

src/commons/constants.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
12
/*---------------------------------------------------------------------------------------------
23
* Copyright (c) Tencent Corporation. All rights reserved.
34
* Licensed under the MIT License. See License.txt in the project root for license information.
@@ -6,6 +7,9 @@
67
"use strict";
78

89
export class Constants {
9-
// eslint-disable-next-line @typescript-eslint/naming-convention
10-
public static TerraformTerminalName = "TIAT-Terraform";
10+
public static TerraformTerminalName = "TIC-Terraform";
11+
public static REQUEST_CLIENT = "TENCENTCLOUD_API_REQUEST_CLIENT";
12+
public static TENCENTCLOUD_SECRET_ID = "TENCENTCLOUD_SECRET_ID";
13+
public static TENCENTCLOUD_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY";
14+
public static TENCENTCLOUD_REGION = "TENCENTCLOUD_REGION";
1115
}

src/commons/context.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@ import { container } from './container';
22
import * as vscode from 'vscode';
33

44
export const Context = Symbol('ExtensionContext');
5+
export const REQUEST_CLIENT_PREFIX = "Terraform-Vscode-";//Terraform-1.81.61@vscode";
56

67
export function bindExtensionContext(ctx: vscode.ExtensionContext) {
78
container.bind(Context).toConstantValue(ctx);
9+
}
10+
11+
export function getExtensionVersion(): string {
12+
let extension = vscode.extensions.getExtension('Tencent-Cloud.vscode-tencentcloud-terraform');
13+
let currentVersion = extension.packageJSON.version;
14+
return currentVersion;
15+
}
16+
17+
export function genRequestClient(): string {
18+
const currentVersion = getExtensionVersion();
19+
const reqCli = `${REQUEST_CLIENT_PREFIX}v${currentVersion}`;
20+
return reqCli;
821
}

src/commons/tencent/user/index.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { localize } from "vscode-nls-i18n";
2-
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation, MessageItem, extensions } from "vscode";
2+
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation, MessageItem } from "vscode";
33

44
import { container } from "../../container";
55
import { Context } from "../../context";
@@ -10,6 +10,7 @@ import { Credential } from "tencentcloud-sdk-nodejs/tencentcloud/common/interfac
1010
import { getCamClient, getCommonClient, getStsClient } from "@/connectivity/client";
1111
import * as loginMgt from "../../../views/login/loginMgt";
1212
import * as settingUtils from "../../../utils/settingUtils";
13+
import * as context from "../../context";
1314

1415
export namespace user {
1516
export interface UserInfo {
@@ -24,7 +25,6 @@ export namespace user {
2425
arn?: string;
2526
}
2627

27-
export const REQUEST_CLIENT_PREFIX = "Terraform-Vscode-";//Terraform-1.81.61@vscode";
2828
export const AKSK_TITLE = "TcTerraform.pickup.aksk";
2929
export const OAUTH_TITLE = "TcTerraform.pickup.oauth";
3030
export const AKSK_PLACEHOLD = "TcTerraform.pickup.aksk.placeholder";
@@ -72,8 +72,7 @@ export namespace user {
7272
try {
7373
// query user info
7474
const stsClient = await getStsClient();
75-
const currentVersion = getExtensionVersion();
76-
const reqCli = `${REQUEST_CLIENT_PREFIX}v${currentVersion}`;
75+
const reqCli = context.genRequestClient();
7776
stsClient.sdkVersion = reqCli;
7877
console.log('[DEBUG]--------------------getStsClient:', stsClient);
7978
// const stsClient = await getCommonClient("sts.tencentcloudapi.com", "2018-08-13");
@@ -132,12 +131,6 @@ export namespace user {
132131
}
133132
}
134133

135-
function getExtensionVersion(): string {
136-
let extension = extensions.getExtension('Tencent-Cloud.vscode-tencentcloud-terraform');
137-
let currentVersion = extension.packageJSON.version;
138-
return currentVersion;
139-
}
140-
141134
export async function loginOut() {
142135
const yesBtn: MessageItem = { title: localize("TcTerraform.common.yes") };
143136
const action = await window.showWarningMessage(

src/extension.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from 'vscode';
4-
import * as settingUtils from "./utils/settingUtils";
54
import { init } from "vscode-nls-i18n";
65
import { TerraformCommand, TerraformerCommand } from "./commons/customCmdRegister";
76
import { terraformShellManager } from "./client/terminal/terraformShellManager";
@@ -16,6 +15,8 @@ import { GitUtils } from './utils/gitUtils';
1615
import _ from 'lodash';
1716
import * as autocomplete from './autocomplete/TerraformExampleProvider';
1817
import * as loginMgt from './views/login/loginMgt';
18+
import * as context from './commons/context';
19+
import { Constants } from './commons/constants';
1920

2021
const TF_MODE: vscode.DocumentFilter = { language: 'terraform', scheme: 'file' };
2122
const COMPATIBLE_MODE: vscode.DocumentFilter = { scheme: 'file' };
@@ -29,6 +30,8 @@ export async function activate(context: vscode.ExtensionContext) {
2930
await TerraformRunner.getInstance().checkInstalled();
3031
await TerraformerRunner.getInstance().checkInstalled();
3132

33+
// set request client
34+
await setRequestClient();
3235
// terraform cmd
3336
context.subscriptions.push(vscode.commands.registerCommand('tcTerraform.apply', () => {
3437
terraformShellManager.getShell().runTerraformCmd(TerraformCommand.Apply);
@@ -115,9 +118,31 @@ export async function activate(context: vscode.ExtensionContext) {
115118

116119
}
117120

121+
async function setRequestClient() {
122+
const config = vscode.workspace.getConfiguration('terminal.integrated.env');
123+
const os = getOSPlatform();
124+
const curConfig = config[os] || {};
118125

126+
// env[Constants.REQUEST_CLIENT] = context.genRequestClient();
127+
const updatedConfig = { ...curConfig, [Constants.REQUEST_CLIENT]: context.genRequestClient() };
128+
129+
await config.update(os, updatedConfig, vscode.ConfigurationTarget.Global);
130+
}
131+
132+
function getOSPlatform(): string {
133+
const platform = process.platform;
134+
switch (platform) {
135+
case 'win32':
136+
return 'windows';
137+
case 'darwin':
138+
return 'osx';
139+
case 'linux':
140+
default:
141+
return 'linux';
142+
}
143+
}
119144

120145
// This method is called when your extension is deactivated
121146
export function deactivate() {
122-
/* TODO document why this function 'deactivate' is empty */
147+
123148
}

0 commit comments

Comments
 (0)