terraform-generator
Advanced tools
Comparing version 0.3.2 to 0.4.0
export default class Argument { | ||
readonly args: (string | Argument)[]; | ||
constructor(...args: (string | Argument)[]); | ||
readonly asIs: boolean; | ||
constructor(...args: (boolean | string | Argument)[]); | ||
toTerraform(): string; | ||
} |
@@ -5,2 +5,10 @@ "use strict"; | ||
constructor(...args) { | ||
this.asIs = false; | ||
if (args.length > 0 && typeof args[0] === 'boolean') { | ||
this.asIs = args[0]; | ||
args.splice(0, 1); | ||
} | ||
if (args.length < 1 || args.filter(arg => arg == null || typeof arg === 'boolean').length > 0) { | ||
throw new Error(`Invalid argument: ${args}`); | ||
} | ||
this.args = args; | ||
@@ -7,0 +15,0 @@ } |
import { Argument, Block } from '..'; | ||
export default class Attribute extends Argument { | ||
constructor(block: Block, name: string); | ||
private static constructArgument; | ||
} |
@@ -6,5 +6,14 @@ "use strict"; | ||
constructor(block, name) { | ||
super(`${block.asArgument().toTerraform()}.${name}`); | ||
super(Attribute.constructArgument(block, name)); | ||
} | ||
static constructArgument(block, name) { | ||
if (!block) { | ||
throw new Error('Attribute block cannot be null.'); | ||
} | ||
if (!name || !name.trim()) { | ||
throw new Error('Attribute name cannot be empty.'); | ||
} | ||
return `${block.asArgument().toTerraform()}.${name.trim()}`; | ||
} | ||
} | ||
exports.default = Attribute; |
@@ -6,5 +6,5 @@ "use strict"; | ||
constructor(str) { | ||
super(`<<EOT\n${str}\nEOT`); | ||
super(true, `<<EOT\n${str}\nEOT`); | ||
} | ||
} | ||
exports.default = Heredoc; |
@@ -16,6 +16,2 @@ import { TerraformVersion, Attribute, Argument } from '..'; | ||
private validateIdentifier; | ||
private isObjectArgument; | ||
private argumentsToString; | ||
private argumentToString; | ||
private argumentValueToString; | ||
} |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const __1 = require(".."); | ||
const __1 = __importDefault(require("..")); | ||
class Block { | ||
constructor(type, names, args) { | ||
this.validateIdentifier(type); | ||
if (names.length === 0) { | ||
if (names.length < 1) { | ||
throw new Error('Names cannot be empty.'); | ||
@@ -43,3 +46,3 @@ } | ||
str += '{\n'; | ||
str += this.argumentsToString(version, this.arguments); | ||
str += __1.default.argumentsToString(version, this.arguments); | ||
str += '}\n\n'; | ||
@@ -53,101 +56,3 @@ return str; | ||
} | ||
isObjectArgument(value) { | ||
if (['string', 'number', 'boolean'].indexOf(typeof value) >= 0 | ||
|| value instanceof Block || value instanceof __1.Argument || value instanceof __1.Map) { | ||
return false; | ||
} | ||
else if (typeof value === 'object') { | ||
return true; | ||
} | ||
else { | ||
throw new Error(`Invalid value: ${value}`); | ||
} | ||
} | ||
argumentsToString(version, args) { | ||
let str = ''; | ||
for (const key in args) { | ||
str += this.argumentToString(version, key, args[key]); | ||
} | ||
return str; | ||
} | ||
argumentToString(version, key, value) { | ||
try { | ||
if (value == null) { | ||
return ''; | ||
} | ||
let operator = ' = '; | ||
let isObjectArray = false; | ||
if (Array.isArray(value)) { | ||
if (value.length === 0 || this.isObjectArgument(value[0])) { | ||
operator = ' '; | ||
isObjectArray = true; | ||
} | ||
} | ||
else { | ||
if (this.isObjectArgument(value)) { | ||
operator = ' '; | ||
} | ||
} | ||
if (isObjectArray) { | ||
let str = ''; | ||
if (Array.isArray(value)) { | ||
value.forEach(element => { | ||
str += `${key}${operator}${this.argumentValueToString(version, element)}\n`; | ||
}); | ||
} | ||
return str; | ||
} | ||
else { | ||
return `${key}${operator}${this.argumentValueToString(version, value)}\n`; | ||
} | ||
} | ||
catch (err) { | ||
throw new Error(`Invalid value: ${key} = ${value}`); | ||
} | ||
} | ||
; | ||
argumentValueToString(version, value) { | ||
if (value instanceof Block) { | ||
return this.argumentValueToString(version, value.asArgument()); | ||
} | ||
else if (value instanceof __1.Argument) { | ||
if (value instanceof __1.Heredoc) { | ||
return value.toTerraform(); | ||
} | ||
if (version === '0.11') { | ||
return `"\${${value.toTerraform()}}"`; | ||
} | ||
else { | ||
return value.toTerraform(); | ||
} | ||
} | ||
else if (value instanceof __1.Map) { | ||
return this.argumentValueToString(version, value.args); | ||
} | ||
else if (['string', 'number', 'boolean'].indexOf(typeof value) >= 0) { | ||
return JSON.stringify(value); | ||
} | ||
else if (typeof value === 'object') { | ||
if (Array.isArray(value)) { | ||
let str = '[\n'; | ||
value.forEach(element => { | ||
str += `${this.argumentValueToString(version, element)},\n`; | ||
}); | ||
str += ']'; | ||
return str; | ||
} | ||
else { | ||
let str = '{\n'; | ||
for (const key in value) { | ||
str += this.argumentToString(version, key, value[key]); | ||
} | ||
str += '}'; | ||
return str; | ||
} | ||
} | ||
else { | ||
throw new Error(`Invalid value: ${value}`); | ||
} | ||
} | ||
} | ||
exports.default = Block; |
@@ -11,6 +11,8 @@ import { default as TerraformGenerator } from './TerraformGenerator'; | ||
export { default as Variable } from './blocks/Variable'; | ||
export { default as Backend } from './blocks/Backend'; | ||
export { default as Argument } from './arguments/Argument'; | ||
export { default as Attribute } from './arguments/Attribute'; | ||
export { default as Heredoc } from './arguments/Heredoc'; | ||
export { default as Function } from './arguments/Function'; | ||
export { default as Map } from './types/Map'; | ||
export { default as Utils } from './utils/Utils'; |
@@ -22,2 +22,4 @@ "use strict"; | ||
exports.Variable = Variable_1.default; | ||
var Backend_1 = require("./blocks/Backend"); | ||
exports.Backend = Backend_1.default; | ||
var Argument_1 = require("./arguments/Argument"); | ||
@@ -29,2 +31,4 @@ exports.Argument = Argument_1.default; | ||
exports.Heredoc = Heredoc_1.default; | ||
var Function_1 = require("./arguments/Function"); | ||
exports.Function = Function_1.default; | ||
var Map_1 = require("./types/Map"); | ||
@@ -31,0 +35,0 @@ exports.Map = Map_1.default; |
@@ -1,2 +0,2 @@ | ||
import { Block, Resource, DataSource, Module, Output, Provider, Variable } from '.'; | ||
import { Block, Resource, DataSource, Module, Output, Provider, Variable, Backend } from '.'; | ||
export declare type TerraformVersion = '0.11' | '0.12'; | ||
@@ -7,7 +7,12 @@ export interface TerraformGeneratorOptions { | ||
export default class TerraformGenerator { | ||
readonly options: TerraformGeneratorOptions; | ||
private readonly options; | ||
private readonly arguments; | ||
private readonly blocks; | ||
constructor(options: TerraformGeneratorOptions); | ||
constructor(options: TerraformGeneratorOptions, args?: object); | ||
addBlock(block: Block): TerraformGenerator; | ||
generate(): string; | ||
static argumentsToString(version: TerraformVersion, args: object): string; | ||
private static isObjectArgument; | ||
private static argumentToString; | ||
private static argumentValueToString; | ||
addProvider(name: string, args: object): Provider; | ||
@@ -19,2 +24,3 @@ addResource(type: string, name: string, args: object): Resource; | ||
addVariable(name: string, args: object): Variable; | ||
addBackend(type: string, args: object): Backend; | ||
} |
@@ -5,5 +5,6 @@ "use strict"; | ||
class TerraformGenerator { | ||
constructor(options) { | ||
constructor(options, args) { | ||
this.blocks = []; | ||
this.options = options; | ||
this.arguments = args; | ||
} | ||
@@ -16,7 +17,117 @@ addBlock(block) { | ||
let str = ''; | ||
if (this.arguments || this.blocks.filter(block => block instanceof _1.Backend).length > 0) { | ||
str += 'terraform {\n'; | ||
str += TerraformGenerator.argumentsToString(this.options.version, this.arguments); | ||
this.blocks.forEach(block => { | ||
if (block instanceof _1.Backend) { | ||
str += block.toTerraform(this.options.version); | ||
} | ||
}); | ||
str += '}\n\n'; | ||
} | ||
this.blocks.forEach(block => { | ||
str += block.toTerraform(this.options.version); | ||
if (!(block instanceof _1.Backend)) { | ||
str += block.toTerraform(this.options.version); | ||
} | ||
}); | ||
return str; | ||
} | ||
static argumentsToString(version, args) { | ||
let str = ''; | ||
for (const key in args) { | ||
str += this.argumentToString(version, key, args[key]); | ||
} | ||
return str; | ||
} | ||
static isObjectArgument(value) { | ||
if (['string', 'number', 'boolean'].indexOf(typeof value) >= 0 | ||
|| value instanceof _1.Block || value instanceof _1.Argument || value instanceof _1.Map) { | ||
return false; | ||
} | ||
else if (typeof value === 'object') { | ||
return true; | ||
} | ||
else { | ||
throw new Error(`Invalid value: ${value}`); | ||
} | ||
} | ||
static argumentToString(version, key, value) { | ||
try { | ||
if (value == null) { | ||
return ''; | ||
} | ||
let operator = ' = '; | ||
let isObjectArray = false; | ||
if (Array.isArray(value)) { | ||
if (value.length === 0 || this.isObjectArgument(value[0])) { | ||
operator = ' '; | ||
isObjectArray = true; | ||
} | ||
} | ||
else { | ||
if (this.isObjectArgument(value)) { | ||
operator = ' '; | ||
} | ||
} | ||
if (isObjectArray) { | ||
let str = ''; | ||
if (Array.isArray(value)) { | ||
value.forEach(element => { | ||
str += `${key}${operator}${this.argumentValueToString(version, element)}\n`; | ||
}); | ||
} | ||
return str; | ||
} | ||
else { | ||
return `${key}${operator}${this.argumentValueToString(version, value)}\n`; | ||
} | ||
} | ||
catch (err) { | ||
throw new Error(`Invalid value: ${key} = ${value}`); | ||
} | ||
} | ||
; | ||
static argumentValueToString(version, value) { | ||
if (value instanceof _1.Block) { | ||
return this.argumentValueToString(version, value.asArgument()); | ||
} | ||
else if (value instanceof _1.Argument) { | ||
if (value.asIs) { | ||
return value.toTerraform(); | ||
} | ||
if (version === '0.11') { | ||
return `"\${${value.toTerraform()}}"`; | ||
} | ||
else { | ||
return value.toTerraform(); | ||
} | ||
} | ||
else if (value instanceof _1.Map) { | ||
return this.argumentValueToString(version, value.args); | ||
} | ||
else if (['string', 'number', 'boolean'].indexOf(typeof value) >= 0) { | ||
return JSON.stringify(value); | ||
} | ||
else if (typeof value === 'object') { | ||
if (Array.isArray(value)) { | ||
let str = '[\n'; | ||
value.forEach(element => { | ||
str += `${this.argumentValueToString(version, element)},\n`; | ||
}); | ||
str += ']'; | ||
return str; | ||
} | ||
else { | ||
let str = '{\n'; | ||
for (const key in value) { | ||
str += this.argumentToString(version, key, value[key]); | ||
} | ||
str += '}'; | ||
return str; | ||
} | ||
} | ||
else { | ||
throw new Error(`Invalid value: ${value}`); | ||
} | ||
} | ||
addProvider(name, args) { | ||
@@ -52,3 +163,8 @@ const block = new _1.Provider(name, args); | ||
} | ||
addBackend(type, args) { | ||
const block = new _1.Backend(type, args); | ||
this.addBlock(block); | ||
return block; | ||
} | ||
} | ||
exports.default = TerraformGenerator; |
{ | ||
"name": "terraform-generator", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"author": "Ah Zhe", | ||
@@ -22,2 +22,3 @@ "description": "Generate Terraform plan using Node.js.", | ||
"lint": "eslint src/**/*.ts test/**/*.ts", | ||
"lint-mac": "eslint 'src/**/*.ts' 'test/**/*.ts'", | ||
"test": "jest", | ||
@@ -24,0 +25,0 @@ "tryout": "ts-node tryout.ts", |
@@ -49,6 +49,8 @@ # **terraform-generator** | ||
```javascript | ||
const tfg = new TerraformGenerator({ version: '0.12' }); | ||
const tfg = new TerraformGenerator({ version: '0.12' }, { | ||
required_version: '>= 0.12' | ||
}); | ||
``` | ||
### **Block** | ||
### **Blocks** | ||
Block's arguments are not typed, please refer to official Terraform documentation on what arguments can be supplied. | ||
@@ -67,3 +69,3 @@ | ||
### **Argument Types** | ||
### **Arguments** | ||
```javascript | ||
@@ -98,5 +100,8 @@ { | ||
blockAttribute: block.getAttribute('attrName'), | ||
asIsArg: new Argument(true, 'AS IS'), // it will be printed as is, without extra symbol, quotes and whatnot, regardless of Terraform version | ||
heredoc: new Heredoc(`line1 | ||
line2 | ||
line3`); | ||
line3`), | ||
function1: new Function('max', 5, 12, 19), | ||
function2: new Function('sort', 'a', block.getAttribute('attrName'), 'c'), | ||
custom1: new Argument('max(5, 12, 9)'), | ||
@@ -107,3 +112,3 @@ custom2: new Argument('sort("a", ', block.getAttribute('attrName'), ', "c")') | ||
### **Attribute** | ||
### **Attributes** | ||
```javascript | ||
@@ -110,0 +115,0 @@ block.getAttribute('id') // block id, string |
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
28812
34
639
230