class EZDyn::Record

Abstraction of Dyn REST API DNS records.

Constants

DefaultTTL

Default TTL (time to live) for new records

Public Class Methods

new(client:, raw: nil, uri: nil, type: nil, fqdn: nil, value: nil, ttl: nil, record_id: nil) click to toggle source

@private

# File lib/ezdyn/record.rb, line 8
def initialize(client:, raw: nil, uri: nil, type: nil, fqdn: nil, value: nil, ttl: nil, record_id: nil)
  @client = client
  @exists = nil
  @type = RecordType.find(type)
  @fqdn = fqdn
  @value = value
  @ttl = ttl
  @in_sync = false

  if not raw.nil?
    self.sync_raw(raw)

  elsif not uri.nil?
    @uri = uri.gsub(%r{^/?(REST/)?}, "")
    if @uri !~ %r{^[A-Za-z]+Record/[^/]+/[^/]+/[0-9]+}
      raise "Invalid Record URI: '#{uri}'"
    end

    type_uri_name, zone, fqdn, record_id = @uri.split('/')

    @type = RecordType.find(type_uri_name)
    @fqdn = fqdn
    @record_id = record_id
    @zone = Zone.new(client: @client, name: zone)
    @exists = true
  end
end

Public Instance Methods

delete!() click to toggle source

Attempt to delete this record.

# File lib/ezdyn/record.rb, line 138
def delete!
  @client.delete(record: self)
end
exists?() click to toggle source

Returns whether this record existed at its last sync.

# File lib/ezdyn/record.rb, line 72
def exists?
  @exists
end
fqdn() click to toggle source

Returns the FQDN of this record.

# File lib/ezdyn/record.rb, line 49
def fqdn
  self.sync! if @fqdn.nil?
  @fqdn
end
in_sync?() click to toggle source

Returns whether this record has been synced.

# File lib/ezdyn/record.rb, line 77
def in_sync?
  @in_sync
end
record_id() click to toggle source

Returns the Dyn REST API ID for this record.

# File lib/ezdyn/record.rb, line 61
def record_id
  self.sync! if @record_id.nil?
  @record_id
end
sync!() click to toggle source

Attempts to sync the record to the API.

@raise [RuntimeError] if the record could not be synced. @raise [RuntimeError] if more than one record matches this record. @raise [RuntimeError] if returned data was not in an expected format.

# File lib/ezdyn/record.rb, line 95
def sync!
  return self if self.in_sync?

  data = @client.fetch_uri_data(uri: self.uri)
  if data.is_a? Array
    if data.count == 0
      @in_sync = true
      @exists = false
      return self

    elsif data.count > 1
      raise "More than one record was found"

    end
  end

  if data.is_a? Array and data.count == 1
    data = data.first
  end

  if data.is_a? Hash
    self.sync_raw(data)

  else
    raise "Unrecognized data: #{data.class} #{data}"
  end

  self
end
sync_raw(raw) click to toggle source

@private

# File lib/ezdyn/record.rb, line 126
def sync_raw(raw)
  @zone = Zone.new(client: @client, name: raw["zone"])
  @ttl = raw["ttl"]
  @fqdn = raw["fqdn"]
  @type = RecordType.find(raw["record_type"])
  @record_id = raw["record_id"].to_s
  @value = Array(@type.value_key).map { |k| raw["rdata"][k] }.join(' ')
  @in_sync = true
  @exists = true
end
ttl() click to toggle source

Returns the TTL of this record.

# File lib/ezdyn/record.rb, line 43
def ttl
  self.sync! if @ttl.nil?
  @ttl
end
type() click to toggle source

Returns the record type.

# File lib/ezdyn/record.rb, line 37
def type
  self.sync! if @type.nil?
  @type
end
uri() click to toggle source

@private

# File lib/ezdyn/record.rb, line 82
def uri
  if @uri.nil?
    "#{self.type.uri_name}/#{self.zone.name}/#{self.fqdn}/#{self.record_id}"
  else
    @uri
  end
end
value() click to toggle source

Returns the record data value.

# File lib/ezdyn/record.rb, line 55
def value
  self.sync! if @value.nil?
  @value
end
zone() click to toggle source

Returns the zone of this record.

# File lib/ezdyn/record.rb, line 67
def zone
  @zone ||= @client.guess_zone(fqdn: self.fqdn)
end