Skip to content

Commit 98add33

Browse files
committed
1.enable the terraformer. 2.adjust code format.
1 parent 064f3d2 commit 98add33

23 files changed

+537
-161
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ This extension supports the following features:
88

99
- Terraform commands: init, plan, apply, validate, refresh and destroy.
1010
- Auto complete: Autocomplete resource types, parameters, and resource definitions.
11-
- Import resource: display the existing `CVM` resource and then import it as a tf file by `terraform import`.
11+
- Import resource: display the existing `CVM` resource and then import it as a tf file by [Terraformer](https://github.com/GoogleCloudPlatform/terraformer).
1212

1313
*TO-DO(Features to be supported in the future):*
1414
- Visualize: graph the terraform resources and modules.
15-
- Resource Import: support import of more kinds of resources by [Terraformer](https://github.com/GoogleCloudPlatform/terraformer).
1615
- Autocomplete: provider code snippets of the specified resource.
1716
- Connect to Tencent Cloud: login to Tencent Cloud and sync your account info(eg: obtain AKSK/Token automatically).
1817

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-tencentcloud-terraform",
33
"displayName": "Tencent Cloud Terraform",
44
"description": "VS Code extension for developing with Terraform on Tencent Cloud",
5-
"version": "0.0.5",
5+
"version": "0.0.6",
66
"license": "MIT",
77
"publisher": "Tencent-Cloud",
88
"icon": "images/tc-tf-logo.png",
@@ -150,9 +150,10 @@
150150
"category": "TencentCloud Terraform"
151151
},
152152
{
153-
"command": "tcTerraform.push",
154-
"title": "Push",
155-
"category": "TencentCloud Terraform"
153+
"command": "tcTerraform.git.push",
154+
"title": "Push to git",
155+
"category": "TencentCloud Terraform",
156+
"shortTitle": "Push"
156157
},
157158
{
158159
"command": "tcTerraformer.import",

src/utils/baseRunner.ts renamed to src/client/runner/baseRunner.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,34 @@ export abstract class BaseRunner {
1818
* @param cwd
1919
* @returns
2020
*/
21-
public abstract preImport(cwd: string, args?: any, path?: string): Promise<any>;
21+
public abstract preImport(cwd: string, args?: any, file?: string): Promise<any>;
2222

2323
/**
2424
* execute this command to import the existing resource from tencentcloud
2525
* @param cwd
2626
* @param args
2727
* @returns
2828
*/
29-
public abstract executeImport(cwd: string, args?: string): Promise<any>;
29+
public abstract executeImport(cwd: string, args?: any, cmd?: any, flags?: any): Promise<any>;
3030

3131
/**
3232
* execute this command to handle post of the terraform import.
3333
* @param cwd
3434
* @param executor Choose who will execute this command? terraform or terraformer
35+
* @param cmd
36+
* @param flags
3537
* @returns
3638
*/
37-
public abstract postImport(cwd: string, executor?:string, args?: string): Promise<any>;
39+
public abstract postImport(cwd: string, executor?: string, args?: string): Promise<any>;
40+
41+
/**
42+
* execute this command to plan the tf code
43+
* @param cwd
44+
* @param args
45+
* @param cmd
46+
* @param flags
47+
*/
48+
public abstract executePlan(cwd: string, args?: any, cmd?: any, flags?: any): Promise<any>;
3849

3950
/**
4051
* check binary whether ready or not
@@ -46,5 +57,5 @@ export abstract class BaseRunner {
4657
* @param cwd
4758
* @returns
4859
*/
49-
public abstract executeShow(cwd: string, args?: string): Promise<any>;
60+
public abstract executeShow(cwd: string, args?: any): Promise<any>;
5061
}

src/utils/terraformRunner.ts renamed to src/client/runner/terraformRunner.ts

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import * as path from "path";
88
import * as fse from "fs-extra";
99
import * as vscode from "vscode";
10-
import { executeCommand } from "./cpUtils";
1110
import { BaseRunner } from "./baseRunner";
12-
import { TerraformCommand } from "../commons/commands";
13-
import { terraformShellManager } from "../terraformShellManager";
14-
import * as settingUtils from "./settingUtils";
15-
import { openUrlHintOrNotShowAgain } from "./uiUtils";
11+
import { TerraformCommand } from "../../commons/customCmdRegister";
12+
import { terraformShellManager } from "../terminal/terraformShellManager";
13+
import { executeCommand } from "../../utils/cpUtils";
14+
import * as settingUtils from "../../utils/settingUtils";
15+
import { openUrlHintOrNotShowAgain } from "../../utils/uiUtils";
1616

1717

1818
export class TerraformRunner extends BaseRunner {
@@ -33,33 +33,35 @@ export class TerraformRunner extends BaseRunner {
3333
// throw new Error("Method not implemented.");
3434
}
3535

36-
public async executeShow(cwd: string, args?: string): Promise<string> {
37-
return await executeCommand(
38-
"terraform",
39-
["show"],
40-
{
41-
shell: true,
42-
cwd,
43-
}
44-
);
36+
public async executePlan(cwd: string, args: any): Promise<string> {
37+
console.debug("[DEBUG]#### TerraformRunner executePlan begin.");
38+
39+
const resAddress = `${args.resource.type}.${args.resource.name}`;
40+
41+
// reset state
42+
await this.resetTFState(resAddress);
43+
44+
terraformShellManager.getIntegratedShell(TerraformRunner.getInstance()).runTerraformCmd(TerraformCommand.Plan);
45+
46+
return "";
4547
}
4648

47-
public async executeImport(cwd: string, args?: string): Promise<string> {
49+
public async executeShow(cwd: string, args?: any): Promise<string> {
4850
return await executeCommand(
4951
"terraform",
50-
[args],
52+
["show"],
5153
{
5254
shell: true,
5355
cwd,
5456
}
5557
);
5658
}
5759

58-
public async preImport(cwd: string, params: any, file: string): Promise<{ importArgs: string, tfFile: string }> {
59-
const fileName = (file === undefined) ? params.resource.type + '.tf' : file;
60+
public async preImport(cwd: string, args: any, file: string): Promise<{ importArgs: string, tfFile: string }> {
61+
const fileName = (file === undefined) ? args.resource.type + '.tf' : file;
6062

61-
const defaultContents = `resource "${params.resource.type}" "${params.resource.name}" {}`;
62-
const resAddress = `${params.resource.type}.${params.resource.name}`;
63+
const defaultContents = `resource "${args.resource.type}" "${args.resource.name}" {}`;
64+
const resAddress = `${args.resource.type}.${args.resource.name}`;
6365

6466
const tfFile: string = path.join(cwd, fileName);
6567

@@ -68,22 +70,20 @@ export class TerraformRunner extends BaseRunner {
6870
// reset state
6971
await this.resetTFState(resAddress);
7072

71-
const importArgs = ['import ', params.resource.type, '.', params.resource.name, ' ', params.resource.id].join('');
73+
const importArgs = ['import ', args.resource.type, '.', args.resource.name, ' ', args.resource.id].join('');
7274
console.debug("[DEBUG]#### import cmd: args=[%s], defaultContents=[%s]", importArgs, defaultContents);
7375
return { importArgs, tfFile };
7476
}
7577

76-
77-
private async resetFileContent(tfFile: string, defaultContents: string) {
78-
if (!fse.existsSync(tfFile)) {
79-
fse.writeFileSync(tfFile, defaultContents);
80-
} else {
81-
await fse.writeFile(tfFile, defaultContents);
82-
}
83-
}
84-
85-
private async resetTFState(resAddress: string) {
86-
await terraformShellManager.getIntegratedShell().runTerraformCmd(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
78+
public async executeImport(cwd: string, args?: string): Promise<string> {
79+
return await executeCommand(
80+
"terraform",
81+
[args],
82+
{
83+
shell: true,
84+
cwd,
85+
}
86+
);
8787
}
8888

8989
/**
@@ -110,6 +110,21 @@ export class TerraformRunner extends BaseRunner {
110110
}
111111
return;
112112
}
113+
114+
private async resetFileContent(tfFile: string, defaultContents: string) {
115+
if (!fse.existsSync(tfFile)) {
116+
fse.writeFileSync(tfFile, defaultContents);
117+
} else {
118+
await fse.writeFile(tfFile, defaultContents);
119+
}
120+
}
121+
122+
public async resetTFState(resAddress: string) {
123+
console.debug("[DEBUG]#### TerraformRunner resetTFState begin.");
124+
125+
await terraformShellManager.getIntegratedShell(TerraformRunner.getInstance())
126+
.runTerraformCmd(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
127+
}
113128
}
114129

115130
export function getCheckTerraformCmd(): boolean {

src/utils/terraformerRunner.ts renamed to src/client/runner/terraformerRunner.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
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.
45
*--------------------------------------------------------------------------------------------*/
56
"use strict";
67
import * as vscode from "vscode";
7-
import * as settingUtils from "./settingUtils";
8-
import { executeCommand } from "./cpUtils";
8+
import * as settingUtils from "../../utils/settingUtils";
9+
import { executeCommand } from "../../utils/cpUtils";
910
import { BaseRunner } from "./baseRunner";
10-
import { openUrlHintOrNotShowAgain } from "./uiUtils";
11+
import { openUrlHintOrNotShowAgain } from "../../utils/uiUtils";
1112

1213
export const defaultProduct = ["vpc", "subnet", "security_group"];
1314

@@ -55,8 +56,8 @@ export class TerraformerRunner extends BaseRunner {
5556
}
5657

5758

58-
public async preImport(cwd: string, args?: any, path?: string): Promise<any> {
59-
console.debug("[DEBUG]#### TerraformerRunner.preImport begin, cwd:[%s], args:[%s], path:[%s]", cwd, args, path);
59+
public async preImport(cwd: string, args?: any, file?: string): Promise<any> {
60+
console.debug("[DEBUG]#### TerraformerRunner.preImport begin, cwd:[%s], args:[%s], path:[%s]", cwd, args, file);
6061
return await executeCommand(
6162
"terraform",
6263
["init", "-upgrade"],
@@ -101,7 +102,7 @@ export class TerraformerRunner extends BaseRunner {
101102
public async postImport(cwd: string, args?: string): Promise<any> {
102103
console.debug("[DEBUG]#### TerraformerRunner.postImport begin, cwd:[%s], args:[%s]", cwd, args);
103104
const exeArgs = args.split(",");
104-
105+
105106
return await executeCommand(
106107
"terraformer",
107108
exeArgs,
@@ -112,6 +113,11 @@ export class TerraformerRunner extends BaseRunner {
112113
);
113114
}
114115

116+
public async executePlan(cwd: string, args?: string): Promise<string> {
117+
console.debug("[DEBUG]#### TerraformerRunner not need this step, skip it.");
118+
return "";
119+
}
120+
115121
public async executeShow(cwd: string, args?: string): Promise<string> {
116122
console.debug("[DEBUG]#### TerraformerRunner not need this step, skip it.");
117123
return "";
File renamed without changes.

src/cloudShell.ts renamed to src/client/terminal/cloudShell.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,16 @@
55

66
"use strict";
77

8-
import * as fsExtra from "fs-extra";
9-
import * as path from "path";
10-
import { MessageItem } from "vscode";
118
import * as vscode from "vscode";
129
import * as TelemetryWrapper from "vscode-extension-telemetry-wrapper";
1310
// import { AzureAccount, CloudShell } from "./azure-account.api";
1411
import { BaseShell } from "./baseShell";
1512
// import { aciConfig, Constants, exportContainerCmd, exportTestScript } from "./constants";
1613
// import { azFileDelete, azFilePush, escapeFile, TerraformCommand, TestOption } from "./shared";
17-
import { TerraformCommand } from "./commons/commands";
14+
import { TerraformCommand } from "../../commons/customCmdRegister";
1815
import { terraformChannel } from "./terraformChannel";
1916
// import { getStorageAccountforCloudShell, IStorageAccount } from "./utils/cloudShellUtils";
20-
import * as settingUtils from "./utils/settingUtils";
21-
import { DialogOption, DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
22-
import { selectWorkspaceFolder } from "./utils/workspaceUtils";
17+
import { DialogOption, DialogType, promptForOpenOutputChannel } from "../../utils/uiUtils";
2318

2419
export class TencentCloudShell extends BaseShell {
2520

0 commit comments

Comments
 (0)