terraform-generator
Advanced tools
Comparing version 0.1.12 to 0.2.0
@@ -1,8 +0,7 @@ | ||
import TerraformGenerator, { Attribute, Argument } from '../..'; | ||
import { TerraformVersion, Attribute, Argument } from '../..'; | ||
export default abstract class Block { | ||
protected readonly tfGenerator: TerraformGenerator; | ||
readonly blockType: string; | ||
readonly blockNames: string[]; | ||
private readonly arguments; | ||
constructor(tfGenerator: TerraformGenerator, type: string, names: string[], args?: object); | ||
constructor(type: string, names: string[], args: object); | ||
getArguments(): object; | ||
@@ -13,3 +12,3 @@ getArgument(key: string): any; | ||
deleteArgument(key: string): Block; | ||
toTerraform(): string; | ||
toTerraform(version: TerraformVersion): string; | ||
abstract asArgument(): Argument; | ||
@@ -16,0 +15,0 @@ abstract getAttribute(name: string): Attribute; |
@@ -5,3 +5,3 @@ "use strict"; | ||
class Block { | ||
constructor(tfGenerator, type, names, args) { | ||
constructor(type, names, args) { | ||
this.validateIdentifier(type); | ||
@@ -14,9 +14,5 @@ if (names.length === 0) { | ||
}); | ||
this.tfGenerator = tfGenerator; | ||
this.blockType = type; | ||
this.blockNames = names; | ||
this.arguments = args ? args : {}; | ||
if (tfGenerator) { | ||
tfGenerator.addBlock(this); | ||
} | ||
} | ||
@@ -43,3 +39,3 @@ getArguments() { | ||
} | ||
toTerraform() { | ||
toTerraform(version) { | ||
let str = this.blockType; | ||
@@ -50,3 +46,3 @@ this.blockNames.forEach(name => { | ||
str += '{\n'; | ||
str += this.argumentsToString(this.arguments); | ||
str += this.argumentsToString(version, this.arguments); | ||
str += '}\n\n'; | ||
@@ -72,10 +68,10 @@ return str; | ||
} | ||
argumentsToString(args) { | ||
argumentsToString(version, args) { | ||
let str = ''; | ||
for (const key in args) { | ||
str += this.argumentToString(key, args[key]); | ||
str += this.argumentToString(version, key, args[key]); | ||
} | ||
return str; | ||
} | ||
argumentToString(key, value) { | ||
argumentToString(version, key, value) { | ||
try { | ||
@@ -102,3 +98,3 @@ if (value == null) { | ||
value.forEach(element => { | ||
str += `${key}${operator}${this.argumentValueToString(element)}\n`; | ||
str += `${key}${operator}${this.argumentValueToString(version, element)}\n`; | ||
}); | ||
@@ -109,3 +105,3 @@ } | ||
else { | ||
return `${key}${operator}${this.argumentValueToString(value)}\n`; | ||
return `${key}${operator}${this.argumentValueToString(version, value)}\n`; | ||
} | ||
@@ -118,5 +114,5 @@ } | ||
; | ||
argumentValueToString(value) { | ||
argumentValueToString(version, value) { | ||
if (value instanceof Block) { | ||
return this.argumentValueToString(value.asArgument()); | ||
return this.argumentValueToString(version, value.asArgument()); | ||
} | ||
@@ -127,3 +123,3 @@ else if (value instanceof __1.Argument) { | ||
} | ||
if (this.tfGenerator && this.tfGenerator.options.version === '0.11') { | ||
if (version === '0.11') { | ||
return `"\${${value.toTerraform()}}"`; | ||
@@ -136,3 +132,3 @@ } | ||
else if (value instanceof __1.Map) { | ||
return this.argumentValueToString(value.args); | ||
return this.argumentValueToString(version, value.args); | ||
} | ||
@@ -146,3 +142,3 @@ else if (['string', 'number', 'boolean'].indexOf(typeof value) >= 0) { | ||
value.forEach(element => { | ||
str += `${this.argumentValueToString(element)},\n`; | ||
str += `${this.argumentValueToString(version, element)},\n`; | ||
}); | ||
@@ -155,3 +151,3 @@ str += ']'; | ||
for (const key in value) { | ||
str += this.argumentToString(key, value[key]); | ||
str += this.argumentToString(version, key, value[key]); | ||
} | ||
@@ -158,0 +154,0 @@ str += '}'; |
@@ -1,8 +0,8 @@ | ||
import TerraformGenerator, { Block, Argument, Attribute } from '../..'; | ||
import { Block, Argument, Attribute } from '../..'; | ||
export default class DataSource extends Block { | ||
readonly type: string; | ||
readonly name: string; | ||
constructor(tfGenerator: TerraformGenerator, type: string, name: string, args?: object); | ||
constructor(type: string, name: string, args: object); | ||
asArgument(): Argument; | ||
getAttribute(name: string): Attribute; | ||
} |
@@ -5,4 +5,4 @@ "use strict"; | ||
class DataSource extends __1.Block { | ||
constructor(tfGenerator, type, name, args) { | ||
super(tfGenerator, 'data', [type, name], args); | ||
constructor(type, name, args) { | ||
super('data', [type, name], args); | ||
this.type = type; | ||
@@ -9,0 +9,0 @@ this.name = name; |
@@ -1,7 +0,7 @@ | ||
import TerraformGenerator, { Block, Argument, Attribute } from '../..'; | ||
import { Block, Argument, Attribute } from '../..'; | ||
export default class Module extends Block { | ||
readonly name: string; | ||
constructor(tfGenerator: TerraformGenerator, name: string, args?: object); | ||
constructor(name: string, args: object); | ||
asArgument(): Argument; | ||
getAttribute(name: string): Attribute; | ||
} |
@@ -5,4 +5,4 @@ "use strict"; | ||
class Module extends __1.Block { | ||
constructor(tfGenerator, name, args) { | ||
super(tfGenerator, 'module', [name], args); | ||
constructor(name, args) { | ||
super('module', [name], args); | ||
this.name = name; | ||
@@ -9,0 +9,0 @@ } |
@@ -1,7 +0,7 @@ | ||
import TerraformGenerator, { Block, Argument, Attribute } from '../..'; | ||
import { Block, Argument, Attribute } from '../..'; | ||
export default class Output extends Block { | ||
readonly name: string; | ||
constructor(tfGenerator: TerraformGenerator, name: string, args: object); | ||
constructor(name: string, args: object); | ||
asArgument(): Argument; | ||
getAttribute(name: string): Attribute; | ||
} |
@@ -5,4 +5,4 @@ "use strict"; | ||
class Output extends __1.Block { | ||
constructor(tfGenerator, name, args) { | ||
super(tfGenerator, 'output', [name], args); | ||
constructor(name, args) { | ||
super('output', [name], args); | ||
this.name = name; | ||
@@ -9,0 +9,0 @@ } |
@@ -1,7 +0,7 @@ | ||
import TerraformGenerator, { Block, Argument, Attribute } from '../..'; | ||
import { Block, Argument, Attribute } from '../..'; | ||
export default class Provider extends Block { | ||
readonly name: string; | ||
constructor(tfGenerator: TerraformGenerator, name: string, args: object); | ||
constructor(name: string, args: object); | ||
asArgument(): Argument; | ||
getAttribute(name: string): Attribute; | ||
} |
@@ -5,4 +5,4 @@ "use strict"; | ||
class Provider extends __1.Block { | ||
constructor(tfGenerator, name, args) { | ||
super(tfGenerator, 'provider', [name], args); | ||
constructor(name, args) { | ||
super('provider', [name], args); | ||
this.name = name; | ||
@@ -9,0 +9,0 @@ } |
@@ -1,8 +0,8 @@ | ||
import TerraformGenerator, { Block, Argument, Attribute } from '../..'; | ||
import { Block, Argument, Attribute } from '../..'; | ||
export default class Resource extends Block { | ||
readonly type: string; | ||
readonly name: string; | ||
constructor(tfGenerator: TerraformGenerator, type: string, name: string, args?: object); | ||
constructor(type: string, name: string, args: object); | ||
asArgument(): Argument; | ||
getAttribute(name: string): Attribute; | ||
} |
@@ -5,4 +5,4 @@ "use strict"; | ||
class Resource extends __1.Block { | ||
constructor(tfGenerator, type, name, args) { | ||
super(tfGenerator, 'resource', [type, name], args); | ||
constructor(type, name, args) { | ||
super('resource', [type, name], args); | ||
this.type = type; | ||
@@ -9,0 +9,0 @@ this.name = name; |
@@ -1,7 +0,7 @@ | ||
import TerraformGenerator, { Block, Argument, Attribute } from '../..'; | ||
import { Block, Argument, Attribute } from '../..'; | ||
export default class Variable extends Block { | ||
readonly name: string; | ||
constructor(tfGenerator: TerraformGenerator, name: string, args?: object); | ||
constructor(name: string, args: object); | ||
asArgument(): Argument; | ||
getAttribute(name: string): Attribute; | ||
} |
@@ -5,4 +5,4 @@ "use strict"; | ||
class Variable extends __1.Block { | ||
constructor(tfGenerator, name, args) { | ||
super(tfGenerator, 'variable', [name], args); | ||
constructor(name, args) { | ||
super('variable', [name], args); | ||
this.name = name; | ||
@@ -9,0 +9,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { Block } from '..'; | ||
import { Block, Resource, DataSource, Module, Output, Provider, Variable } from '..'; | ||
export declare type TerraformVersion = '0.11' | '0.12'; | ||
@@ -12,2 +12,8 @@ export interface TerraformGeneratorOptions { | ||
generate(): string; | ||
addProvider(name: string, args: object): Provider; | ||
addResource(type: string, name: string, args: object): Resource; | ||
addDataSource(type: string, name: string, args: object): DataSource; | ||
addModule(name: string, args: object): Module; | ||
addOutput(name: string, args: object): Output; | ||
addVariable(name: string, args: object): Variable; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const __1 = require(".."); | ||
class TerraformGenerator { | ||
@@ -15,7 +16,37 @@ constructor(options) { | ||
this.blocks.forEach(block => { | ||
str += block.toTerraform(); | ||
str += block.toTerraform(this.options.version); | ||
}); | ||
return str; | ||
} | ||
addProvider(name, args) { | ||
const block = new __1.Provider(name, args); | ||
this.addBlock(block); | ||
return block; | ||
} | ||
addResource(type, name, args) { | ||
const block = new __1.Resource(type, name, args); | ||
this.addBlock(block); | ||
return block; | ||
} | ||
addDataSource(type, name, args) { | ||
const block = new __1.DataSource(type, name, args); | ||
this.addBlock(block); | ||
return block; | ||
} | ||
addModule(name, args) { | ||
const block = new __1.Module(name, args); | ||
this.addBlock(block); | ||
return block; | ||
} | ||
addOutput(name, args) { | ||
const block = new __1.Output(name, args); | ||
this.addBlock(block); | ||
return block; | ||
} | ||
addVariable(name, args) { | ||
const block = new __1.Variable(name, args); | ||
this.addBlock(block); | ||
return block; | ||
} | ||
} | ||
exports.default = TerraformGenerator; |
{ | ||
"name": "terraform-generator", | ||
"version": "0.1.12", | ||
"version": "0.2.0", | ||
"author": "Ah Zhe", | ||
@@ -5,0 +5,0 @@ "description": "Generate Terraform plan using Node.js.", |
@@ -39,3 +39,3 @@ # **terraform-generator** | ||
```javascript | ||
const tfGenerator = new TerraformGenerator({ version: '0.12' }); | ||
const tfg = new TerraformGenerator({ version: '0.12' }); | ||
``` | ||
@@ -47,3 +47,3 @@ | ||
```javascript | ||
new Provider(tfGenerator, 'aws', { | ||
tfg.addProvider('aws', { | ||
region: 'ap-southeast-1', | ||
@@ -53,3 +53,3 @@ profile: 'example' | ||
const vpc = new Resource(tfGenerator, 'aws_vpc', 'vpc', { | ||
const vpc = tfg.addResource('aws_vpc', 'vpc', { | ||
cidr_block: '172.88.0.0/16' | ||
@@ -157,6 +157,6 @@ }); | ||
// Start writing Terraform plan | ||
const tfGenerator = new TerraformGenerator({ version: '0.12' }); | ||
const tfg = new TerraformGenerator({ version: '0.12' }); | ||
// Configure provider | ||
new Provider(tfGenerator, 'aws', { | ||
tfg.addProvider('aws', { | ||
region: 'ap-southeast-1', | ||
@@ -167,3 +167,3 @@ profile: 'example' | ||
// Find VPC by name | ||
const vpc = new DataSource(tfGenerator, 'aws_vpc', 'vpc', { | ||
const vpc = new DataSource('aws_vpc', 'vpc', { | ||
filter: [{ | ||
@@ -185,3 +185,3 @@ name: 'tag:Name', | ||
const name = `${tier.name}${i}`; | ||
const subnet = new Resource(tfGenerator, 'aws_subnet', `subnet_${name}`, { | ||
const subnet = tfg.addResource('aws_subnet', `subnet_${name}`, { | ||
vpc_id: vpc.getAttribute('id'), | ||
@@ -193,7 +193,7 @@ cidr_block: cidr, | ||
subnets[tier.name].push(subnet); | ||
}) | ||
}); | ||
}); | ||
// Output all subnet ids | ||
new Output(tfGenerator, 'subnets', { | ||
tfg.addOutput('subnets', { | ||
value: new Map({ | ||
@@ -208,3 +208,3 @@ webSubnets: subnets.web.map(subnet => subnet.getAttribute('id')), | ||
const outputPath = path.join('output', configs.env, 'subnets', 'terraform.tf'); | ||
fs.writeFileSync(outputPath, tfGenerator.generate()); | ||
fs.writeFileSync(outputPath, tfg.generate()); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23081
493