From 252be2489a24c9d9c5914c9b26624cf3dde493f3 Mon Sep 17 00:00:00 2001 From: Chang Zhe Jiet Date: Sat, 25 Nov 2023 01:47:02 +0800 Subject: [PATCH] add import --- src/TerraformGenerator.ts | 26 +++++++++++----------- src/blocks/Import.ts | 2 +- test/blocks/Import.test.ts | 8 +++---- test/tfg/Others.test.ts | 6 +++++ test/tfg/__snapshots__/Others.test.ts.snap | 16 +++++++++++++ 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/TerraformGenerator.ts b/src/TerraformGenerator.ts index 72af11d..e81b35a 100644 --- a/src/TerraformGenerator.ts +++ b/src/TerraformGenerator.ts @@ -310,6 +310,19 @@ export class TerraformGenerator { return block; } + /** + * Add import into Terraform. + * + * Refer to Terraform documentation on what can be put as arguments. + * + * @param args arguments + */ + import(args?: Record): Import { + const block = new Import(args); + this.addBlocks(block); + return block; + } + /** * Add variable values into Terraform. * @@ -323,19 +336,6 @@ export class TerraformGenerator { return this; } - /** - * Add import into Terraform. - * - * Refer to Terraform documentation on what can be put as arguments. - * - * @param args arguments - */ - import(args?: Record): Import { - const importBlock = new Import(args); - this.addBlocks(importBlock); - return importBlock; - } - /** * Merge this instance with other TerraformGenerator instances. * diff --git a/src/blocks/Import.ts b/src/blocks/Import.ts index fc5e805..d1d518e 100644 --- a/src/blocks/Import.ts +++ b/src/blocks/Import.ts @@ -7,7 +7,7 @@ import { Block } from '.'; export class Import extends Block { /** - * Construct import blocks. + * Construct import. * * Refer to Terraform documentation on what can be put as arguments. * diff --git a/test/blocks/Import.test.ts b/test/blocks/Import.test.ts index a35cd28..e06857c 100644 --- a/test/blocks/Import.test.ts +++ b/test/blocks/Import.test.ts @@ -2,8 +2,8 @@ import { arg4 } from '..'; import { Import } from '../../src/blocks'; test('Import', () => { - const importTest = new Import(arg4); - expect(importTest.toTerraform()).toMatchSnapshot(); - expect(() => importTest.asArgument()).toThrow(); - expect(() => importTest.attr('attr')).toThrow(); + const imp = new Import(arg4); + expect(imp.toTerraform()).toMatchSnapshot(); + expect(() => imp.asArgument()).toThrow(); + expect(() => imp.attr('attr')).toThrow(); }); diff --git a/test/tfg/Others.test.ts b/test/tfg/Others.test.ts index 3a0274f..fb38c40 100644 --- a/test/tfg/Others.test.ts +++ b/test/tfg/Others.test.ts @@ -61,6 +61,12 @@ const createTerraformGenerator = (): TerraformGenerator => { c: r.attr('x') }); + tfg.import({ + a: 'a', + b: 123, + c: r.attr('x') + }); + tfg.resource('tags', 'tags', { tags: map({ 'a': 'a', diff --git a/test/tfg/__snapshots__/Others.test.ts.snap b/test/tfg/__snapshots__/Others.test.ts.snap index 0a0bb38..9f15156 100644 --- a/test/tfg/__snapshots__/Others.test.ts.snap +++ b/test/tfg/__snapshots__/Others.test.ts.snap @@ -90,6 +90,10 @@ exports[`Others 2`] = ` "blockType": "provisioner", "type": "provisioner", }, + Import { + "blockNames": [], + "blockType": "import", + }, Resource { "blockNames": [ "tags", @@ -188,6 +192,12 @@ b = 123 c = aws_vpc.test.x } +import{ +a = "a" +b = 123 +c = aws_vpc.test.x +} + resource "tags" "tags"{ tags = { a = "a" @@ -276,6 +286,12 @@ b = 123 c = aws_vpc.test.x } +import{ +a = "a" +b = 123 +c = aws_vpc.test.x +} + resource "tags" "tags"{ tags = { a = "a"