Skip to content

Commit 064f3d2

Browse files
committed
terraformer integrated
1 parent 751205a commit 064f3d2

File tree

8 files changed

+76
-44
lines changed

8 files changed

+76
-44
lines changed

src/import/cvm.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { Instance } from "tencentcloud-sdk-nodejs-cvm/tencentcloud/services/cvm/
66
import { ITencentCloudAPI } from "../commons/tencent/sdkApi";
77
import { error } from "console";
88

9-
109
export class CvmService implements ITencentCloudAPI {
1110
async getConfig(params?: any): Promise<any> {
1211
return {
12+
'product': 'cvm',
1313
'resource': {
1414
'name': "tencentcloud_instance",
1515
//'xxx': "yyy"
@@ -39,9 +39,6 @@ export class CvmService implements ITencentCloudAPI {
3939

4040
return res;
4141
}
42-
43-
44-
4542
}
4643

4744
// export async function describeInstances(params:any): Promise<Instance[]> {

src/import/mysql.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ITencentCloudAPI } from "@/commons/tencent/sdkApi";
2+
3+
export class MysqlService implements ITencentCloudAPI {
4+
describeInstances(params?: any): Promise<any> {
5+
throw new Error("Method not implemented.");
6+
}
7+
getConfig(params?: any): Promise<any> {
8+
throw new Error("Method not implemented.");
9+
}
10+
11+
}

src/import/tke.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ITencentCloudAPI } from "@/commons/tencent/sdkApi";
2+
3+
export class TkeService implements ITencentCloudAPI {
4+
describeInstances(params?: any): Promise<any> {
5+
throw new Error("Method not implemented.");
6+
}
7+
getConfig(params?: any): Promise<any> {
8+
throw new Error("Method not implemented.");
9+
}
10+
}

src/integratedShell.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { command } from "./commons/tencent/commands";
2323
import { promisify } from "util";
2424
import { ChildProcess } from "child_process";
2525
import * as cp from "child_process";
26-
import { TerraformerRunner, CommandType, FlagType, FlagsMap } from "./utils/terraformerRunner";
26+
import { TerraformerRunner, CommandType, FlagType, FlagsMap, defaultProduct } from "./utils/terraformerRunner";
2727
import { values } from "lodash";
2828

2929
// import stripAnsi from 'strip-ansi';
@@ -78,15 +78,23 @@ export class IntegratedShell extends BaseShell {
7878
return;
7979
}
8080

81+
const preRet = await runner.preImport(cwd);
82+
console.debug("[DEBUG]#### Executed pre-import. result:[%s]", preRet);
83+
84+
const resource = defaultProduct;
85+
if (!defaultProduct.includes(params.product)) {
86+
resource.push(params.product);
87+
}
88+
8189
const cmd = CommandType.Import;
8290
const flags: FlagsMap[] = [
8391
{
8492
flag: FlagType.Resources,
85-
value: ["mysql", "vpc", "subnet", "security_group"].join(",")
93+
value: resource.join(",")
8694
},
8795
{
8896
flag: FlagType.Filter,
89-
value: ["tencentcloud_mysql_instance", "cdb-fitq5t9h"].join("=")
97+
value: [params.resource.type, params.resource.id].join("=")
9098
},
9199
{
92100
flag: FlagType.Regions,
@@ -98,24 +106,23 @@ export class IntegratedShell extends BaseShell {
98106
},
99107
];
100108

101-
console.debug("[DEBUG]#### Executing import command. cwd:[%s], cmd:[%s], flags:[%s]", cwd, cmd.toString, flags.toString);
102-
const result = await runner.executeImport(cwd, "", cmd, flags);
103-
console.debug("[DEBUG]#### Executed import command. result:[%s]", result);
104-
109+
const importRet = await runner.executeImport(cwd, "", cmd, flags);
110+
console.debug("[DEBUG]#### Executed import command. result:[%s]", importRet);
105111

112+
// terraform state replace-provider registry.terraform.io/-/tencentcloud tencentcloudstack/tencentcloud
113+
const args = "";
114+
const postRet = await runner.postImport(cwd, args);
106115

107116
const content: string = await runner.executeShow(cwd);
108117

109-
110-
const tfFile: string = result;
118+
const tfFile: string = importRet;
111119

112120
vscode.window.showInformationMessage(`The resource:[${params.resource.type}] has been imported successfully, generated tf file:[${tfFile}].`);
113121

114122
await commands.executeCommand("vscode.open", Uri.file(tfFile), ViewColumn.Active || ViewColumn.One);
115123
}
116124

117125

118-
119126
public async runTerraformCmdWithoutTerminal(tfCommand: string, args?: string[]) {
120127
const cmd = [tfCommand, ...(args || [])].join(' ');
121128
const { stdout, stderr } = await promisify(cp.exec)(cmd);

src/utils/baseRunner.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ export abstract class BaseRunner {
1212
this.init();
1313
}
1414

15+
public abstract init(): void;
1516
/**
1617
* execute this command to prepare the terraform import.
1718
* @param cwd
1819
* @returns
1920
*/
2021
public abstract preImport(cwd: string, args?: any, path?: string): Promise<any>;
2122

22-
/**
23-
* execute this command to display/output the terraform state.
24-
* @param cwd
25-
* @returns
26-
*/
27-
public abstract executeShow(cwd: string, args?: string): Promise<any>;
28-
2923
/**
3024
* execute this command to import the existing resource from tencentcloud
3125
* @param cwd
@@ -37,14 +31,20 @@ export abstract class BaseRunner {
3731
/**
3832
* execute this command to handle post of the terraform import.
3933
* @param cwd
34+
* @param executor Choose who will execute this command? terraform or terraformer
4035
* @returns
4136
*/
42-
public abstract postImport(cwd: string, args?: string): Promise<any>;
37+
public abstract postImport(cwd: string, executor?:string, args?: string): Promise<any>;
4338

4439
/**
4540
* check binary whether ready or not
4641
*/
4742
public abstract checkInstalled(): Promise<void>;
4843

49-
public abstract init(): void;
44+
/**
45+
* execute this command to display/output the terraform state.
46+
* @param cwd
47+
* @returns
48+
*/
49+
public abstract executeShow(cwd: string, args?: string): Promise<any>;
5050
}

src/utils/terraformRunner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class TerraformRunner extends BaseRunner {
3030
}
3131

3232
public init(): void {
33-
throw new Error("Method not implemented.");
33+
// throw new Error("Method not implemented.");
3434
}
3535

3636
public async executeShow(cwd: string, args?: string): Promise<string> {
@@ -91,8 +91,8 @@ export class TerraformRunner extends BaseRunner {
9191
* @param cwd
9292
* @param args
9393
*/
94-
public postImport(cwd: string, args?: string): Promise<any> {
95-
throw new Error("Method not implemented.");
94+
public async postImport(cwd: string, args?: string): Promise<any> {
95+
console.debug("[DEBUG]#### terraform postImport TODO.");
9696
}
9797

9898
public async checkInstalled(): Promise<void> {

src/utils/terraformerRunner.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { executeCommand } from "./cpUtils";
99
import { BaseRunner } from "./baseRunner";
1010
import { openUrlHintOrNotShowAgain } from "./uiUtils";
1111

12+
export const defaultProduct = ["vpc", "subnet", "security_group"];
13+
1214
export enum CommandType {
1315
Import = "import tencentcloud",
1416
Plan = "plan tencentcloud",
@@ -34,6 +36,7 @@ export interface FlagsMap {
3436
}
3537

3638
export class TerraformerRunner extends BaseRunner {
39+
static defaultProduct: any;
3740
private constructor() {
3841
super();
3942
}
@@ -48,34 +51,26 @@ export class TerraformerRunner extends BaseRunner {
4851
}
4952

5053
public init(): void {
51-
throw new Error("Method not implemented.");
54+
// throw new Error("Method not implemented.");
5255
}
5356

5457

5558
public async preImport(cwd: string, args?: any, path?: string): Promise<any> {
56-
await executeCommand(
59+
console.debug("[DEBUG]#### TerraformerRunner.preImport begin, cwd:[%s], args:[%s], path:[%s]", cwd, args, path);
60+
return await executeCommand(
5761
"terraform",
5862
["init", "-upgrade"],
5963
{
6064
shell: true,
6165
cwd,
6266
},
6367
);
64-
65-
66-
// return await executeCommand(
67-
// "terraformer",
68-
// exeArgs,
69-
// {
70-
// shell: true,
71-
// cwd,
72-
// }
73-
// );
7468
}
7569

7670
public async executeImport(cwd: string, args?: string, cmd?: CommandType, flags?: FlagsMap[]): Promise<string> {
71+
console.debug("[DEBUG]#### TerraformerRunner.executeImport begin, cwd:[%s], args:[%s], cmd:[%s], flags:[%s]", cwd, args, cmd.toString(), flags.toString());
7772
const exeArgs: string[] = [];
78-
if (args){
73+
if (args) {
7974
exeArgs.push(args);
8075
}
8176

@@ -85,13 +80,13 @@ export class TerraformerRunner extends BaseRunner {
8580

8681
if (flags) {
8782
flags.forEach((vv) => {
88-
exeArgs.push(vv.flag.toString(), " ", vv.value);
83+
exeArgs.push(vv.flag.toString(), vv.value);
8984
});
9085
}
9186

9287
const opExeArgs: string = exeArgs.join(" ");
9388

94-
console.debug("[DEBUG]#### import opExeArgs:[%s]", "terraformer" + opExeArgs);
89+
console.debug("[DEBUG]#### import exeArgs:[%s]", opExeArgs);
9590

9691
return await executeCommand(
9792
"terraformer",
@@ -104,11 +99,21 @@ export class TerraformerRunner extends BaseRunner {
10499
}
105100

106101
public async postImport(cwd: string, args?: string): Promise<any> {
107-
throw new Error("Method not implemented.");
102+
console.debug("[DEBUG]#### TerraformerRunner.postImport begin, cwd:[%s], args:[%s]", cwd, args);
103+
const exeArgs = args.split(",");
104+
105+
return await executeCommand(
106+
"terraformer",
107+
exeArgs,
108+
{
109+
shell: true,
110+
cwd,
111+
}
112+
);
108113
}
109114

110115
public async executeShow(cwd: string, args?: string): Promise<string> {
111-
console.debug("[DEBUG]#### terraformer not need this step, skip it.");
116+
console.debug("[DEBUG]#### TerraformerRunner not need this step, skip it.");
112117
return "";
113118
}
114119

src/views/resources/resExplorer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export class CvmResProvider extends tencent.tree.TreeDataProvider {
3636
name: defaultResourceName,
3737
id: instance.InstanceId
3838
},
39-
fileName: config.import.file
39+
product: config.product,
40+
fileName: config.import.file,
41+
this: config
4042
}],
4143
},
4244
}))

0 commit comments

Comments
 (0)