@@ -17,13 +17,14 @@ import { executeCommand } from "./utils/cpUtils";
1717import { drawGraph } from "./utils/dotUtils" ;
1818import { isDotInstalled } from "./utils/dotUtils" ;
1919import { selectWorkspaceFolder } from "./utils/workspaceUtils" ;
20- import { checkTerraformInstalled } from "./terraformUtils" ;
21- import { TerraformCommand } from "./common" ;
20+ import { TerraformCommand } from "./commons/commands" ;
2221import * as helper from "./utils/helper" ;
2322import { command } from "./commons/tencent/commands" ;
2423import { promisify } from "util" ;
2524import { ChildProcess } from "child_process" ;
2625import * as cp from "child_process" ;
26+ import { TerraformerRunner , CommandType , FlagType , FlagsMap } from "./utils/terraformerRunner" ;
27+ import { values } from "lodash" ;
2728
2829// import stripAnsi from 'strip-ansi';
2930
@@ -66,72 +67,54 @@ export class IntegratedShell extends BaseShell {
6667 }
6768
6869 public async import ( params : any , file ?: string ) : Promise < void > {
69- // const fileName = "cvm.tf";
70- await checkTerraformInstalled ( ) ;
70+
71+ const runner = TerraformerRunner . getInstance ( ) ; // terraform or terraformer
72+
73+ await runner . checkInstalled ( ) ;
7174
7275 const cwd : string = await selectWorkspaceFolder ( ) ;
7376 if ( ! cwd ) {
7477 TelemetryWrapper . sendError ( Error ( "noWorkspaceSelected" ) ) ;
7578 return ;
7679 }
7780
78- const fileName = ( file === undefined ) ? params . resource . type + '.tf' : file ;
81+ const cmd = CommandType . Import ;
82+ const flags : FlagsMap [ ] = [
83+ {
84+ flag : FlagType . Resources ,
85+ value : [ "mysql" , "vpc" , "subnet" , "security_group" ] . join ( "," )
86+ } ,
87+ {
88+ flag : FlagType . Filter ,
89+ value : [ "tencentcloud_mysql_instance" , "cdb-fitq5t9h" ] . join ( "=" )
90+ } ,
91+ {
92+ flag : FlagType . Regions ,
93+ value : "ap-guangzhou"
94+ } ,
95+ {
96+ flag : FlagType . Redirect ,
97+ value : "terraformer_default_result"
98+ } ,
99+ ] ;
79100
80- const defaultContents = `resource "${ params . resource . type } " "${ params . resource . name } " {}` ;
81- const resAddress = `${ params . resource . type } .${ params . resource . name } ` ;
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 ) ;
82104
83- const tfFile : string = path . join ( cwd , fileName ) ;
84105
85- // reset file
86- await this . resetFileContent ( tfFile , defaultContents ) ;
87- // reset state
88- await this . resetTFState ( resAddress ) ;
89106
90- const importArgs = [ 'import ' , params . resource . type , '.' , params . resource . name , ' ' , params . resource . id ] . join ( '' ) ;
91- console . debug ( "[DEBUG]#### import cmd: args=[%s], defaultContents=[%s]" , importArgs , defaultContents ) ;
107+ const content : string = await runner . executeShow ( cwd ) ;
92108
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-
104- const content : string =
105- await executeCommand (
106- "terraform" ,
107- [ "show" ] ,
108- {
109- shell : true ,
110- cwd,
111- }
112- ) ;
113109
114- console . debug ( "[DEBUG]#### import content:[%s]" , content ) ;
115- fse . writeFileSync ( tfFile , content ) ;
110+ const tfFile : string = result ;
116111
117112 vscode . window . showInformationMessage ( `The resource:[${ params . resource . type } ] has been imported successfully, generated tf file:[${ tfFile } ].` ) ;
118- await commands . executeCommand ( "vscode.open" , Uri . file ( tfFile ) , ViewColumn . Active || ViewColumn . One ) ;
119- }
120113
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- }
114+ await commands . executeCommand ( "vscode.open" , Uri . file ( tfFile ) , ViewColumn . Active || ViewColumn . One ) ;
127115 }
128116
129- private async resetTFState ( resAddress : string ) {
130- // const runFunc = () => this.runTerraformCmdWithoutTerminal(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
131- // await helper.retryF(runFunc);
132117
133- await this . runTerraformCmd ( TerraformCommand . State , [ 'rm' , '-lock=false' , resAddress ] ) ;
134- }
135118
136119 public async runTerraformCmdWithoutTerminal ( tfCommand : string , args ?: string [ ] ) {
137120 const cmd = [ tfCommand , ...( args || [ ] ) ] . join ( ' ' ) ;
0 commit comments