Class: Roadworker::Route53Wrapper::ResourceRecordSetWrapper

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/roadworker/route53-wrapper.rb

Overview

ResourceRecordSetCollectionWrapper

Instance Method Summary collapse

Methods included from Log

#log

Constructor Details

#initialize(resource_record_set, hosted_zone, options) ⇒ ResourceRecordSetWrapper

Returns a new instance of ResourceRecordSetWrapper.



157
158
159
160
161
# File 'lib/roadworker/route53-wrapper.rb', line 157

def initialize(resource_record_set, hosted_zone, options)
  @resource_record_set = resource_record_set
  @hosted_zone = hosted_zone
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



280
281
282
# File 'lib/roadworker/route53-wrapper.rb', line 280

def method_missing(method_name, *args)
  @resource_record_set.send(method_name, *args)
end

Instance Method Details

#deleteObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/roadworker/route53-wrapper.rb', line 222

def delete
  if self.type =~ /\A(SOA|NS)\Z/i
    hz_name = (@hosted_zone.name || @options.hosted_zone_name).downcase.sub(/\.\Z/, '')
    rrs_name = @resource_record_set.name.downcase.sub(/\.\Z/, '')
    return if hz_name == rrs_name
  end

  log(:info, 'Delete ResourceRecordSet', :red) do
    log_id = [self.name, self.type].join(' ')
    rrset_setid = self.set_identifier
    rrset_setid ? (log_id + " (#{rrset_setid})") : log_id
  end

  unless @options.dry_run
    @resource_record_set.delete
    @options.updated = true
  end
end

#dns_nameObject



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/roadworker/route53-wrapper.rb', line 246

def dns_name
  alias_target = @resource_record_set.alias_target || {}
  dns_name = alias_target[:dns_name]

  if dns_name
    [
      dns_name,
      AWS::Route53.normalize_dns_name_options(alias_target),
    ]
  else
    nil
  end
end

#dns_name=(value) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/roadworker/route53-wrapper.rb', line 260

def dns_name=(value)
  if value
    dns_name, dns_name_opts = value
    @resource_record_set.alias_target = AWS::Route53.dns_name_to_alias_target(dns_name, dns_name_opts, @hosted_zone.id, @hosted_zone.name || @options.hosted_zone_name)
  else
    @resource_record_set.alias_target = nil
  end
end

#eql?(expected_record) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/roadworker/route53-wrapper.rb', line 163

def eql?(expected_record)
  Route53Wrapper::RRSET_ATTRS_WITH_TYPE.all? do |attribute|
    expected = expected_record.send(attribute)
    expected = expected.sort_by {|i| i.to_s } if expected.kind_of?(Array)
    expected = nil if expected.kind_of?(Array) && expected.empty?
    actual = self.send(attribute)
    actual = actual.sort_by {|i| i.to_s } if actual.kind_of?(Array)
    actual = nil if actual.kind_of?(Array) && actual.empty?

    if !expected and !actual
      true
    elsif expected and actual
      case attribute
      when :dns_name
        expected[0] = expected[0].downcase.sub(/\.\Z/, '')
        actual[0] = actual[0].downcase.sub(/\.\Z/, '')
      end

      (expected == actual)
    else
      false
    end
  end
end

#health_checkObject



269
270
271
# File 'lib/roadworker/route53-wrapper.rb', line 269

def health_check
  @options.health_checks[@resource_record_set.health_check_id]
end

#health_check=(check) ⇒ Object



273
274
275
276
# File 'lib/roadworker/route53-wrapper.rb', line 273

def health_check=(check)
  health_check_id = check ? @options.health_checks.find_or_create(check) : nil
  @resource_record_set.health_check_id = health_check_id
end

#nameObject



241
242
243
244
# File 'lib/roadworker/route53-wrapper.rb', line 241

def name
  value = @resource_record_set.name
  value ? value.gsub("\\052", '*') : value
end

#update(expected_record) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/roadworker/route53-wrapper.rb', line 188

def update(expected_record)
  log_id_proc = proc do
    log_id = [self.name, self.type].join(' ')
    rrset_setid = self.set_identifier
    rrset_setid ? (log_id + " (#{rrset_setid})") : log_id
  end

  log(:info, 'Update ResourceRecordSet', :green, &log_id_proc)

  Route53Wrapper::RRSET_ATTRS_WITH_TYPE.each do |attribute|
    expected = expected_record.send(attribute)
    expected = expected.sort_by {|i| i.to_s } if expected.kind_of?(Array)
    expected = nil if expected.kind_of?(Array) && expected.empty?
    actual = self.send(attribute)
    actual = actual.sort_by {|i| i.to_s } if actual.kind_of?(Array)
    actual = nil if actual.kind_of?(Array) && actual.empty?

    if (expected and !actual) or (!expected and actual)
      log(:info, "  set #{attribute}=#{expected.inspect}" , :green)
      self.send(:"#{attribute}=", expected) unless @options.dry_run
    elsif expected and actual
      if expected != actual
        log(:info, "  set #{attribute}=#{expected.inspect}" , :green)
        self.send(:"#{attribute}=", expected) unless @options.dry_run
      end
    end
  end

  unless @options.dry_run
    @resource_record_set.update
    @options.updated = true
  end
end