Class: MuxTf::PlanSummaryHandler

Inherits:
Object
  • Object
show all
Extended by:
TerraformHelpers
Includes:
Coloring, TerraformHelpers, PiotrbCliUtils::Util
Defined in:
lib/mux_tf/plan_summary_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TerraformHelpers

tf_apply, tf_force_unlock, tf_init, tf_plan, tf_show, tf_stream_helper, tf_validate

Methods included from Coloring

included, #pastel

Constructor Details

#initialize(data) ⇒ PlanSummaryHandler

Returns a new instance of PlanSummaryHandler.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/mux_tf/plan_summary_handler.rb', line 103

def initialize(data)
  @data = data
  @parts = []

  data["output_changes"]&.each do |output_name, v|
    case v["actions"]
    when ["no-op"]
      # do nothing
    when ["create"]
      parts << {
        type: "output",
        action: "create",
        after_unknown: v["after_unknown"],
        sensitive: [v["before_sensitive"], v["after_sensitive"]],
        address: output_name
      }
    when ["update"]
      parts << {
        type: "output",
        action: "update",
        after_unknown: v["after_unknown"],
        sensitive: [v["before_sensitive"], v["after_sensitive"]],
        address: output_name
      }
    when ["delete"]
      parts << {
        type: "output",
        action: "delete",
        after_unknown: v["after_unknown"],
        sensitive: [v["before_sensitive"], v["after_sensitive"]],
        address: output_name
      }
    else
      puts "[??] #{output_name}"
      puts "UNKNOWN OUTPUT ACTIONS: #{v['actions'].inspect}"
      puts "TODO: update plan_summary to support this!"
    end
  end

  data["resource_changes"]&.each do |v|
    next unless v["change"]

    case v["change"]["actions"]
    when ["no-op"]
      # do nothing
      if v["change"]["importing"]
        parts << {
          type: "resource",
          action: "import",
          address: v["address"],
          deps: find_deps(data, v["address"])
        }
      end
    when ["create"]
      parts << {
        type: "resource",
        action: "create",
        address: v["address"],
        deps: find_deps(data, v["address"])
      }
    when ["update"]
      # ap [v["change"]["actions"], v["change"]["importing"]]
      parts << {
        type: "resource",
        action: v["change"]["importing"] ? "import-update" : "update",
        address: v["address"],
        deps: find_deps(data, v["address"])
      }
    when ["delete"]
      parts << {
        type: "resource",
        action: "delete",
        address: v["address"],
        deps: find_deps(data, v["address"])
      }
    when %w[delete create]
      parts << {
        type: "resource",
        action: "replace",
        address: v["address"],
        deps: find_deps(data, v["address"])
      }
    when %w[create delete]
      parts << {
        type: "resource",
        action: "replace (create before delete)",
        address: v["address"],
        deps: find_deps(data, v["address"])
      }
    when ["read"]
      parts << {
        type: "resource",
        action: "read",
        address: v["address"],
        deps: find_deps(data, v["address"])
      }
    when ["forget"]
      parts << {
        type: "resource",
        action: "forget",
        address: v["address"],
        deps: find_deps(data, v["address"])
      }
    else
      puts "[??] #{v['address']}"
      puts "UNKNOWN RESOURCE ACTIONS: #{v['change']['actions'].inspect}"
      puts "TODO: update plan_summary to support this!"
    end
  end

  prune_unchanged_deps(parts)
end

Class Method Details

.color_for_action(action) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mux_tf/plan_summary_handler.rb', line 32

def color_for_action(action)
  case action
  when "create", "add"
    :green
  when "update", "change", "import-update"
    :yellow
  when "delete", "remove"
    :red
  when "replace" # rubocop:disable Lint/DuplicateBranch
    :red
  when "replace (create before delete)" # rubocop:disable Lint/DuplicateBranch
    :red
  when "read"
    :cyan
  when "import", "forget" # rubocop:disable Lint/DuplicateBranch
    :cyan
  else
    :reset
  end
end

.data_from_file(file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mux_tf/plan_summary_handler.rb', line 16

def data_from_file(file)
  if File.exist?("#{file}.json") && File.mtime("#{file}.json").to_f >= File.mtime(file).to_f
    JSON.parse(File.read("#{file}.json"))
  else
    puts "Analyzing changes ... #{file}"
    result = tf_show(file, json: true)
    data = result.parsed_output
    File.write("#{file}.json", JSON.dump(data))
    data
  end
end

.format_action(action) ⇒ Object



78
79
80
81
82
# File 'lib/mux_tf/plan_summary_handler.rb', line 78

def format_action(action)
  color = color_for_action(action)
  symbol = symbol_for_action(action)
  pastel.decorate(symbol, color)
end

.format_address(address) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mux_tf/plan_summary_handler.rb', line 84

def format_address(address)
  result = []
  parts = ResourceTokenizer.tokenize(address)
  parts.each_with_index do |(part_type, part_value), index|
    case part_type
    when :rt
      result << "." if index.positive?
      result << pastel.cyan(part_value)
    when :rn
      result << "."
      result << part_value
    when :ri
      result << pastel.green(part_value)
    end
  end
  result.join
end

.from_data(data) ⇒ Object



28
29
30
# File 'lib/mux_tf/plan_summary_handler.rb', line 28

def from_data(data)
  new(data)
end

.from_file(file) ⇒ Object



11
12
13
14
# File 'lib/mux_tf/plan_summary_handler.rb', line 11

def from_file(file)
  data = data_from_file(file)
  new data
end

.symbol_for_action(action) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mux_tf/plan_summary_handler.rb', line 53

def symbol_for_action(action)
  case action
  when "create"
    "+"
  when "update"
    "~"
  when "delete"
    "-"
  when "replace"
    ""
  when "replace (create before delete)"
    "±"
  when "read"
    ">"
  when "import"
    ""
  when "import-update"
    "↗︎"
  when "forget"
    ""
  else
    action
  end
end

Instance Method Details

#flat_summaryObject



258
259
260
261
262
# File 'lib/mux_tf/plan_summary_handler.rb', line 258

def flat_summary
  resource_parts.map do |part|
    "[#{self.class.format_action(part[:action])}] #{self.class.format_address(part[:address])}"
  end
end

#nested_summaryObject



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/mux_tf/plan_summary_handler.rb', line 302

def nested_summary
  result = []
  parts = resource_parts.deep_dup
  until parts.empty?
    part = parts.shift
    if part[:deps] == []
      indent = if part[:met_deps] && !part[:met_deps].empty?
                 "  "
               else
                 ""
               end
      message = "[#{self.class.format_action(part[:action])}]#{indent} #{self.class.format_address(part[:address])}"
      message += " - (needs: #{part[:met_deps].join(', ')})" if part[:met_deps]
      result << message
      parts.each do |ipart|
        d = ipart[:deps].delete(part[:address])
        if d
          ipart[:met_deps] ||= []
          ipart[:met_deps] << d
        end
      end
    else
      parts.unshift part
    end
  end
  result
end

#output_partsObject



220
221
222
# File 'lib/mux_tf/plan_summary_handler.rb', line 220

def output_parts
  parts.select { |part| part[:type] == "output" }
end

#output_summaryObject



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/mux_tf/plan_summary_handler.rb', line 275

def output_summary
  result = []
  output_parts.each do |part|
    pieces = [
      "[#{self.class.format_action(part[:action])}]",
      self.class.format_address("output.#{part[:address]}"),
      part[:after_unknown] ? "(unknown)" : nil,
      sensitive_summary(*part[:sensitive])
    ].compact
    result << pieces.join(" ")
  end
  result
end

#plan_text_outputObject



346
347
348
# File 'lib/mux_tf/plan_summary_handler.rb', line 346

def plan_text_output
  PlanUtils.text_version_of_plan_show_from_data(@data)
end

#resource_partsObject



216
217
218
# File 'lib/mux_tf/plan_summary_handler.rb', line 216

def resource_parts
  parts.select { |part| part[:type] == "resource" }
end

#run_interactiveObject



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/mux_tf/plan_summary_handler.rb', line 330

def run_interactive
  prompt = TTY::Prompt.new
  result = prompt.multi_select("Update resources:", per_page: 99, echo: false) { |menu|
    resource_parts.each do |part|
      label = "[#{self.class.format_action(part[:action])}] #{self.class.format_address(part[:address])}"
      menu.choice label, part[:address]
    end
  }

  if result.empty?
    throw :abort, "nothing selected"
  else
    result
  end
end

#sensitive_summary(before_value, after_value) ⇒ Object



264
265
266
267
268
269
270
271
272
273
# File 'lib/mux_tf/plan_summary_handler.rb', line 264

def sensitive_summary(before_value, after_value)
  # before vs after
  if before_value && after_value
    "(#{pastel.yellow('sensitive')})"
  elsif before_value
    "(#{pastel.red('-sensitive')})"
  elsif after_value
    "(#{pastel.cyan('+sensitive')})"
  end
end

#simple_summary(&printer) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/mux_tf/plan_summary_handler.rb', line 289

def simple_summary(&printer)
  printer = method(:puts) if printer.nil?

  flat_summary.each do |line|
    printer.call line
  end
  output_summary.each do |line|
    printer.call line
  end
  printer.call ""
  printer.call summary
end

#summaryObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/mux_tf/plan_summary_handler.rb', line 224

def summary
  # resources
  resource_summary = {}
  resource_parts.each do |part|
    resource_summary[part[:action]] ||= 0
    resource_summary[part[:action]] += 1
  end
  resource_pieces = resource_summary.map { |k, v|
    color = self.class.color_for_action(k)
    "#{pastel.yellow(v)} to #{pastel.decorate(k, color)}"
  }

  # outputs
  output_summary = {}
  output_parts.each do |part|
    output_summary[part[:action]] ||= 0
    output_summary[part[:action]] += 1
  end
  output_pieces = output_summary.map { |k, v|
    color = self.class.color_for_action(k)
    "#{pastel.yellow(v)} to #{pastel.decorate(k, color)}"
  }

  if resource_pieces.any? || output_pieces.any?
    [
      "Plan Summary:",
      resource_pieces.any? ? resource_pieces.join(pastel.gray(", ")) : nil,
      output_pieces.any? ? "Outputs: #{output_pieces.join(pastel.gray(', '))}" : nil
    ].compact.join(" ")
  else
    "Plan Summary: no changes"
  end
end