Skip to content

Commit

Permalink
moved & removed blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang Zhe Jiet authored and Chang Zhe Jiet committed Mar 28, 2024
1 parent b975c8e commit a09f737
Show file tree
Hide file tree
Showing 10 changed files with 773 additions and 53 deletions.
28 changes: 27 additions & 1 deletion src/TerraformGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import child_process from 'child_process';
import fs from 'fs';
import path from 'path';
import shell from 'shelljs';
import { Block, Comment, Resource, Data, Module, Output, Provider, Variable, Backend, Provisioner, ResourceToDataOptions, Locals, Import, ImportArgs, VariableArgs, ModuleArgs, OutputArgs } from './blocks';
import { Block, Comment, Resource, Data, Module, Output, Provider, Variable, Backend, Provisioner, ResourceToDataOptions, Locals, Import, ImportArgs, VariableArgs, ModuleArgs, OutputArgs, Moved, MovedArgs, RemovedArgs, Removed } from './blocks';
import { TerraformArgs, Util } from './utils';

/**
Expand Down Expand Up @@ -313,6 +313,32 @@ export class TerraformGenerator {
return block;
}

/**
* Add moved into Terraform.
*
* Refer to Terraform documentation on what can be put as type & arguments.
*
* @param args arguments
*/
moved(args: MovedArgs): Moved {
const block = new Moved(args);
this.addBlocks(block);
return block;
}

/**
* Add removed into Terraform.
*
* Refer to Terraform documentation on what can be put as type & arguments.
*
* @param args arguments
*/
removed(args: RemovedArgs): Removed {
const block = new Removed(args);
this.addBlocks(block);
return block;
}

/**
* Add variable values into Terraform.
*
Expand Down
37 changes: 37 additions & 0 deletions src/blocks/Moved.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Argument, Attribute } from '../arguments';
import { Util } from '../utils';
import { Block, Resource } from '.';

/**
* @category Block
*/
export interface MovedArgs {
from: Argument;
to: Argument | Resource;
}

/**
* @category Block
*/
export class Moved extends Block<MovedArgs> {

/**
* Construct moved.
*
* Refer to Terraform documentation on what can be put as arguments.
*
* @param args arguments
*/
constructor(args: MovedArgs) {
super('moved', [], args);
}

override asArgument(): Argument {
throw Util.inaccessibleMethod();
}

override attr(_name: string): Attribute {
throw Util.inaccessibleMethod();
}

}
39 changes: 39 additions & 0 deletions src/blocks/Removed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Argument, Attribute } from '../arguments';
import { Util } from '../utils';
import { Block } from '.';

/**
* @category Block
*/
export interface RemovedArgs {
from: Argument;
lifecycle: {
destroy: boolean;
};
}

/**
* @category Block
*/
export class Removed extends Block<RemovedArgs> {

/**
* Construct removed.
*
* Refer to Terraform documentation on what can be put as arguments.
*
* @param args arguments
*/
constructor(args: RemovedArgs) {
super('removed', [], args);
}

override asArgument(): Argument {
throw Util.inaccessibleMethod();
}

override attr(_name: string): Attribute {
throw Util.inaccessibleMethod();
}

}
2 changes: 2 additions & 0 deletions src/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ export * from './Data';
export * from './Import';
export * from './Locals';
export * from './Module';
export * from './Moved';
export * from './Output';
export * from './Provider';
export * from './Provisioner';
export * from './Removed';
export * from './Resource';
export * from './Variable';
23 changes: 23 additions & 0 deletions test/blocks/Moved.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { arg } from '../../src/arguments';
import { Moved, Resource } from '../../src/blocks';

test('To argument', () => {
const moved = new Moved({
from: arg('resource.a'),
to: arg('resource.b')
});
expect(moved.toTerraform()).toMatchSnapshot();
expect(() => moved.asArgument()).toThrow();
expect(() => moved.attr('attr')).toThrow();
});

test('To resource', () => {
const resource = new Resource('resource', 'b');
const moved = new Moved({
from: arg('resource.a'),
to: resource
});
expect(moved.toTerraform()).toMatchSnapshot();
expect(() => moved.asArgument()).toThrow();
expect(() => moved.attr('attr')).toThrow();
});
14 changes: 14 additions & 0 deletions test/blocks/Removed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { arg } from '../../src/arguments';
import { Removed } from '../../src/blocks';

test('Removed', () => {
const moved = new Removed({
from: arg('resource.a'),
lifecycle: {
destroy: false
}
});
expect(moved.toTerraform()).toMatchSnapshot();
expect(() => moved.asArgument()).toThrow();
expect(() => moved.attr('attr')).toThrow();
});
19 changes: 19 additions & 0 deletions test/blocks/__snapshots__/Moved.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`To argument 1`] = `
"moved{
from = resource.a
to = resource.b
}
"
`;

exports[`To resource 1`] = `
"moved{
from = resource.a
to = resource.b
}
"
`;
12 changes: 12 additions & 0 deletions test/blocks/__snapshots__/Removed.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Removed 1`] = `
"removed{
from = resource.a
lifecycle {
destroy = false
}
}
"
`;
14 changes: 13 additions & 1 deletion test/tfg/Others.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { arg, map } from '../../src/arguments';
import { Argument, arg, map } from '../../src/arguments';
import { Provisioner } from '../../src/blocks';
import { TerraformGenerator } from '../../src/TerraformGenerator';

Expand Down Expand Up @@ -75,6 +75,18 @@ const createTerraformGenerator = (): TerraformGenerator => {
line4
`);

tfg.moved({
from: new Argument('resource.a'),
to: resource
});

tfg.removed({
from: new Argument('resource.b'),
lifecycle: {
destroy: false
}
});

const tfg2 = new TerraformGenerator();
tfg2.resource('tfg2', 'tfg2', {
tfg2: 'tfg2'
Expand Down
Loading

0 comments on commit a09f737

Please sign in to comment.