Class: EZDyn::Record
- Inherits:
-
Object
- Object
- EZDyn::Record
- 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
-
#delete! ⇒ Object
Attempt to delete this record.
-
#exists? ⇒ Boolean
Returns whether this record existed at its last sync.
-
#fqdn ⇒ Object
Returns the FQDN of this record.
-
#in_sync? ⇒ Boolean
Returns whether this record has been synced.
-
#initialize(client:, raw: nil, uri: nil, type: nil, fqdn: nil, value: nil, ttl: nil, record_id: nil) ⇒ Record
constructor
A new instance of Record.
-
#record_id ⇒ Object
Returns the Dyn REST API ID for this record.
-
#sync! ⇒ Object
Attempts to sync the record to the API.
- #sync_raw(raw) ⇒ Object
-
#ttl ⇒ Object
Returns the TTL of this record.
-
#type ⇒ Object
Returns the record type.
- #uri ⇒ Object
-
#value ⇒ Object
Returns the record data value.
-
#zone ⇒ Object
Returns the zone of this record.
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.
72 73 74 |
# File 'lib/ezdyn/record.rb', line 72 def exists? @exists end |
#fqdn ⇒ Object
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.
77 78 79 |
# File 'lib/ezdyn/record.rb', line 77 def in_sync? @in_sync end |
#record_id ⇒ Object
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.
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 |
#ttl ⇒ Object
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 |
#type ⇒ Object
Returns the record type.
37 38 39 40 |
# File 'lib/ezdyn/record.rb', line 37 def type self.sync! if @type.nil? @type end |
#uri ⇒ Object
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 |
#value ⇒ Object
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 |
#zone ⇒ Object
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 |