Skip to content

Commit

Permalink
support: set aksk and terraform init/plan/apply/destroy cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
lyu571 committed Oct 20, 2023
1 parent 59b0849 commit 0b4f4fe
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 56 deletions.
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@
"onCommand:tcTerraform.init",
"onCommand:tcTerraform.plan",
"onCommand:tcTerraform.apply",
"onCommand:tcTerraform.import",
"onCommand:tcTerraform.validate",
"onCommand:tcTerraform.refresh",
"onCommand:tcTerraform.destroy",
"onCommand:tcTerraform.visualize",
"onCommand:tcTerraform.test",
"onCommand:tcTerraform.push",
"onCommand:tcTerraformer.import",
"onCommand:tcTerraformer.plan",
"workspaceContains:**/*.tf",
"onLanguage:terraform"
],
Expand Down Expand Up @@ -120,7 +114,8 @@
"commands": [
{
"command": "tcTerraform.login",
"title": "%TcTerraform.view.login.welcome%"
"title": "Login: %TcTerraform.view.login.welcome%",
"category": "TencentCloud Terraform"
},
{
"command": "tcTerraform.resourcesExplorer.refresh",
Expand Down
2 changes: 1 addition & 1 deletion src/client/runner/baseRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export abstract class BaseRunner {
public tfExecutor: any;

constructor() {
this.init();
// this.init();
}

public abstract init(): void;
Expand Down
27 changes: 17 additions & 10 deletions src/client/runner/terraformRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@ export class TerraformRunner extends BaseRunner {
return TerraformRunner.ins;
}

public init(): void {
// throw new Error("Method not implemented.");
public async init(): Promise<any> {
console.debug("[DEBUG]#### TerraformRunner init begin.");

this.setAKSK();

terraformShellManager.getShell().runTerraformCmd(TerraformCommand.Init);

return "init success";
}

public async executePlan(cwd: string, args: any): Promise<string> {
console.debug("[DEBUG]#### TerraformRunner executePlan begin.");

const resAddress = `${args.resource.type}.${args.resource.name}`;
// this.setAKSK();

// reset state
await this.resetTFState(resAddress);
terraformShellManager.getShell().runTerraformCmd(TerraformCommand.Plan);

terraformShellManager.getIntegratedShell(TerraformRunner.getInstance()).runTerraformCmd(TerraformCommand.Plan);

return "";
return "plan success";
}

public async executeShow(cwd: string, args?: any): Promise<string> {
Expand Down Expand Up @@ -125,6 +128,12 @@ export class TerraformRunner extends BaseRunner {
await terraformShellManager.getIntegratedShell(TerraformRunner.getInstance())
.runTerraformCmd(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
}

private setAKSK(runner?: any) {
const [ak, sk] = settingUtils.getAKSK();
terraformShellManager.getIntegratedShell(runner).runNormalCmd("export TENCENTCLOUD_SECRET_ID=" + ak);
terraformShellManager.getIntegratedShell(runner).runNormalCmd("export TENCENTCLOUD_SECRET_KEY=" + sk);
}
}

export function getCheckTerraformCmd(): boolean {
Expand All @@ -134,5 +143,3 @@ export function getCheckTerraformCmd(): boolean {
export function setCheckTerraformCmd(checked: boolean): void {
vscode.workspace.getConfiguration().update("tcTerraform.checkTerraformCmd", checked);
}


18 changes: 18 additions & 0 deletions src/client/terminal/integratedShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ export class IntegratedShell extends BaseShell {
await commands.executeCommand("vscode.open", Uri.file(tfFile), ViewColumn.Active || ViewColumn.One);
}

// run terraform init command
public async init(params?: any): Promise<void> {
console.debug("[DEBUG]#### IntegratedShell init begin. params:[%v]", params);

await this.runner.checkInstalled();
if (this.runner instanceof TerraformRunner) {

const cwd: string = await selectWorkspaceFolder();
if (!cwd) {
TelemetryWrapper.sendError(Error("noWorkspaceSelected"));
return;
}

const result = await this.runner.init();
console.debug("[DEBUG]#### Executed init. result:[%s]", result);
}
}

// run terraform plan command
public async plan(params: any): Promise<void> {
console.debug("[DEBUG]#### IntegratedShell plan begin. params:[%v]", params);
Expand Down
11 changes: 5 additions & 6 deletions src/client/terminal/terraformShellManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class TerraformShellManager implements ITerraformShellManager {

TelemetryWrapper.addContextProperty("isCloudShell", isCloudShell.toString());
if (isCloudShell) {
return TerraformShellManager.cloudShell;
return this.getCloudShell();
}
return TerraformShellManager.integratedShell;
return this.getIntegratedShell();
}

public getCloudShell(): TencentCloudShell {
Expand All @@ -39,14 +39,13 @@ class TerraformShellManager implements ITerraformShellManager {

public getIntegratedShell(runner?: any): IntegratedShell {
if (!TerraformShellManager.integratedShell) {
// default runner is Terraformer
TerraformShellManager.integratedShell = new IntegratedShell(TerraformerRunner.getInstance());
// specify runner
if (runner) {
TerraformShellManager.integratedShell = new IntegratedShell(runner);
} else {
// default runner is Terraformer
TerraformShellManager.integratedShell = new IntegratedShell(TerraformerRunner.getInstance());
}
}

return TerraformShellManager.integratedShell;
}

Expand Down
4 changes: 4 additions & 0 deletions src/commons/customCmdRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export function regResourceRelatedCommands() {
terraformShellManager.getIntegratedShell(TerraformerRunner.getInstance()).import(param, param.fileName);
});

commands.registerCommand("tcTerraform.init", function (param: any) {
terraformShellManager.getIntegratedShell(TerraformRunner.getInstance()).init();
});

commands.registerCommand("tcTerraform.plan", function (param: any) {
terraformShellManager.getIntegratedShell(TerraformRunner.getInstance()).plan(param);
});
Expand Down
3 changes: 0 additions & 3 deletions src/commons/tencent/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ export namespace user {
process.env.TENCENTCLOUD_SECRET_ID = accessKey;
process.env.TENCENTCLOUD_SECRET_KEY = secretKey;

terraformShellManager.getShell().runNormalCmd("export TENCENTCLOUD_SECRET_ID=" + accessKey);
terraformShellManager.getShell().runNormalCmd("export TENCENTCLOUD_SECRET_KEY=" + secretKey);

tree.refreshTreeData();
window.showInformationMessage(localize("TcTerraform.login.success"));
}
Expand Down
44 changes: 37 additions & 7 deletions src/connectivity/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as vscode from "vscode";
import { Client as CvmClient } from "tencentcloud-sdk-nodejs-cvm/tencentcloud/services/cvm/v20170312/cvm_client";
import { Client as TkeClient } from "tencentcloud-sdk-nodejs-tke/tencentcloud/services/tke/v20180525/tke_client";
import { localize } from "vscode-nls-i18n";
import * as utils from "../utils/settingUtils";
import * as settingUtils from "../utils/settingUtils";

const tkeClient = TkeClient;
const cvmClient = CvmClient;
Expand Down Expand Up @@ -36,13 +36,43 @@ export async function getTkeClient(): Promise<TkeClient> {
}

export async function getCvmClient(region?: string): Promise<CvmClient> {
const secretIdConfig = utils.getSecretIdFromUI();
const secretKeyConfig = utils.getSecretKeyFromUI();
const secretIdEnv = utils.getSecretIdFromEnv();
const secretKeyEnv = utils.getSecretKeyFromEnv();
// const secretIdConfig = utils.getSecretIdFromUI();
// const secretKeyConfig = utils.getSecretKeyFromUI();
// const secretIdEnv = utils.getSecretIdFromEnv();
// const secretKeyEnv = utils.getSecretKeyFromEnv();

const secretId = (secretIdEnv === undefined) ? secretIdConfig : secretIdEnv;
const secretKey = (secretKeyEnv === undefined) ? secretKeyConfig : secretKeyEnv;
// const secretId = (secretIdEnv === undefined) ? secretIdConfig : secretIdEnv;
// const secretKey = (secretKeyEnv === undefined) ? secretKeyConfig : secretKeyEnv;
const [secretId, secretKey] = settingUtils.getAKSK();

if (secretId === undefined || secretKey === undefined || secretId === null || secretKey === null) {
let msg = localize("TcTerraform.msg.aksk.notfound");
vscode.window.showErrorMessage(msg);
return null;
}

return new CvmClient({
credential: {
secretId: secretId,
secretKey: secretKey,
},
// 产品地域
region: (process.env.TENCENTCLOUD_REGION === undefined) ?
"ap-guangzhou" : process.env.TENCENTCLOUD_REGION,
// 可选配置实例
profile: {
// signMethod: "TC3-HMAC-SHA256", // 签名方法
httpProfile: {
reqMethod: "POST", // 请求方法
// reqTimeout: 60, // 请求超时时间,默认60s
endpoint: "cvm.tencentcloudapi.com",
},
},
})
}

export async function getStsClient(region?: string): Promise<CvmClient> {
const [secretId, secretKey] = settingUtils.getAKSK();

if (secretId === undefined || secretKey === undefined || secretId === null || secretKey === null) {
let msg = localize("TcTerraform.msg.aksk.notfound");
Expand Down
22 changes: 0 additions & 22 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,7 @@ export async function activate(context: vscode.ExtensionContext) {
await TerraformRunner.getInstance().checkInstalled();
await TerraformerRunner.getInstance().checkInstalled();

// let disposableLogin = vscode.commands.registerCommand('tcTerraform.login', async () => {
// // to-do
// // wait for cloudshell and tccli implement ready
// let accessKey = settingUtils.getSecretIdFromUI();
// let secretKey = settingUtils.getSecretKeyFromUI();

// terraformShellManager.getShell().runNormalCmd("export TENCENTCLOUD_SECRET_ID=" + accessKey);
// terraformShellManager.getShell().runNormalCmd("export TENCENTCLOUD_SECRET_KEY=" + secretKey);
// });

// context.subscriptions.push(disposableLogin);

// terraform cmd
context.subscriptions.push(vscode.commands.registerCommand('tcTerraform.init', () => {
terraformShellManager.getShell().runTerraformCmd(TerraformCommand.Init);
}));

// move plan to customCmdRegister
// context.subscriptions.push(vscode.commands.registerCommand('tcTerraform.plan', () => {
// await terraformShellManager.getIntegratedShell(TerraformRunner.getInstance()).plan();

// }));

context.subscriptions.push(vscode.commands.registerCommand('tcTerraform.apply', () => {
terraformShellManager.getShell().runTerraformCmd(TerraformCommand.Apply);
}));
Expand Down
9 changes: 9 additions & 0 deletions src/utils/settingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export function getSecretKeyFromUI(): string {
return vscode.workspace.getConfiguration().get("tcTerraform.properties.secretKey");
}

export function getAKSK(): [string, string] {
const secretIdConfig = getSecretIdFromUI();
const secretKeyConfig = getSecretKeyFromUI();
const secretIdEnv = getSecretIdFromEnv();
const secretKeyEnv = getSecretKeyFromEnv();

return [secretIdEnv ?? secretIdConfig, secretKeyEnv ?? secretKeyConfig];
}

export function getSecretIdFromEnv(): string {
return process.env.TENCENTCLOUD_SECRET_ID;
}
Expand Down

0 comments on commit 0b4f4fe

Please sign in to comment.