Class: EZDyn::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/ezdyn/record.rb

Overview

Abstraction of Dyn REST API DNS records.

Constant Summary collapse

DefaultTTL =

Default TTL (time to live) for new records

300

Instance Method Summary collapse

Constructor Details

#initialize(client:, raw: nil, uri: nil, type: nil, fqdn: nil, value: nil, ttl: nil, record_id: nil) ⇒ Record

Returns a new instance of Record.



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
# 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

Instance Method Details

#delete!Object

Attempt to delete this record.



138
139
140
# File 'lib/ezdyn/record.rb', line 138

def delete!
  @client.delete(record: self)
end

#exists?Boolean

Returns whether this record existed at its last sync.

Returns:

  • (Boolean)


72
73
74
# File 'lib/ezdyn/record.rb', line 72

def exists?
  @exists
end

#fqdnObject

Returns the FQDN of this record.



49
50
51
52
# File 'lib/ezdyn/record.rb', line 49

def fqdn
  self.sync! if @fqdn.nil?
  @fqdn
end

#in_sync?Boolean

Returns whether this record has been synced.

Returns:

  • (Boolean)


77
78
79
# File 'lib/ezdyn/record.rb', line 77

def in_sync?
  @in_sync
end

#record_idObject

Returns the Dyn REST API ID for this record.



61
62
63
64
# File 'lib/ezdyn/record.rb', line 61

def record_id
  self.sync! if @record_id.nil?
  @record_id
end

#sync!Object

Attempts to sync the record to the API.

Raises:

  • (RuntimeError)

    if the record could not be synced.

  • (RuntimeError)

    if more than one record matches this record.

  • (RuntimeError)

    if returned data was not in an expected format.



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
# 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) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# 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

#ttlObject

Returns the TTL of this record.



43
44
45
46
# File 'lib/ezdyn/record.rb', line 43

def ttl
  self.sync! if @ttl.nil?
  @ttl
end

#typeObject

Returns the record type.



37
38
39
40
# File 'lib/ezdyn/record.rb', line 37

def type
  self.sync! if @type.nil?
  @type
end

#uriObject



82
83
84
85
86
87
88
# 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

#valueObject

Returns the record data value.



55
56
57
58
# File 'lib/ezdyn/record.rb', line 55

def value
  self.sync! if @value.nil?
  @value
end

#zoneObject

Returns the zone of this record.



67
68
69
# File 'lib/ezdyn/record.rb', line 67

def zone
  @zone ||= @client.guess_zone(fqdn: self.fqdn)
end