Class: MuxTf::PlanSummaryHandler
- Inherits:
-
Object
- Object
- MuxTf::PlanSummaryHandler
- Extended by:
- TerraformHelpers
- Includes:
- Coloring, TerraformHelpers, PiotrbCliUtils::Util
- Defined in:
- lib/mux_tf/plan_summary_handler.rb
Class Method Summary collapse
- .color_for_action(action) ⇒ Object
- .data_from_file(file) ⇒ Object
- .format_action(action) ⇒ Object
- .format_address(address) ⇒ Object
- .from_data(data) ⇒ Object
- .from_file(file) ⇒ Object
- .symbol_for_action(action) ⇒ Object
Instance Method Summary collapse
- #flat_summary ⇒ Object
-
#initialize(data) ⇒ PlanSummaryHandler
constructor
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#nested_summary ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #output_parts ⇒ Object
- #output_summary ⇒ Object
- #resource_parts ⇒ Object
- #run_interactive ⇒ Object
- #sensitive_summary(before_value, after_value) ⇒ Object
-
#summary ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
Methods included from TerraformHelpers
tf_apply, tf_force_unlock, tf_init, tf_plan, tf_show, tf_validate
Methods included from Coloring
Constructor Details
#initialize(data) ⇒ PlanSummaryHandler
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
97 98 99 100 101 102 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 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 97 def initialize(data) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity @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 when ["create"] parts << { type: "resource", action: "create", address: v["address"], deps: find_deps(data, v["address"]) } when ["update"] parts << { type: "resource", action: "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"]) } 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" :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" # 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 ..." result = tf_show(file, json: true) data = result.parsed_output File.write("#{file}.json", JSON.dump(data)) data end end |
.format_action(action) ⇒ Object
72 73 74 75 76 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 72 def format_action(action) color = color_for_action(action) symbol = symbol_for_action(action) pastel.decorate(symbol, color) end |
.format_address(address) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 78 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 |
# 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" ">" else action end end |
Instance Method Details
#flat_summary ⇒ Object
235 236 237 238 239 240 241 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 235 def flat_summary result = [] resource_parts.each do |part| result << "[#{self.class.format_action(part[:action])}] #{self.class.format_address(part[:address])}" end result end |
#nested_summary ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 268 def nested_summary # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity 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 = "[#{self.class.format_action(part[:action])}]#{indent} #{self.class.format_address(part[:address])}" += " - (needs: #{part[:met_deps].join(', ')})" if part[:met_deps] result << 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_parts ⇒ Object
197 198 199 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 197 def output_parts parts.select { |part| part[:type] == "output" } end |
#output_summary ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 254 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 |
#resource_parts ⇒ Object
193 194 195 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 193 def resource_parts parts.select { |part| part[:type] == "resource" } end |
#run_interactive ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 296 def run_interactive prompt = TTY::Prompt.new result = prompt.multi_select("Update resources:", per_page: 99, echo: false) { || resource_parts.each do |part| label = "[#{self.class.format_action(part[:action])}] #{self.class.format_address(part[:address])}" .choice label, part[:address] end } if result.empty? throw :abort, "nothing selected" else log "Re-running apply with the selected resources ..." MuxTf::Cli::Current.run_plan(targets: result) end end |
#sensitive_summary(before_value, after_value) ⇒ Object
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 243 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 |
#summary ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/mux_tf/plan_summary_handler.rb', line 201 def summary # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity # 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 |