Class: Bosh::Director::Changeset

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/manifest/changeset.rb

Constant Summary collapse

KEY_NAME =
'name'
REDACT_KEY_NAMES =
%w(
  properties
  bosh
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(before, after, redacted_before = nil, redacted_after = nil) ⇒ Changeset

Returns a new instance of Changeset.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bosh/director/manifest/changeset.rb', line 10

def initialize(before, after, redacted_before = nil, redacted_after = nil)
  @redacted_before = redacted_before.nil? ? Changeset.redact_properties!(Bosh::Common::DeepCopy.copy(before)) : redacted_before
  @redacted_after = redacted_after.nil? ? Changeset.redact_properties!(Bosh::Common::DeepCopy.copy(after)) : redacted_after

  @before = before
  @after = after

  if @before && @after
    @merged = deep_merge(@before, @after)
  elsif @before
    @merged = @before
  else
    @merged = @after
  end
end

Class Method Details

.redact_properties!(obj, redact_key_is_ancestor = false) ⇒ Object

redacts properties from ruby object to avoid having to use a regex to redact properties from diff output please do not use regexes for diffing/redacting



28
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
# File 'lib/bosh/director/manifest/changeset.rb', line 28

def self.redact_properties!(obj, redact_key_is_ancestor = false)
  if redact_key_is_ancestor
    if obj.respond_to?(:key?)
      obj.keys.each{ |key|
        if obj[key].respond_to?(:each)
          redact_properties!(obj[key], true)
        else
          obj[key] = '<redacted>'
        end
      }
    elsif obj.respond_to?(:each_index)
      obj.each_index { |i|
        if obj[i].respond_to?(:each)
          redact_properties!(obj[i], true)
        else
          obj[i] = '<redacted>'
        end
      }
    end
  else
    if obj.respond_to?(:each)
      obj.each{ |a|
        if obj.respond_to?(:key?) && REDACT_KEY_NAMES.any? { |key| key == a.first } && a.last.respond_to?(:key?)
          redact_properties!(a.last, true)
        else
          redact_properties!(a.respond_to?(:last) ? a.last : a)
        end

      }
    end
  end

  obj
end

Instance Method Details

#diff(redact = true, indent = 0) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bosh/director/manifest/changeset.rb', line 63

def diff(redact=true, indent = 0)
  lines = DiffLines.new

  if redact
    output_values_before = @redacted_before
    output_values_after = @redacted_after
  else
    output_values_before = @before
    output_values_after = @after
  end

  @merged.each_pair do |key, value|
    if @before[key] != @after[key]
      if @before.nil? || @before[key].nil?
        lines.concat(yaml_lines({key => output_values_after[key]}, indent, 'added'))

      elsif @after.nil? || @after[key].nil?
        lines.concat(yaml_lines({key => output_values_before[key]}, indent, 'removed'))

      elsif @before[key].is_a?(Array) && @after[key].is_a?(Array)
        lines.concat(compare_arrays(@before[key], @after[key], output_values_before[key], output_values_after[key], key, redact, indent))

      elsif @before[key].is_a?(Hash) && @after[key].is_a?(Hash)
        changeset = Changeset.new(@before[key], @after[key], output_values_before[key], output_values_after[key])
        diff_lines = changeset.diff(redact, indent+1)
        unless diff_lines.empty?
          lines << Line.new(indent, "#{key}:", nil)
          lines.concat(diff_lines)
        end

      else
        lines.concat(yaml_lines({key => output_values_before[key]}, indent, 'removed'))
        lines.concat(yaml_lines({key => output_values_after[key]}, indent, 'added'))
      end
    end
  end
  lines
end