Class: StackMaster::StackDiffer

Inherits:
Object
  • Object
show all
Defined in:
lib/stack_master/stack_differ.rb

Instance Method Summary collapse

Constructor Details

#initialize(proposed_stack, current_stack) ⇒ StackDiffer

Returns a new instance of StackDiffer.



5
6
7
8
# File 'lib/stack_master/stack_differ.rb', line 5

def initialize(proposed_stack, current_stack)
  @proposed_stack = proposed_stack
  @current_stack = current_stack
end

Instance Method Details

#body_diffObject



44
45
46
# File 'lib/stack_master/stack_differ.rb', line 44

def body_diff
  @body_diff ||= Diffy::Diff.new(current_template, proposed_template, context: 7, include_diff_info: true).to_s
end

#body_different?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/stack_master/stack_differ.rb', line 40

def body_different?
  body_diff != ''
end

#current_parametersObject



21
22
23
24
25
26
27
# File 'lib/stack_master/stack_differ.rb', line 21

def current_parameters
  if @current_stack
    YAML.dump(sort_params(@current_stack.parameters_with_defaults))
  else
    ''
  end
end

#current_templateObject



15
16
17
18
19
# File 'lib/stack_master/stack_differ.rb', line 15

def current_template
  return '' unless @current_stack
  return @current_stack.template_body unless @current_stack.template_format == :json
  JSON.pretty_generate(TemplateUtils.template_hash(@current_stack.template_body))
end

#noecho_keysObject



65
66
67
68
69
70
71
72
73
# File 'lib/stack_master/stack_differ.rb', line 65

def noecho_keys
  if @current_stack
    @current_stack.parameters_with_defaults.select do |key, value|
      value == "****"
    end.keys
  else
    []
  end
end

#output_diffObject



56
57
58
59
60
61
62
63
# File 'lib/stack_master/stack_differ.rb', line 56

def output_diff
  display_diff('Stack', body_diff)
  display_diff('Parameters', params_diff)
  unless noecho_keys.empty?
    StackMaster.stdout.puts " * can not tell if NoEcho parameters are different."
  end
  StackMaster.stdout.puts "No stack found" if @current_stack.nil?
end

#params_diffObject



52
53
54
# File 'lib/stack_master/stack_differ.rb', line 52

def params_diff
  @param_diff ||= Diffy::Diff.new(current_parameters, proposed_parameters, {}).to_s
end

#params_different?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/stack_master/stack_differ.rb', line 48

def params_different?
  params_diff != ''
end

#proposed_parametersObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/stack_master/stack_differ.rb', line 29

def proposed_parameters
  # **** out any secret parameters in the current stack.
  params = @proposed_stack.parameters_with_defaults
  if @current_stack
    noecho_keys.each do |key|
      params[key] = "****"
    end
  end
  YAML.dump(sort_params(params))
end

#proposed_templateObject



10
11
12
13
# File 'lib/stack_master/stack_differ.rb', line 10

def proposed_template
  return @proposed_stack.template_body unless @proposed_stack.template_format == :json
  JSON.pretty_generate(JSON.parse(@proposed_stack.template_body))
end