Class: RubyTerraform::Client
- Inherits:
-
Object
- Object
- RubyTerraform::Client
- Defined in:
- lib/ruby_terraform/client.rb
Instance Method Summary collapse
- #apply(variables = {}, options = {}) ⇒ Object
- #destroy(variables = {}, options = {}) ⇒ Object
- #get(variables = {}, options = {}) ⇒ Object
-
#initialize(directory, options) ⇒ Client
constructor
A new instance of Client.
- #output(variables = {}, options = {}) ⇒ Object
- #plan(variables = {}, options = {}) ⇒ Object
- #show(variables = {}, options = {}) ⇒ Object
Constructor Details
#initialize(directory, options) ⇒ Client
Returns a new instance of Client.
5 6 7 8 |
# File 'lib/ruby_terraform/client.rb', line 5 def initialize(directory, ) @directory = directory = end |
Instance Method Details
#apply(variables = {}, options = {}) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/ruby_terraform/client.rb', line 39 def apply(variables = {}, = {}) = { input: false, 'no-color' => nil }.merge() status, _stdout, stderr = run('apply', variables, ) fail "Execute terraform apply has been failed\n#{stderr}" unless status.success? true end |
#destroy(variables = {}, options = {}) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/ruby_terraform/client.rb', line 73 def destroy(variables = {}, = {}) = { force: nil, 'no-color' => nil }.merge() status, _stdout, stderr = run('destroy', variables, ) fail "Execute terraform destroy has been failed\n#{stderr}" unless status.success? true end |
#get(variables = {}, options = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/ruby_terraform/client.rb', line 10 def get(variables = {}, = {}) status, _stdout, stderr = run('get', variables, ) fail "Execute terraform get has been failed\n#{stderr}" unless status.success? true end |
#output(variables = {}, options = {}) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/ruby_terraform/client.rb', line 64 def output(variables = {}, = {}) = { 'no-color' => nil }.merge() status, stdout, stderr = run('output', variables, ) fail "Execute terraform output has been failed\n#{stderr}" unless status.success? Hash[stdout.split("\n").map { |line| line.split(' = ') }] end |
#plan(variables = {}, options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ruby_terraform/client.rb', line 16 def plan(variables = {}, = {}) = { input: false, 'no-color' => nil }.merge() status, stdout, stderr = run('plan', variables, ) fail "Execute terraform plan has been failed\n#{stderr}" unless [0, 2].include?(status.exitstatus) { add: {}, destroy: {}, change: {} }.tap do |hash| stdout.split("\n").each do |line| case line when /^\-\/\+ (.*)/ build_hash(hash[:add], Regexp.last_match(1)) build_hash(hash[:destroy], Regexp.last_match(1)) when /^\+ (.*)/ build_hash(hash[:add], Regexp.last_match(1)) when /^\- (.*)/ build_hash(hash[:destroy], Regexp.last_match(1)) when /^\~ (.*)/ build_hash(hash[:change], Regexp.last_match(1)) end end end end |
#show(variables = {}, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ruby_terraform/client.rb', line 47 def show(variables = {}, = {}) = { 'no-color' => nil }.merge() status, stdout, stderr = run('show', variables, ) fail "Execute terraform show has been failed\n#{stderr}" unless [0, 2].include?(status.exitstatus) # Remove outputs stdout = stdout.gsub(/^Outputs:$.*/m, '') {}.tap do |hash| stdout.split("\n").each do |line| next unless (match = line.match(/^(\S*):$/)) build_hash(hash, match[1]) end end end |