Class: MythicBeasts::DNS

Inherits:
Object
  • Object
show all
Defined in:
lib/mythic_beasts/dns.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ DNS

Returns a new instance of DNS.



5
6
7
# File 'lib/mythic_beasts/dns.rb', line 5

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/mythic_beasts/dns.rb', line 3

def client
  @client
end

Instance Method Details

#create_a_record(zone, host, ip, ttl: 300) ⇒ Object

Convenience methods for common record types



56
57
58
# File 'lib/mythic_beasts/dns.rb', line 56

def create_a_record(zone, host, ip, ttl: 300)
  create_records(zone, [{host: host, ttl: ttl, type: "A", data: ip}])
end

#create_aaaa_record(zone, host, ip, ttl: 300) ⇒ Object



60
61
62
# File 'lib/mythic_beasts/dns.rb', line 60

def create_aaaa_record(zone, host, ip, ttl: 300)
  create_records(zone, [{host: host, ttl: ttl, type: "AAAA", data: ip}])
end

#create_cname_record(zone, host, target, ttl: 300) ⇒ Object



64
65
66
# File 'lib/mythic_beasts/dns.rb', line 64

def create_cname_record(zone, host, target, ttl: 300)
  create_records(zone, [{host: host, ttl: ttl, type: "CNAME", data: target}])
end

#create_mx_record(zone, host, priority, mail_server, ttl: 300) ⇒ Object



68
69
70
# File 'lib/mythic_beasts/dns.rb', line 68

def create_mx_record(zone, host, priority, mail_server, ttl: 300)
  create_records(zone, [{host: host, ttl: ttl, type: "MX", data: "#{priority} #{mail_server}"}])
end

#create_records(zone, records) ⇒ Object

Create new DNS record(s) records: Array of hashes with keys: host, ttl, type, data Example: [{ host: ‘www’, ttl: 300, type: ‘A’, data: ‘1.2.3.4’ }]



27
28
29
# File 'lib/mythic_beasts/dns.rb', line 27

def create_records(zone, records)
  client.post("/dns/v2/zones/#{zone}/records", body: {records: records})
end

#create_txt_record(zone, host, text, ttl: 300) ⇒ Object



72
73
74
# File 'lib/mythic_beasts/dns.rb', line 72

def create_txt_record(zone, host, text, ttl: 300)
  create_records(zone, [{host: host, ttl: ttl, type: "TXT", data: text}])
end

#delete_records(zone, host: nil, type: nil, data: nil) ⇒ Object

Delete record(s)



41
42
43
44
45
46
47
48
# File 'lib/mythic_beasts/dns.rb', line 41

def delete_records(zone, host: nil, type: nil, data: nil)
  path = "/dns/v2/zones/#{zone}/records"
  path += "/#{host}" if host
  path += "/#{type}" if type

  params = data ? {data: data} : {}
  client.delete(path, params: params)
end

#dynamic_update(host) ⇒ Object

Dynamic DNS update - updates A/AAAA record to client IP



51
52
53
# File 'lib/mythic_beasts/dns.rb', line 51

def dynamic_update(host)
  client.put("/dns/v2/dynamic/#{host}")
end

#get_record(zone, host, type) ⇒ Object

Get specific record(s) by host and type



20
21
22
# File 'lib/mythic_beasts/dns.rb', line 20

def get_record(zone, host, type)
  client.get("/dns/v2/zones/#{zone}/records/#{host}/#{type}")
end

#records(zone) ⇒ Object

Get all records for a zone



15
16
17
# File 'lib/mythic_beasts/dns.rb', line 15

def records(zone)
  client.get("/dns/v2/zones/#{zone}/records")
end

#update_records(zone, records, host: nil, type: nil) ⇒ Object

Replace record(s)



32
33
34
35
36
37
38
# File 'lib/mythic_beasts/dns.rb', line 32

def update_records(zone, records, host: nil, type: nil)
  path = "/dns/v2/zones/#{zone}/records"
  path += "/#{host}" if host
  path += "/#{type}" if type

  client.put(path, body: {records: records})
end

#zonesObject

List all DNS zones



10
11
12
# File 'lib/mythic_beasts/dns.rb', line 10

def zones
  client.get("/dns/v2/zones")
end