Last active
June 16, 2019 12:06
-
-
Save alex-harvey-z3q/19c625f13a8eb46b4d8aac0fbdc5a9f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'open3' | |
class TerraformTesting | |
@@terraform = "#{ENV['HOME']}/go/bin/terraform" | |
def eval(path, addr, mock_data) | |
command = "#{@@terraform} testing eval #{path} #{addr} -" | |
stdout, status = Open3.capture2(command, stdin_data: mock_data.to_json) | |
result_raw = JSON.parse(stdout) | |
return prepare_result(result_raw["value"], result_raw["type"]) | |
end | |
private | |
def prepare_result(value, type) | |
if value.nil? | |
return nil | |
end | |
if type.is_a?(Array) | |
case type[0] | |
when "object" | |
ret = Object.new | |
value.each do |k,v| | |
ret.singleton_class.instance_eval { attr_reader k.to_sym } | |
ret.instance_variable_set("@#{k}", prepare_result(v, type[1][k])) | |
end | |
return ret | |
when "tuple" | |
ret = [] | |
value.each_with_index do |v, i| | |
ret << prepare_result(v, type[1][i]) | |
end | |
return ret | |
when "list" | |
ret = [] | |
value.each do |v| | |
ret << prepare_result(v, type[1]) | |
end | |
return ret | |
when "map" | |
ret = {} | |
value.each do |k,v| | |
ret[k] = prepare_result(v, type[1]) | |
end | |
return ret | |
when "set" | |
ret = [] | |
value.each do |v| | |
ret << prepare_result(v, type[1]) | |
end | |
return ret | |
end | |
end | |
return value | |
end | |
end | |
mock_data = { | |
"variables": { | |
"instance_count": 1, | |
"ami": "ami-08589eca6dcc9b39c", | |
"instance_type": "t2.micro", | |
"ebs_block_device": [] | |
}, | |
"locals": { | |
"key_name": "default" | |
} | |
} | |
t = TerraformTesting.new | |
result = t.eval(".", "aws_instance.this", mock_data) | |
r = result[0] | |
puts "AMI: #{r.ami}" | |
puts "Instance type: #{r.instance_type}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment