Class: SprinkleDNS::CLI::HostedZoneDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/sprinkle_dns/cli/hosted_zone_diff.rb

Defined Under Namespace

Classes: Entry, HostedZone

Instance Method Summary collapse

Instance Method Details

#diff(existing_hosted_zones, missing_hosted_zones, configuration) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
62
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
101
# File 'lib/sprinkle_dns/cli/hosted_zone_diff.rb', line 6

def diff(existing_hosted_zones, missing_hosted_zones, configuration)
  entries = []

  hosted_zones = if configuration.create_hosted_zones?
    (existing_hosted_zones + missing_hosted_zones)
  else
    existing_hosted_zones
  end

  hosted_zones.each do |hosted_zone|
    policy_service = SprinkleDNS::EntryPolicyService.new(hosted_zone, configuration)

    to_create = policy_service.entries_to_create
    to_update = policy_service.entries_to_update
    to_delete = policy_service.entries_to_delete

    if missing_hosted_zones.include?(hosted_zone)
      entries << hosted_zone_to_struct('+', hosted_zone)
    end

    hosted_zone.entries.each do |entry|
      if to_create.include?(entry)
        entries << entry_to_struct('+', entry, hosted_zone)
      elsif to_update.include?(entry)
        old_entry = entry
        new_entry = entry.new_entry

        entries << entry_to_struct('u-', old_entry, hosted_zone)
        entries << entry_to_struct('u+', new_entry, hosted_zone, old_entry)
      elsif to_delete.include?(entry)
        entries << entry_to_struct('-', entry, hosted_zone)
      else
        if configuration.show_untouched?
          entries << entry_to_struct(nil, entry, hosted_zone)
        end
      end
    end
  end

  coloured_entries = []

  entries.each do |e|
    colour_mod = case e.action
    when '+'
      ->(text) { "#{fg(*green)}#{text}#{color_reset}" }
    when '-'
      ->(text) { "#{fg(*red)}#{text}#{color_reset}" }
    when nil
      ->(text) { "#{text}" }
    end

    colour_mod_highlight = case e.action
    when '+'
      ->(text) { "#{fg(*black)}#{bg(*green)}#{text}#{color_reset}" }
    when '-'
      ->(text) { "#{fg(*black)}#{bg(*red)}#{text}#{color_reset}" }
    when nil
      ->(text) { "#{text}" }
    end

    case e
    when HostedZone
      information = [colour_mod.call(e.action), colour_mod_highlight.call(bold(e.name))]
    when Entry
      information = [colour_mod.call(e.action)]

      information << if e.type_highlight
        colour_mod_highlight.call(e.type)
      else
        colour_mod.call(e.type)
      end

      information << if e.name_highlight
        colour_mod_highlight.call(e.name)
      else
        colour_mod.call(e.name)
      end

      information << if e.value1_highlight
        colour_mod_highlight.call(e.value1)
      else
        colour_mod.call(e.value1)
      end

      information << if e.value2_highlight
        colour_mod_highlight.call(e.value2)
      else
        colour_mod.call(e.value2)
      end
    end

    coloured_entries << information.compact.delete_if(&:empty?)
  end

  coloured_entries
end