Class: MuxTf::PlanUtils

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

Constant Summary collapse

KNOWN_AFTER_APPLY =
"(known after apply)"
SENSITIVE =
"(sensitive value)"

Class 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

Class Method Details

.action_reason_to_text(action_reason) ⇒ Object



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/mux_tf/plan_utils.rb', line 456

def action_reason_to_text(action_reason)
  case action_reason
  when "replace_because_tainted"
    "because the object is tainted in the prior state"
  when "replace_because_cannot_update"
    "because the provider does not support updating the object, or the changed attributes"
  when "replace_by_request"
    "because the user requested the object to be replaced"
  when "delete_because_no_resource_config"
    "because the object is no longer in the configuration"
  when "delete_because_no_module"
    "because the module this object belongs to is no longer in the configuration"
  when "delete_because_wrong_repetition"
    "because of the repetition mode has changed"
  when "delete_because_count_index"
    "because the repetition count has changed"
  when "delete_because_each_key"
    "because the repetition key has changed"
  when "read_because_config_unknown"
    "because reading this will only be possible during apply"
  when "read_because_dependency_pending"
    "because reading this will only be possible after a dependency is available"
  else
    "because #{action_reason}"
  end
end

.colorize_symbol(symbol) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mux_tf/plan_utils.rb', line 120

def colorize_symbol(symbol)
  case symbol
  when "+"
    pastel.green(symbol)
  when "~"
    pastel.yellow(symbol)
  when "-"
    pastel.red(symbol)
  when "?"
    pastel.orange(symbol)
  when "±"
    pastel.bright_red(symbol)
  when ">"
    pastel.blue(symbol)
  when " "
    symbol
  else
    warning "Unknown symbol: #{symbol.inspect}"
    symbol
  end
end

.format_output_value_diff(mode, value_arg) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/mux_tf/plan_utils.rb', line 338

def format_output_value_diff(mode, value_arg)
  case mode
  when :both
    vleft = in_display_representation(value_arg[0])
    vright = in_display_representation(value_arg[1])
    if [vleft, vright].any? { |v| v.is_a?(String) && v.include?("\n") }
      if pastel.strip(vright) == KNOWN_AFTER_APPLY
        "#{vleft} -> #{vright}".split("\n")
      else
        string_diff(pastel.strip(vleft), pastel.strip(vright))
      end
    else
      "#{vleft} -> #{vright}".split("\n")
    end
  when :right
    vright = in_display_representation(value_arg[1])
    vright.split("\n")
  when :left, :first
    vleft = in_display_representation(value_arg[0])
    vleft.split("\n")
  end
end

.format_value(change) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/mux_tf/plan_utils.rb', line 228

def format_value(change)
  symbol, _key, *value_arg = change

  mode = :both
  case symbol
  when "+", "-"
    mode = :first
  when "~"
    mode = :both
  else
    warning "Unknown symbol: #{symbol.inspect}"
  end

  format_value_diff(mode, value_arg)
end

.format_value_diff(mode, value_arg) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/mux_tf/plan_utils.rb', line 205

def format_value_diff(mode, value_arg)
  case mode
  when :both
    vleft = in_display_representation(value_arg[0])
    vright = in_display_representation(value_arg[1])
    if [vleft, vright].any? { |v| v.is_a?(String) && v.include?("\n") }
      if pastel.strip(vright) == KNOWN_AFTER_APPLY
        "#{vleft} -> #{vright}".split("\n")
      else
        string_diff(pastel.strip(vleft), pastel.strip(vright))
      end
    else
      "#{vleft} -> #{vright}".split("\n")
    end
  when :right
    vright = in_display_representation(value_arg[1])
    vright.split("\n")
  when :left, :first
    vleft = in_display_representation(value_arg[0])
    vleft.split("\n")
  end
end

.get_pretty_action_and_symbol(actions) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/mux_tf/plan_utils.rb', line 244

def get_pretty_action_and_symbol(actions)
  case actions
  when ["delete"]
    pretty_action = "delete"
    symbol = "-"
  when ["update"]
    pretty_action = "updated in-place"
    symbol = "~"
  when ["create"]
    pretty_action = "created"
    symbol = "+"
  when %w[delete create]
    pretty_action = "replaced (delete first)"
    symbol = "±"
  when ["read"]
    pretty_action = "read"
    symbol = ">"
  when ["no-op"]
    pretty_action = "no-op"
    symbol = " "
  else
    warning "Unknown action: #{actions.inspect}"
    pretty_action = actions.inspect
    symbol = "?"
  end

  [pretty_action, symbol]
end

.get_value_type(change) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/mux_tf/plan_utils.rb', line 368

def get_value_type(change)
  case change["actions"]
  when ["create"]
    get_x_type("after", change)
  when ["delete"]
    get_x_type("before", change)
  when ["update"]
    bt = get_x_type("before", change)
    at = get_x_type("after", change)
    raise "Type mismatch before vs after: #{bt} != #{at}" if bt != at

    at
  when ["no-op"]
    :none
  else
    raise "Unknown action: #{change['actions'].inspect}"
  end
end

.get_x_type(key, change) ⇒ Object



361
362
363
364
365
366
# File 'lib/mux_tf/plan_utils.rb', line 361

def get_x_type(key, change)
  return :hash if change[key].is_a?(Hash) || change["#{key}_sensitive"].is_a?(Hash) || change["#{key}_unknown"].is_a?(Hash)
  return :array if change[key].is_a?(Array) || change["#{key}_sensitive"].is_a?(Array) || change["#{key}_unknown"].is_a?(Array)

  :scalar
end

.in_display_representation(value) ⇒ Object



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
# File 'lib/mux_tf/plan_utils.rb', line 164

def in_display_representation(value)
  if valid_json?(value)
    json_body = JSON.pretty_generate(JSON.parse(value))
    wrap(json_body, prefix: "json(", suffix: ")", color: :gray)
  elsif valid_yaml?(value)
    yaml_body = YAML.dump(YAML.safe_load(value))
    yaml_body.gsub!(/^---\n/, "")
    wrap(yaml_body, prefix: "yaml(", suffix: ")", newline: true, color: :gray, indent: 2)
  elsif [KNOWN_AFTER_APPLY, SENSITIVE].include?(value)
    pastel.gray(value)
  elsif value.is_a?(String) && value.include?("\n")
    wrap(value, prefix: "<<- EOT", suffix: "EOT", newline: true, color: :gray)
  # elsif value.is_a?(Array)
  #   body = value.ai.rstrip
  #   wrap(body, prefix: "", suffix: "", newline: false, color: :gray, indent: 2)
  elsif value.is_a?(Array)
    max_key = value.length.to_s.length
    body = "["
    value.each_with_index do |v, _index|
      # body += "\n  #{in_display_representation(index).ljust(max_key)}: "
      body += "\n"
      body += indent(in_display_representation(v), indent: 2, first_line_indent: 2)
      body += ","
    end
    body += "\n]"
    body
  elsif value.is_a?(Hash)
    max_key = value.keys.map(&:length).max
    body = "{"
    value.each do |k, v|
      body += "\n  #{in_display_representation(k).ljust(max_key)}: "
      body += indent(in_display_representation(v), indent: 2, first_line_indent: 0)
      body += ","
    end
    body += "\n}"
    body
  else
    value.inspect
  end
end

.indent(text, indent: 2, first_line_indent: 0) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/mux_tf/plan_utils.rb', line 154

def indent(text, indent: 2, first_line_indent: 0)
  text.split("\n").map.with_index { |line, index|
    if index.zero?
      "#{' ' * first_line_indent}#{line}"
    else
      "#{' ' * indent}#{line}"
    end
  }.join("\n")
end

.prep_before_after(change) ⇒ Object



387
388
389
390
391
392
393
394
395
396
# File 'lib/mux_tf/plan_utils.rb', line 387

def prep_before_after(change)
  before = change["before"]
  after = change["after"]
  before = KNOWN_AFTER_APPLY if change["before_unknown"]
  before = SENSITIVE if change["before_sensitive"]
  after = KNOWN_AFTER_APPLY if change["after_unknown"]
  after = SENSITIVE if change["after_sensitive"]

  [before, after]
end

.string_diff(value1, value2) ⇒ Object



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

def string_diff(value1, value2)
  value1 = value1.split("\n")
  value2 = value2.split("\n")

  output = []
  diffs = Diff::LCS.diff value1, value2
  diffs.each do |diff|
    hunk = Diff::LCS::Hunk.new(value1, value2, diff, 5, 0)
    diff_lines = hunk.diff(:unified).split("\n")
    # diff_lines.shift # remove the first line
    output += diff_lines.map { |line| " #{line}" }
  end
  output
end

.text_version_of_plan_show_from_data(data) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/mux_tf/plan_utils.rb', line 483

def text_version_of_plan_show_from_data(data)
  output = []

  output << "Terraform will perform the following actions:"

  if data["resource_drift"]
    output << ""
    output << "Resource Drift:"
    data["resource_drift"].each do |resource|
      output << tf_show_json_resource(resource)
    end
  end

  if data["resource_changes"]
    output << ""
    output << "Resource Changes:"
    data["resource_changes"].each do |resource|
      next if resource["change"]["actions"] == ["no-op"]

      output << tf_show_json_resource(resource)
    end
  end

  actual_output_changes = (data["output_changes"] || []).reject { |_, change| change["actions"] == ["no-op"] }

  if actual_output_changes.length.positive?
    output << ""
    output << "Changes to Outputs:"
    max_outer_key_length = actual_output_changes.keys.map(&:length).max || 0
    actual_output_changes.each do |key, output_change|
      output << tf_show_json_output(key, output_change, max_outer_key_length)
    end
  end

  output.join("\n")
end

.tf_show_json_output(output_key, output_change, max_outer_key_length) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/mux_tf/plan_utils.rb', line 398

def tf_show_json_output(output_key, output_change, max_outer_key_length)
  _pretty_action, symbol = get_pretty_action_and_symbol(output_change["actions"])

  output = []

  global_indent = " " * 2

  value_type = get_value_type(output_change)

  start_prefix = format("#{global_indent}#{colorize_symbol(symbol)} %s = ", output_key.ljust(max_outer_key_length))
  blank_start_prefix = " " * pastel.strip(start_prefix).length
  outer_mode = :both
  outer_mode = :right if ["+"].include?(symbol)
  outer_mode = :left if ["-"].include?(symbol)
  outer_mode = :right if [" "].include?(symbol)

  case value_type
  when :hash
    output << "#{start_prefix}{"

    fake_resource = {
      "change" => output_change
    }
    diff = tf_show_json_resource_diff(fake_resource)
    max_diff_key_length = diff.map { |change| change[1].length }.max

    diff.each do |change|
      change_symbol, key, *_values = change
      prefix = format("#{global_indent}    #{colorize_symbol(change_symbol)} %s = ", key.ljust(max_diff_key_length))
      suffix = ""
      blank_prefix = " " * pastel.strip(prefix).length
      format_value(change).each_with_index do |line, index|
        output << if index.zero?
                    "#{prefix}#{line}#{suffix}"
                  else
                    "#{blank_prefix}#{line}#{suffix}"
                  end
      end
    end

    output << "#{global_indent}  }"
  when :array, :scalar, :none
    before, after = prep_before_after(output_change)
    format_value_diff(outer_mode, [before, after]).each_with_index do |line, index|
      output << if index.zero?
                  "#{start_prefix}#{line}"
                else
                  "#{blank_start_prefix}#{line}"
                end
    end
  else
    raise "Unknown value type: #{value_type.inspect}"
  end

  output.join("\n")
end

.tf_show_json_resource(resource) ⇒ Object

Example

# kubectl_manifest.crossplane-provider-controller-config["aws-ecr"] will be updated in-place
~ resource "kubectl_manifest" "crossplane-provider-controller-config" {
      id                      = "/apis/pkg.crossplane.io/v1alpha1/controllerconfigs/aws-ecr-config"
      name                    = "aws-ecr-config"
    ~ yaml_body               = (sensitive value)
    ~ yaml_body_parsed        = "          apiVersion: pkg.crossplane.io/v1alpha1\n          kind: ControllerConfig\n          metadata:\n            annotations:\n        -     eks.amazonaws.com/role-arn: <AWS_PROVIDER_ARN> <<- irsa\n        +     eks.amazonaws.com/role-arn: arn:aws:iam::852088082597:role/admin-crossplane-provider-aws-ecr\n            name: aws-ecr-config\n          spec:\n            podSecurityContext:\n              fsGroup: 2000\n      EOT\n      # (12 unchanged attributes hidden)\n  }\n"


293
294
295
296
297
298
299
300
301
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
329
330
331
332
333
334
335
336
# File 'lib/mux_tf/plan_utils.rb', line 293

def tf_show_json_resource(resource)
  pretty_action, symbol = get_pretty_action_and_symbol(resource["change"]["actions"])

  output = []

  global_indent = " " * 2

  output << ""
  output << "#{global_indent}#{pastel.bold("# #{resource['address']}")} will be #{pretty_action}"
  output << "#{global_indent}  >> #{action_reason_to_text(resource['action_reason'])}" if resource["action_reason"]
  output << "#{global_indent}#{colorize_symbol(symbol)} resource \"#{resource['type']}\" \"#{resource['name']}\" {"
  diff = tf_show_json_resource_diff(resource)
  max_diff_key_length = diff.map { |change| change[1].length }.max

  fields_which_caused_replacement = []

  if resource["change"]["replace_paths"]
    if resource["change"]["replace_paths"].length != 1
      warning "Multiple replace paths found for resource #{resource['address']}: #{resource['change']['replace_paths'].inspect}"
    elsif resource["change"]["replace_paths"][0].length != 1
      warning "Multiple fields found to be replaced for resource #{resource['address']}: #{resource['change']['replace_paths'].inspect}"
    else
      fields_which_caused_replacement << resource["change"]["replace_paths"][0][0]
    end
  end

  diff.each do |change|
    change_symbol, key, *_values = change
    prefix = format("#{global_indent}  #{colorize_symbol(change_symbol)} %s = ", key.ljust(max_diff_key_length))
    suffix = ""
    suffix = " (forces replacement)" if fields_which_caused_replacement.include?(key)
    blank_prefix = " " * pastel.strip(prefix).length
    format_value(change).each_with_index do |line, index|
      output << if index.zero?
                  "#{prefix}#{line}#{suffix}"
                else
                  "#{blank_prefix}#{line}#{suffix}"
                end
    end
  end
  output << "#{global_indent}}"

  output.join("\n")
end

.tf_show_json_resource_diff(resource) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mux_tf/plan_utils.rb', line 67

def tf_show_json_resource_diff(resource)
  before = resource["change"]["before"] || {}
  after = resource["change"]["after"] || {}

  update_placeholders(after, resource["change"]["after_unknown"], KNOWN_AFTER_APPLY)

  before = before.sort.to_h
  after = after.sort.to_h

  update_placeholders(before, resource["change"]["before_sensitive"], SENSITIVE)
  update_placeholders(after, resource["change"]["after_sensitive"], SENSITIVE)

  # hash_diff = HashDiff::Comparison.new(before, after)
  # similarity: 0.0, numeric_tolerance: 1, array_path: true,
  Hashdiff.diff(before, after, use_lcs: false)
end

.update_placeholders(dst, src, placeholder) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mux_tf/plan_utils.rb', line 29

def update_placeholders(dst, src, placeholder)
  return unless src

  case src
  when Array
    src.each_with_index do |v, index|
      case v
      when TrueClass
        dst[index] = placeholder
      when FalseClass
        # do nothing
      when Hash
        dst[index] ||= {}
        update_placeholders(dst[index], v, placeholder)
      else
        warning "Unknown array value (index: #{index}) for sensitive: #{v.inspect}"
      end
    end
  when Hash
    src.each do |key, value|
      case value
      when TrueClass
        dst[key] = placeholder
      when FalseClass
        # do nothing
      when Array
        dst[key] ||= []
        update_placeholders(dst[key], value, placeholder)
      when Hash
        dst[key] ||= {}
        update_placeholders(dst[key], value, placeholder)
      else
        warning "Unknown value (key: #{key}) for sensitive: #{value.inspect}"
      end
    end
  end
end

.valid_json?(value) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/mux_tf/plan_utils.rb', line 99

def valid_json?(value)
  value.is_a?(String) && !!JSON.parse(value)
rescue JSON::ParserError
  false
end

.valid_yaml?(value) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mux_tf/plan_utils.rb', line 105

def valid_yaml?(value)
  if value.is_a?(String)
    parsed = YAML.safe_load(value)
    parsed.is_a?(Hash) || parsed.is_a?(Array)
  else
    false
  end
rescue Psych::DisallowedClass => _e
  # ap e
  false
rescue Psych::SyntaxError => _e # rubocop:disable Lint/DuplicateBranch
  # ap e
  false
end

.warning(message, binding_arg: binding) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mux_tf/plan_utils.rb', line 13

def warning(message, binding_arg: binding)
  stack = binding_arg.send(:caller)
  stack_match = stack[0].match(/^(?<path>.+):(?<ln>\d+):in [`'](?<method>.+)'$/)
  msg = []
  if stack_match
    stack_line = stack_match.named_captures
    stack_line["path"].gsub!(MuxTf::ROOT, pastel.gray("{mux_tf}"))
    msg << "#{pastel.orange('WARNING')}: #{message}"
    msg << "at #{pastel.cyan(stack_line['path'])}:#{pastel.white(stack_line['ln'])}:in `#{pastel.cyan(stack_line['method'])}'"
  else
    p [stack_match, stack[0]]
    msg << "#{pastel.orange('WARNING')}: #{message}"
  end
  puts msg.join(" - ")
end

.wrap(text, prefix: "(", suffix: ")", newline: false, color: nil, indent: 0) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mux_tf/plan_utils.rb', line 142

def wrap(text, prefix: "(", suffix: ")", newline: false, color: nil, indent: 0)
  result = String.new
  result << (color ? pastel.decorate(prefix, color) : prefix)
  result << "\n" if newline
  result << text.split("\n").map { |line|
    "#{' ' * indent}#{line}"
  }.join("\n")
  result << "\n" if newline
  result << (color ? pastel.decorate(suffix, color) : suffix)
  result
end