Class: Terraformer::Terraform
- Inherits:
-
Object
- Object
- Terraformer::Terraform
- Defined in:
- lib/terraformer/terraform.rb
Class Method Summary collapse
- .execute(data, klass_name, options) ⇒ Object
- .parse_json_file(file_path) ⇒ Object
- .tf(generated_tf, klass_name, tf_path) ⇒ Object
- .tfstate(generated_tfstate, tfstate_path) ⇒ Object
- .tfstate_skeleton ⇒ Object
Class Method Details
.execute(data, klass_name, options) ⇒ Object
10 11 12 13 |
# File 'lib/terraformer/terraform.rb', line 10 def execute(data, klass_name, ) tf(data[:tf], klass_name, [:name]) tfstate(data[:tfstate], [:merge]) end |
.parse_json_file(file_path) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/terraformer/terraform.rb', line 53 def parse_json_file(file_path) file_content = File.read(file_path) JSON.parse(file_content) rescue JSON::ParserError raise TerraformInvalidTfstateFile, "Unable to parse: #{file_path}." end |
.tf(generated_tf, klass_name, tf_path) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/terraformer/terraform.rb', line 15 def tf(generated_tf, klass_name, tf_path) tf_file_name = "#{klass_name.split('::')[-1].downcase}.tf" tf_file_name = tf_path unless tf_path.nil? if File.file?(tf_file_name) UserInput.ask("File: #{tf_file_name} already exists. Overwrite?", "Y", "n") end File.open(tf_file_name, "w") do |f| f.write(generated_tf) end end |
.tfstate(generated_tfstate, tfstate_path) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/terraformer/terraform.rb', line 28 def tfstate(generated_tfstate, tfstate_path) unless tfstate_path.nil? if File.file?(tfstate_path) tfstate = parse_json_file(tfstate_path) else raise TerraformFileNotFound, "File not found while trying to merge state file with: #{tfstate_path}." end else tfstate_path = "terraform.tfstate" if File.file?(tfstate_path) UserInput.ask("File: #{tfstate_path} already exists and the 'merge' option was not passed. Overwrite?", "Y", "n") end tfstate = tfstate_skeleton end tfstate["serial"] = tfstate["serial"] + 1 tfstate["modules"][0]["resources"] = tfstate["modules"][0]["resources"].merge(generated_tfstate) state_file = JSON.pretty_generate(tfstate) File.open(tfstate_path, "w+") do |f| f.write(state_file) f.flush end end |
.tfstate_skeleton ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/terraformer/terraform.rb', line 60 def tfstate_skeleton { "version" => 3, "terraform_version" => "0.7.3", "serial" => 0, "lineage" => SecureRandom.uuid, "modules" => [ { "path" => [ "root" ], "outputs" => {}, "resources" => {}, "depends_on" => [] } ] } end |