Module: MuxTf::TerraformHelpers

Includes:
PiotrbCliUtils::ShellHelpers, PiotrbCliUtils::Util
Included in:
Cli::Current, Cli::Current::PlanCommand, Cli::PlanSummary, InitFormatter, PlanFormatter, PlanSummaryHandler, PlanSummaryHandler, PlanUtils
Defined in:
lib/mux_tf/terraform_helpers.rb

Defined Under Namespace

Classes: ResultStruct

Instance Method Summary collapse

Instance Method Details

#tf_apply(filename: nil, targets: []) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mux_tf/terraform_helpers.rb', line 38

def tf_apply(filename: nil, targets: [])
  args = []
  args << filename if filename
  if targets && !targets.empty?
    targets.each do |target|
      args << "-target=#{target}"
    end
  end

  cmd = tf_prepare_command(["apply", *args], need_auth: true)

  tf_stream_helper(cmd, operation: :apply, echo_stdout: true)
end

#tf_force_unlock(id:) ⇒ Object



10
11
12
# File 'lib/mux_tf/terraform_helpers.rb', line 10

def tf_force_unlock(id:)
  run_terraform(tf_prepare_command(["force-unlock", "-force", id], need_auth: true))
end

#tf_init(input: nil, upgrade: nil, reconfigure: nil, color: true, json: false, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mux_tf/terraform_helpers.rb', line 66

def tf_init(input: nil, upgrade: nil, reconfigure: nil, color: true, json: false, &block)
  args = []
  args << "-input=#{input.inspect}" unless input.nil?
  args << "-upgrade" unless upgrade.nil?
  args << "-reconfigure" unless reconfigure.nil?
  args << "-no-color" unless color
  args << "-json" if json

  cmd = tf_prepare_command(["init", *args], need_auth: true)
  stream_terraform(cmd, split_streams: true, &block)
end

#tf_plan(out:, color: true, detailed_exitcode: nil, compact_warnings: false, input: nil, targets: [], json: false, split_streams: false, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mux_tf/terraform_helpers.rb', line 78

def tf_plan(out:, color: true, detailed_exitcode: nil, compact_warnings: false, input: nil, targets: [], json: false, split_streams: false,
            &block)
  args = []
  args += ["-out", out]
  args << "-input=#{input.inspect}" unless input.nil?
  args << "-compact-warnings" if compact_warnings
  args << "-no-color" unless color
  args << "-detailed-exitcode" if detailed_exitcode
  args << "-json" if json
  if targets && !targets.empty?
    targets.each do |target|
      args << "-target=#{target}"
    end
  end

  cmd = tf_prepare_command(["plan", *args], need_auth: true)
  stream_or_run_terraform(cmd, split_streams: json || split_streams, &block)
end

#tf_show(file, json: false, capture: false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mux_tf/terraform_helpers.rb', line 97

def tf_show(file, json: false, capture: false)
  if json
    args = ["show", "-json", file]
    cmd = tf_prepare_command(args, need_auth: true)
    capture_terraform(cmd, json: true)
  else
    args = ["show", file]
    cmd = tf_prepare_command(args, need_auth: true)
    if capture
      capture_terraform(cmd)
    else
      run_terraform(cmd)
    end
  end
end

#tf_stream_helper(cmd, operation:, echo_stdout: false, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mux_tf/terraform_helpers.rb', line 14

def tf_stream_helper(cmd, operation:, echo_stdout: false, &block)
  # stdout = ""

  stderr_handler = StderrLineHandler.new(operation: operation)

  result_struct = stream_terraform(cmd, split_streams: true) { |(stream, raw_line)|
    case stream
    when :command
      log "Running command: #{raw_line.strip} ...", depth: 2
    when :stdout
      # stdout += raw_line
      print raw_line if echo_stdout
      block&.call(:stdout, raw_line)
    when :stderr
      stderr_handler.handle(raw_line)
    end
  }

  stderr_handler.flush
  stderr_handler.do_print_errors

  result_struct
end

#tf_validateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mux_tf/terraform_helpers.rb', line 52

def tf_validate
  cmd = tf_prepare_command(["validate", "-json"], need_auth: true)

  stdout = ""

  tf_stream_helper(cmd, operation: :validate) do |stream, raw_line|
    stdout += raw_line if stream == :stdout
  end

  throw :abort, false if stdout.strip.empty?

  JSON.parse(stdout)
end