UNPKG

terraform-generator

Version:

Generate Terraform configurations with Node.js.

338 lines (337 loc) 11.6 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _TerraformGenerator_instances, _TerraformGenerator_arguments, _TerraformGenerator_blocks, _TerraformGenerator_variables, _TerraformGenerator_generateTf, _TerraformGenerator_generateTfvars; Object.defineProperty(exports, "__esModule", { value: true }); exports.TerraformGenerator = void 0; const child_process_1 = __importDefault(require("child_process")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const shelljs_1 = __importDefault(require("shelljs")); const blocks_1 = require("./blocks"); const utils_1 = require("./utils"); /** * @category TerraformGenerator */ class TerraformGenerator { /** * Construct Terraform generator. * * Refer to Terraform documentation on what can be put as arguments. * * @param args arguments */ constructor(args) { _TerraformGenerator_instances.add(this); _TerraformGenerator_arguments.set(this, void 0); _TerraformGenerator_blocks.set(this, []); _TerraformGenerator_variables.set(this, {}); __classPrivateFieldSet(this, _TerraformGenerator_arguments, args, "f"); } /** * Generate Terraform configuration as string. */ generate() { return { tf: __classPrivateFieldGet(this, _TerraformGenerator_instances, "m", _TerraformGenerator_generateTf).call(this), tfvars: Object.keys(__classPrivateFieldGet(this, _TerraformGenerator_variables, "f")).length > 0 ? __classPrivateFieldGet(this, _TerraformGenerator_instances, "m", _TerraformGenerator_generateTfvars).call(this) : undefined }; } /** * Write Terraform configuration to a file. * * @param options options */ write(options) { if (!options) { options = {}; } if (!options.dir) { options.dir = '.'; } if (!options.tfFilename) { options.tfFilename = 'terraform.tf'; } if (!options.tfFilename.endsWith('.tf')) { options.tfFilename += '.tf'; } if (!options.tfvarsFilename) { options.tfvarsFilename = 'terraform.tfvars'; } if (!options.tfvarsFilename.endsWith('.tfvars')) { options.tfvarsFilename += '.tfvars'; } if (options.format === true) { options.format = 'terraform'; } const result = this.generate(); shelljs_1.default.mkdir('-p', options.dir); fs_1.default.writeFileSync(path_1.default.join(options.dir, options.tfFilename), result.tf); if (result.tfvars) { fs_1.default.writeFileSync(path_1.default.join(options.dir, options.tfvarsFilename), result.tfvars); } if (options.format) { child_process_1.default.execSync(`${options.format} fmt`, { cwd: options.dir }); } } /** * Add blocks into Terraform. * * @param blocks blocks */ addBlocks(...blocks) { blocks.forEach(block => __classPrivateFieldGet(this, _TerraformGenerator_blocks, "f").push(block)); return this; } /** * Add comment into Terraform. * * @param comment comment */ comment(comment) { const block = new blocks_1.Comment(comment); this.addBlocks(block); return block; } /** * Add provider into Terraform. * * Refer to Terraform documentation on what can be put as type & arguments. * * @param type type * @param args arguments */ provider(type, args) { const block = new blocks_1.Provider(type, args); this.addBlocks(block); return block; } /** * Add resource into Terraform. * * Refer to Terraform documentation on what can be put as type & arguments. * * @param type type * @param name name * @param args arguments * @param provisioners provisioners */ resource(type, name, args, provisioners) { const block = new blocks_1.Resource(type, name, args, provisioners); this.addBlocks(block); return block; } /** * Convert resource into data source and add it into Terraform. * * @param resource resource * @param options options * @param argNames names of resource arguments to be converted into data source arguments; * use array for name mapping, position 0 = original resource's argument name, position 1 = mapped data source's argument name * @param args extra arguments */ dataFromResource(resource, options, argNames, args) { const block = resource.toData(options, argNames, args); this.addBlocks(block); return block; } /** * Add data source into Terraform. * * Refer to Terraform documentation on what can be put as type & arguments. * * @param type type * @param name name * @param args arguments */ data(type, name, args) { const block = new blocks_1.Data(type, name, args); this.addBlocks(block); return block; } /** * Add module into Terraform. * * Refer to Terraform documentation on what can be put as arguments. * * @param name name * @param args arguments */ module(name, args) { const block = new blocks_1.Module(name, args); this.addBlocks(block); return block; } /** * Add output into Terraform. * * Refer to Terraform documentation on what can be put as arguments. * * @param name name * @param args arguments */ output(name, args) { const block = new blocks_1.Output(name, args); this.addBlocks(block); return block; } /** * Add locals into Terraform. * * Refer to Terraform documentation on what can be put as arguments. * * @param args arguments */ locals(args) { const block = new blocks_1.Locals(args); this.addBlocks(block); return block; } /** * Add variable into Terraform. * * Refer to Terraform documentation on what can be put as arguments. * * @param name name * @param args arguments * @param value variable value */ variable(name, args, value) { const block = new blocks_1.Variable(name, args); this.addBlocks(block); if (value != null) { this.addVars({ [name]: value }); } return block; } /** * Add import into Terraform. * * Refer to Terraform documentation on what can be put as arguments. * * @param args arguments */ import(args) { const block = new blocks_1.Import(args); this.addBlocks(block); return block; } /** * Add backend into Terraform. * * Refer to Terraform documentation on what can be put as type & arguments. * * @param type type * @param args arguments */ backend(type, args) { const block = new blocks_1.Backend(type, args); this.addBlocks(block); return block; } /** * Add moved into Terraform. * * Refer to Terraform documentation on what can be put as type & arguments. * * @param args arguments */ moved(args) { const block = new blocks_1.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) { const block = new blocks_1.Removed(args); this.addBlocks(block); return block; } /** * Add variable values into Terraform. * * @param variables variables */ addVars(variables) { __classPrivateFieldSet(this, _TerraformGenerator_variables, { ...__classPrivateFieldGet(this, _TerraformGenerator_variables, "f"), ...variables }, "f"); return this; } /** * Merge this instance with other TerraformGenerator instances. * * @param tfgs other instances */ merge(...tfgs) { tfgs.forEach(tfg => { this.addBlocks(...tfg.getBlocks()); this.addVars(tfg.getVars()); }); return this; } /** * Get arguments. */ getArguments() { return __classPrivateFieldGet(this, _TerraformGenerator_arguments, "f"); } /** * Get blocks. */ getBlocks() { return __classPrivateFieldGet(this, _TerraformGenerator_blocks, "f"); } /** * Get variables. */ getVars() { return __classPrivateFieldGet(this, _TerraformGenerator_variables, "f"); } } exports.TerraformGenerator = TerraformGenerator; _TerraformGenerator_arguments = new WeakMap(), _TerraformGenerator_blocks = new WeakMap(), _TerraformGenerator_variables = new WeakMap(), _TerraformGenerator_instances = new WeakSet(), _TerraformGenerator_generateTf = function _TerraformGenerator_generateTf() { let str = ''; if (__classPrivateFieldGet(this, _TerraformGenerator_arguments, "f") || __classPrivateFieldGet(this, _TerraformGenerator_blocks, "f").filter(block => block.isInsideTerraformBlock()).length > 0) { str += 'terraform {\n'; str += utils_1.Util.argumentsToString(__classPrivateFieldGet(this, _TerraformGenerator_arguments, "f")); __classPrivateFieldGet(this, _TerraformGenerator_blocks, "f").forEach(block => { if (block.isInsideTerraformBlock()) { str += block.toTerraform(); } }); str += '}\n\n'; } __classPrivateFieldGet(this, _TerraformGenerator_blocks, "f").forEach(block => { if (!block.isInsideTerraformBlock()) { str += block.toTerraform(); } }); return utils_1.Util.unescape(str); }, _TerraformGenerator_generateTfvars = function _TerraformGenerator_generateTfvars() { let str = ''; Object.keys(__classPrivateFieldGet(this, _TerraformGenerator_variables, "f")).forEach(key => { str += utils_1.Util.argumentsToString({ [key]: __classPrivateFieldGet(this, _TerraformGenerator_variables, "f")[key] }); str += '\n'; }); return str; };