Skip to content

Commit e855148

Browse files
committed
terraform import demo
1 parent 47c89f5 commit e855148

File tree

12 files changed

+172
-140
lines changed

12 files changed

+172
-140
lines changed

images/images_bak/logo.png

-26.6 KB
Binary file not shown.

package-lock.json

Lines changed: 12 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"publisher": "Tencent-Cloud",
88
"icon": "images/tc-tf-logo.png",
99
"main": "./out/src/extension.js",
10+
"type": "commonjs",
1011
"repository": {
1112
"type": "git",
1213
"url": "https://github.com/tencentcloudstack/vscode-tencentcloud-terraform.git"
@@ -73,17 +74,6 @@
7374
"contextualTitle": "Resources Explorer",
7475
"visibility": "collapsed"
7576
},
76-
{
77-
"id": "tcTerraform.resourcesExplorer.tke",
78-
"name": "%TcTerraform.view.resource.explorer.tke%",
79-
"contextualTitle": "Resources Explorer",
80-
"visibility": "collapsed"
81-
},
82-
{
83-
"id": "tcTerraform.templatesExplorer",
84-
"name": "%TcTerraform.view.template.explorer%",
85-
"visibility": "collapsed"
86-
},
8777
{
8878
"id": "tcTerraform.helpExplorer",
8979
"name": "%TcTerraform.view.help.explorer%",
@@ -249,18 +239,19 @@
249239
"all": "^0.0.0",
250240
"asynckit": "^0.4.0",
251241
"axios": "^1.3.5",
242+
"chalk": "^4.1.2",
252243
"fs-extra": "^11.1.0",
253244
"inversify": "^6.0.1",
254245
"lodash": "^4.17.21",
255246
"lodash-es": "^4.17.21",
256247
"opn": "^6.0.0",
257248
"reflect-metadata": "^0.1.13",
258249
"shared": "^0.2.0",
259-
"strip-ansi": "^7.0.1",
250+
"strip-ansi": "=6.0.0",
260251
"tencentcloud-sdk-nodejs": "^4.0.580",
261252
"tencentcloud-sdk-nodejs-cvm": "^4.0.580",
262253
"tencentcloud-sdk-nodejs-tke": "^4.0.576",
263254
"vscode-extension-telemetry-wrapper": "^0.13.3",
264255
"vscode-nls-i18n": "^0.2.4"
265256
}
266-
}
257+
}

package.nls.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"TcTerraform.view.session.explorer": "Connect to TencentCloud",
44
"TcTerraform.view.template.explorer": "Tempaltes Management",
55
"TcTerraform.view.resource.explorer": "Resources Explorer",
6-
"TcTerraform.view.resource.explorer.cvm": "CVM",
7-
"TcTerraform.view.resource.explorer.tke": "TKE",
6+
"TcTerraform.view.resource.explorer.cvm": "Import Resouce: CVM",
7+
"TcTerraform.view.resource.explorer.tke": "Import Resouce: TKE",
88
"TcTerraform.view.help.explorer": "Help",
99
"TcTerraform.view.help.explorer.provider": "TencentCloud Terraform Provider",
1010
"TcTerraform.view.help.explorer.doc": "Documentation",

src/autocomplete/TerraformDefinitionProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
Definition
77
} from "vscode";
88
import * as _ from "lodash";
9-
import * as opn from "opn";
9+
// import * as opn from "opn";
10+
import opn from "opn";
1011
// var urls = require("../../config/snippets/tiat-urls.json");
1112
import urls from "../../config/snippets/tiat-urls.json";
1213

src/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export enum TerraformCommand {
88
Refresh = "terraform refresh",
99
Destroy = "terraform destroy",
1010
Validate = "terraform validate",
11+
Show = "terraform show",
12+
State = "terraform state"
1113
}
1214

1315
export enum TcCliCommand {

src/integratedShell.ts

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ import { drawGraph } from "./utils/dotUtils";
1818
import { isDotInstalled } from "./utils/dotUtils";
1919
import { selectWorkspaceFolder } from "./utils/workspaceUtils";
2020
import { checkTerraformInstalled } from "./terraformUtils";
21+
import { TerraformCommand } from "./common";
22+
import * as helper from "./utils/helper";
23+
import { command } from "./commons/tencent/commands";
24+
import { promisify } from "util";
25+
import { ChildProcess } from "child_process";
26+
import * as cp from "child_process";
27+
2128
// import stripAnsi from 'strip-ansi';
22-
import stripAnsi from 'strip-ansi';
2329

2430
export class IntegratedShell extends BaseShell {
2531
private static readonly GRAPH_FILE_NAME = "graph.png";
@@ -72,32 +78,29 @@ export class IntegratedShell extends BaseShell {
7278
const fileName = (file === undefined) ? params.resource.type + '.tf' : file;
7379

7480
const defaultContents = `resource "${params.resource.type}" "${params.resource.name}" {}`;
81+
const resAddress = `${params.resource.type}.${params.resource.name}`;
7582

7683
const tfFile: string = path.join(cwd, fileName);
7784

78-
if (!fse.existsSync(tfFile)) {
79-
fse.writeFileSync(tfFile, defaultContents);
80-
} else {
81-
await fse.writeFile(tfFile, defaultContents);
82-
}
85+
// reset file
86+
await this.resetFileContent(tfFile, defaultContents);
87+
// reset state
88+
await this.resetTFState(resAddress);
8389

8490
const importArgs = ['import ', params.resource.type, '.', params.resource.name, ' ', params.resource.id].join('');
85-
console.debug("[DEBUG]#### import cmd: args=[$s], defaultContents=[%s]", importArgs, defaultContents);
86-
87-
// await this.deleteFile(cwd, fileName);
88-
// const output: string = await executeCommand(
89-
// "terraform",
90-
// [args],
91-
// {
92-
// shell: true,
93-
// cwd,
94-
// },
95-
// );
96-
97-
await this.runTerraformCmd("terraform " + importArgs);
98-
99-
// const stripAnsiPromise = import('strip-ansi');
100-
// const stripAnsi = (await stripAnsiPromise).default;
91+
console.debug("[DEBUG]#### import cmd: args=[%s], defaultContents=[%s]", importArgs, defaultContents);
92+
93+
const importRet: string = await executeCommand(
94+
"terraform",
95+
[importArgs],
96+
{
97+
shell: true,
98+
cwd,
99+
},
100+
);
101+
102+
// await this.runTerraformCmd("terraform " + importArgs);
103+
101104
const content: string =
102105
await executeCommand(
103106
"terraform",
@@ -107,10 +110,6 @@ export class IntegratedShell extends BaseShell {
107110
cwd,
108111
}
109112
);
110-
// const content = stripAnsi(output);
111-
112-
const my = stripAnsi('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');
113-
console.debug("my:%s", my);
114113

115114
console.debug("[DEBUG]#### import content:[%s]", content);
116115
fse.writeFileSync(tfFile, content);
@@ -119,10 +118,36 @@ export class IntegratedShell extends BaseShell {
119118
await commands.executeCommand("vscode.open", Uri.file(tfFile), ViewColumn.Active || ViewColumn.One);
120119
}
121120

122-
public async runTerraformCmd(tfCommand: string) {
121+
private async resetFileContent(tfFile: string, defaultContents: string) {
122+
if (!fse.existsSync(tfFile)) {
123+
fse.writeFileSync(tfFile, defaultContents);
124+
} else {
125+
await fse.writeFile(tfFile, defaultContents);
126+
}
127+
}
128+
129+
private async resetTFState(resAddress: string) {
130+
// const runFunc = () => this.runTerraformCmdWithoutTerminal(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
131+
// await helper.retryF(runFunc);
132+
133+
await this.runTerraformCmd(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
134+
}
135+
136+
public async runTerraformCmdWithoutTerminal(tfCommand: string, args?: string[]) {
137+
const cmd = [tfCommand, ...(args || [])].join(' ');
138+
const { stdout, stderr } = await promisify(cp.exec)(cmd);
139+
return { stdout, stderr };
140+
}
141+
142+
public async runTerraformCmd(tfCommand: string, args?: string[]) {
123143
this.checkCreateTerminal();
124144
this.terminal.show();
125-
this.terminal.sendText(tfCommand);
145+
146+
// const cmd= [tfCommand, args.values].join(' ');
147+
let tmp: string[] = [tfCommand];
148+
args.forEach((arg) => tmp.push(arg));
149+
const cmd = tmp.join(' ');
150+
this.terminal.sendText(cmd);
126151
}
127152

128153
public async runNormalCmd(tfCommand: string, newLine = true) {

0 commit comments

Comments
 (0)