Class: RubyTerraform::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_terraform/client.rb

Instance Method Summary collapse

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, options)
  @directory = directory
  @options = options
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 = {}, options = {})
  options = { input: false, 'no-color' => nil }.merge(options)

  status, _stdout, stderr = run('apply', variables, options)
  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 = {}, options = {})
  options = { force: nil, 'no-color' => nil }.merge(options)

  status, _stdout, stderr = run('destroy', variables, options)
  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 = {}, options = {})
  status, _stdout, stderr = run('get', variables, options)
  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 = {}, options = {})
  options = { 'no-color' => nil }.merge(options)

  status, stdout, stderr = run('output', variables, options)
  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 = {}, options = {})
  options = { input: false, 'no-color' => nil }.merge(options)

  status, stdout, stderr = run('plan', variables, options)
  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 = {}, options = {})
  options = { 'no-color' => nil }.merge(options)

  status, stdout, stderr = run('show', variables, options)
  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