Skip to content

Commit

Permalink
Merge pull request #2 from tencentcloudstack/feat/support-import
Browse files Browse the repository at this point in the history
1.enable the terraformer. 2.adjust code format.
  • Loading branch information
lyu571 authored Oct 17, 2023
2 parents a7f2e22 + 98add33 commit 21ec176
Show file tree
Hide file tree
Showing 23 changed files with 537 additions and 161 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ This extension supports the following features:

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

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

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-tencentcloud-terraform",
"displayName": "Tencent Cloud Terraform",
"description": "VS Code extension for developing with Terraform on Tencent Cloud",
"version": "0.0.5",
"version": "0.0.6",
"license": "MIT",
"publisher": "Tencent-Cloud",
"icon": "images/tc-tf-logo.png",
Expand Down Expand Up @@ -150,9 +150,10 @@
"category": "TencentCloud Terraform"
},
{
"command": "tcTerraform.push",
"title": "Push",
"category": "TencentCloud Terraform"
"command": "tcTerraform.git.push",
"title": "Push to git",
"category": "TencentCloud Terraform",
"shortTitle": "Push"
},
{
"command": "tcTerraformer.import",
Expand Down
19 changes: 15 additions & 4 deletions src/utils/baseRunner.ts → src/client/runner/baseRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,34 @@ export abstract class BaseRunner {
* @param cwd
* @returns
*/
public abstract preImport(cwd: string, args?: any, path?: string): Promise<any>;
public abstract preImport(cwd: string, args?: any, file?: string): Promise<any>;

/**
* execute this command to import the existing resource from tencentcloud
* @param cwd
* @param args
* @returns
*/
public abstract executeImport(cwd: string, args?: string): Promise<any>;
public abstract executeImport(cwd: string, args?: any, cmd?: any, flags?: any): Promise<any>;

/**
* execute this command to handle post of the terraform import.
* @param cwd
* @param executor Choose who will execute this command? terraform or terraformer
* @param cmd
* @param flags
* @returns
*/
public abstract postImport(cwd: string, executor?:string, args?: string): Promise<any>;
public abstract postImport(cwd: string, executor?: string, args?: string): Promise<any>;

/**
* execute this command to plan the tf code
* @param cwd
* @param args
* @param cmd
* @param flags
*/
public abstract executePlan(cwd: string, args?: any, cmd?: any, flags?: any): Promise<any>;

/**
* check binary whether ready or not
Expand All @@ -46,5 +57,5 @@ export abstract class BaseRunner {
* @param cwd
* @returns
*/
public abstract executeShow(cwd: string, args?: string): Promise<any>;
public abstract executeShow(cwd: string, args?: any): Promise<any>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import * as path from "path";
import * as fse from "fs-extra";
import * as vscode from "vscode";
import { executeCommand } from "./cpUtils";
import { BaseRunner } from "./baseRunner";
import { TerraformCommand } from "../commons/commands";
import { terraformShellManager } from "../terraformShellManager";
import * as settingUtils from "./settingUtils";
import { openUrlHintOrNotShowAgain } from "./uiUtils";
import { TerraformCommand } from "../../commons/customCmdRegister";
import { terraformShellManager } from "../terminal/terraformShellManager";
import { executeCommand } from "../../utils/cpUtils";
import * as settingUtils from "../../utils/settingUtils";
import { openUrlHintOrNotShowAgain } from "../../utils/uiUtils";


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

public async executeShow(cwd: string, args?: string): Promise<string> {
return await executeCommand(
"terraform",
["show"],
{
shell: true,
cwd,
}
);
public async executePlan(cwd: string, args: any): Promise<string> {
console.debug("[DEBUG]#### TerraformRunner executePlan begin.");

const resAddress = `${args.resource.type}.${args.resource.name}`;

// reset state
await this.resetTFState(resAddress);

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

return "";
}

public async executeImport(cwd: string, args?: string): Promise<string> {
public async executeShow(cwd: string, args?: any): Promise<string> {
return await executeCommand(
"terraform",
[args],
["show"],
{
shell: true,
cwd,
}
);
}

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

const defaultContents = `resource "${params.resource.type}" "${params.resource.name}" {}`;
const resAddress = `${params.resource.type}.${params.resource.name}`;
const defaultContents = `resource "${args.resource.type}" "${args.resource.name}" {}`;
const resAddress = `${args.resource.type}.${args.resource.name}`;

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

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

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


private async resetFileContent(tfFile: string, defaultContents: string) {
if (!fse.existsSync(tfFile)) {
fse.writeFileSync(tfFile, defaultContents);
} else {
await fse.writeFile(tfFile, defaultContents);
}
}

private async resetTFState(resAddress: string) {
await terraformShellManager.getIntegratedShell().runTerraformCmd(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
public async executeImport(cwd: string, args?: string): Promise<string> {
return await executeCommand(
"terraform",
[args],
{
shell: true,
cwd,
}
);
}

/**
Expand All @@ -110,6 +110,21 @@ export class TerraformRunner extends BaseRunner {
}
return;
}

private async resetFileContent(tfFile: string, defaultContents: string) {
if (!fse.existsSync(tfFile)) {
fse.writeFileSync(tfFile, defaultContents);
} else {
await fse.writeFile(tfFile, defaultContents);
}
}

public async resetTFState(resAddress: string) {
console.debug("[DEBUG]#### TerraformRunner resetTFState begin.");

await terraformShellManager.getIntegratedShell(TerraformRunner.getInstance())
.runTerraformCmd(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
}
}

export function getCheckTerraformCmd(): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable @typescript-eslint/naming-convention */
/*---------------------------------------------------------------------------------------------
* Copyright (c) Tencent Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
"use strict";
import * as vscode from "vscode";
import * as settingUtils from "./settingUtils";
import { executeCommand } from "./cpUtils";
import * as settingUtils from "../../utils/settingUtils";
import { executeCommand } from "../../utils/cpUtils";
import { BaseRunner } from "./baseRunner";
import { openUrlHintOrNotShowAgain } from "./uiUtils";
import { openUrlHintOrNotShowAgain } from "../../utils/uiUtils";

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

Expand Down Expand Up @@ -55,8 +56,8 @@ export class TerraformerRunner extends BaseRunner {
}


public async preImport(cwd: string, args?: any, path?: string): Promise<any> {
console.debug("[DEBUG]#### TerraformerRunner.preImport begin, cwd:[%s], args:[%s], path:[%s]", cwd, args, path);
public async preImport(cwd: string, args?: any, file?: string): Promise<any> {
console.debug("[DEBUG]#### TerraformerRunner.preImport begin, cwd:[%s], args:[%s], path:[%s]", cwd, args, file);
return await executeCommand(
"terraform",
["init", "-upgrade"],
Expand Down Expand Up @@ -101,7 +102,7 @@ export class TerraformerRunner extends BaseRunner {
public async postImport(cwd: string, args?: string): Promise<any> {
console.debug("[DEBUG]#### TerraformerRunner.postImport begin, cwd:[%s], args:[%s]", cwd, args);
const exeArgs = args.split(",");

return await executeCommand(
"terraformer",
exeArgs,
Expand All @@ -112,6 +113,11 @@ export class TerraformerRunner extends BaseRunner {
);
}

public async executePlan(cwd: string, args?: string): Promise<string> {
console.debug("[DEBUG]#### TerraformerRunner not need this step, skip it.");
return "";
}

public async executeShow(cwd: string, args?: string): Promise<string> {
console.debug("[DEBUG]#### TerraformerRunner not need this step, skip it.");
return "";
Expand Down
File renamed without changes.
9 changes: 2 additions & 7 deletions src/cloudShell.ts → src/client/terminal/cloudShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@

"use strict";

import * as fsExtra from "fs-extra";
import * as path from "path";
import { MessageItem } from "vscode";
import * as vscode from "vscode";
import * as TelemetryWrapper from "vscode-extension-telemetry-wrapper";
// import { AzureAccount, CloudShell } from "./azure-account.api";
import { BaseShell } from "./baseShell";
// import { aciConfig, Constants, exportContainerCmd, exportTestScript } from "./constants";
// import { azFileDelete, azFilePush, escapeFile, TerraformCommand, TestOption } from "./shared";
import { TerraformCommand } from "./commons/commands";
import { TerraformCommand } from "../../commons/customCmdRegister";
import { terraformChannel } from "./terraformChannel";
// import { getStorageAccountforCloudShell, IStorageAccount } from "./utils/cloudShellUtils";
import * as settingUtils from "./utils/settingUtils";
import { DialogOption, DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
import { selectWorkspaceFolder } from "./utils/workspaceUtils";
import { DialogOption, DialogType, promptForOpenOutputChannel } from "../../utils/uiUtils";

export class TencentCloudShell extends BaseShell {

Expand Down
Loading

0 comments on commit 21ec176

Please sign in to comment.