Class: SprinkleDNS::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dns_provider, dry_run: false, diff: true, force: true, delete: false, interactive_progress: true, create_hosted_zones: false, show_untouched: false) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sprinkle_dns/client.rb', line 20

def initialize(dns_provider, dry_run: false, diff: true, force: true, delete: false, interactive_progress: true, create_hosted_zones: false, show_untouched: false)
  @config = SprinkleDNS::Config.new(
    dry_run: dry_run,
    diff: diff,
    force: force,
    delete: delete,
    interactive_progress: interactive_progress,
    create_hosted_zones: create_hosted_zones,
    show_untouched: show_untouched,
  )
  @dns_provider = dns_provider
  @wanted_hosted_zones = []

  @progress_printer = if @config.interactive_progress?
    SprinkleDNS::CLI::InteractiveChangeRequestPrinter.new
  else
    SprinkleDNS::CLI::PropagatedChangeRequestPrinter.new
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/sprinkle_dns/client.rb', line 18

def config
  @config
end

#wanted_hosted_zonesObject (readonly)

Returns the value of attribute wanted_hosted_zones.



18
19
20
# File 'lib/sprinkle_dns/client.rb', line 18

def wanted_hosted_zones
  @wanted_hosted_zones
end

Instance Method Details

#alias(type, name, hosted_zone_id, dns_name, hosted_zone = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/sprinkle_dns/client.rb', line 51

def alias(type, name, hosted_zone_id, dns_name, hosted_zone = nil)
  hosted_zone = find_or_init_hosted_zone(name, hosted_zone)
  name        = zonify!(name)
  dns_name    = zonify!(dns_name)

  hosted_zone.add_or_update_hosted_zone_entry(HostedZoneAlias.new(type, name, hosted_zone_id, dns_name, hosted_zone.name))
end

#compareObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sprinkle_dns/client.rb', line 59

def compare
  existing_hosted_zones = @dns_provider.fetch_hosted_zones(filter: @wanted_hosted_zones.map(&:name))

  # Tell our existing hosted zones about our wanted changes
  existing_hosted_zones.each do |existing_hosted_zone|
    wanted_hosted_zone = @wanted_hosted_zones.find{|whz| whz.name == existing_hosted_zone.name}

    wanted_hosted_zone.resource_record_sets.each do |entry|
      existing_hosted_zone.add_or_update_hosted_zone_entry(entry)
    end
  end

  [@wanted_hosted_zones, existing_hosted_zones]
end

#entry(type, name, value, ttl = 3600, hosted_zone = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/sprinkle_dns/client.rb', line 40

def entry(type, name, value, ttl = 3600, hosted_zone = nil)
  hosted_zone = find_or_init_hosted_zone(name, hosted_zone)
  name        = zonify!(name)

  if ['CNAME', 'MX'].include?(type)
    value = Array.wrap(value)
    value.map!{|v| zonify!(v)}
  end
  hosted_zone.add_or_update_hosted_zone_entry(HostedZoneEntry.new(type, name, Array.wrap(value), ttl, hosted_zone.name))
end

#sprinkle!Object



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
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
# File 'lib/sprinkle_dns/client.rb', line 74

def sprinkle!
  wanted_hosted_zones, existing_hosted_zones = compare

  missing_hosted_zone_names = wanted_hosted_zones.map(&:name) - existing_hosted_zones.map(&:name)
  missing_hosted_zones = wanted_hosted_zones.select{|whz| missing_hosted_zone_names.include?(whz.name)}

  if missing_hosted_zones.any? && !@config.create_hosted_zones?
    missing_hosted_zones_error(missing_hosted_zones)
  end

  if @config.diff?
    SprinkleDNS::CLI::HostedZoneDiff.new.diff(existing_hosted_zones, missing_hosted_zones, @config).each do |line|
      puts line.join(' ')
    end
  end

  if @config.dry_run?
    return [existing_hosted_zones, nil]
  end

  hosted_zones = (existing_hosted_zones + missing_hosted_zones)

  unless @config.force?
    changes = hosted_zones.map{|h| SprinkleDNS::EntryPolicyService.new(h, @config)}.collect{|eps| eps.entries_to_change}.sum

    if missing_hosted_zones.any? || changes > 0
      messages = []
      messages << "#{missing_hosted_zones.size} hosted-zone(s) to create" if missing_hosted_zones.any?
      messages << "#{changes} change(s) to make" if changes > 0
      print messages.join(' and ').concat(". Continue? (y/N)")

      case gets.strip
      when 'y', 'Y'
        # continue
      else
        puts ".. exiting!"
        return [hosted_zones, nil]
      end
    else
      puts "No changes to make, everything up to date!"
      puts ".. exiting!"
      return [hosted_zones, nil]
    end
  end

  # Create missing hosted zones
  change_requests_hosted_zones = @dns_provider.create_hosted_zones(missing_hosted_zones)
  if change_requests_hosted_zones.any?
    puts
    puts "Creating hosted zones:"
    @progress_printer.reset!
    begin
      @dns_provider.check_change_requests(change_requests_hosted_zones)
      @progress_printer.draw(change_requests_hosted_zones, 'CREATING', 'CREATED')
    end until change_requests_hosted_zones.all?{|cr| cr.in_sync}
  end

  # Update hosted zones
  change_requests_entries = @dns_provider.change_hosted_zones(hosted_zones, @config)
  if change_requests_entries.any?
    puts
    puts "Updating hosted zones:"
    @progress_printer.reset!
    begin
      @dns_provider.check_change_requests(change_requests_entries)
      @progress_printer.draw(change_requests_entries, 'UPDATING', 'UPDATED')
    end until change_requests_entries.all?{|cr| cr.in_sync}
  end

  change_requests = change_requests_hosted_zones + change_requests_entries

  [hosted_zones, change_requests]
end