Skip to content

Commit

Permalink
Lib improvements; simplified kite errors (#11)
Browse files Browse the repository at this point in the history
* Lib improvements

- Added error module
- Added file checks

* Simplified Kite errors
  • Loading branch information
vshatravenko authored and Louis committed Sep 13, 2017
1 parent bcfcbc5 commit 0cef996
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/kite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require 'kite/version'
require 'kite/helpers'
require 'kite/error'

require 'kite/base'
require 'kite/core'
Expand Down
4 changes: 2 additions & 2 deletions lib/kite/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def new(cloud_name)
desc "generate", "Generate Cloud IaC from configuration"
def generate()
say "Generating Cloud #{ options[:cloud] } IaC", :green
@values = YAML.load(File.read('config/cloud.yml'))
@values = parse_cloud_config

case options[:cloud]
when 'aws'
Expand Down Expand Up @@ -54,7 +54,7 @@ def generate()
desc 'render MANIFEST', 'Render manifest file from configuration and Terraform output'
def render(manifest)
say "Rendering #{ manifest } manifest", :green
@values = YAML.load(File.read('config/cloud.yml'))
@values = parse_cloud_config
@tf_output = parse_tf_state('terraform/terraform.tfstate')

case manifest
Expand Down
2 changes: 2 additions & 0 deletions lib/kite/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Kite::Error < Thor::Error
end
24 changes: 23 additions & 1 deletion lib/kite/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
module Kite::Helpers
# Check config/cloud.yml file to be complete
def check_cloud_config(config)
raise Kite::Error, 'The config/cloud.yml is not filled out!' unless config.find { |key, hash| hash.find { |k, v| v.nil? } }.nil?
end

# Check if Terraform IaC was applied
def check_terraform_applied
raise Kite::Error, 'Did you terraform apply? terraform.tfstate is missing!' unless File.file? "terraform/terraform.tfstate"
end

# Parse Terraform .tfstate file, returning the output hash
def parse_tf_state(path)
tf_state = YAML.load(File.open(path))
check_terraform_applied

tf_state = YAML.load(File.read(path))
tf_output = tf_state["modules"].first["outputs"]
tf_output.map { |k, v| tf_output[k] = v["value"] }

tf_output
end

# Parse config/cloud.yml, returning the output hash
def parse_cloud_config
cloud_config = YAML.load(File.read('config/cloud.yml'))
check_cloud_config(cloud_config)

cloud_config
end

end

0 comments on commit 0cef996

Please sign in to comment.