Class: Dnsync::Nsone

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsync/nsone.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, domain) ⇒ Nsone

Returns a new instance of Nsone.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/dnsync/nsone.rb', line 3

def initialize(api_key, domain)
  unless api_key.present?
    raise ArgumentError, "api_key must be specified"
  end

  unless domain.present?
    raise ArgumentError, "domain must be specified"
  end
  
  @api_key = api_key
  @domain  = domain
end

Instance Method Details

#connectionObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dnsync/nsone.rb', line 16

def connection
  @connection ||= Faraday.new('https://api.nsone.net/v1/') do |conn|
    conn.request :json

    # conn.response :logger
    conn.response :raise_error
    conn.response :json, :content_type => /\bjson$/

    conn.adapter Faraday.default_adapter

    conn.headers['X-NSONE-Key'] = @api_key

    conn.options.timeout      = 5
    conn.options.open_timeout = 5
  end
end

#create_record(record) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dnsync/nsone.rb', line 62

def create_record(record)
  answers = record.answers.map do |answer|
    if answer.priority
      { :answer => [ answer.priority, answer.content ] }
    else
      { :answer => [ answer.content ] }
    end
  end

  connection.put("zones/#{@domain}/#{record.name}/#{record.type}") do |req|
    req.body = {
      :type    => record.type,
      :zone    => @domain,
      :domain  => record.name,
      :ttl     => record.ttl,
      :answers => answers
    }
  end
end

#record_for(fqdn, record_type) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dnsync/nsone.rb', line 43

def record_for(fqdn, record_type)
  record = connection.get("zones/#{@domain}/#{fqdn}/#{record_type}").body
  
  answers = record['answers'].map do |answer_record|
    case answer_record['answer'].length
    when 2
      priority, content = *answer_record['answer']
    when 1
      content = answer_record['answer'].first
    else
      raise "Unknown answer format: #{answer_record.inspect}"
    end
    
    Answer.new(content, priority)
  end
  
  Record.new(record['domain'], record['type'], record['ttl'], answers)
end

#remove_record(record) ⇒ Object



102
103
104
# File 'lib/dnsync/nsone.rb', line 102

def remove_record(record)
  connection.delete("zones/#{@domain}/#{record.name}/#{record.type}")
end

#update_record(record) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dnsync/nsone.rb', line 82

def update_record(record)
  answers = record.answers.map do |answer|
    if answer.priority
      { :answer => [ answer.priority, answer.content ] }
    else
      { :answer => [ answer.content ] }
    end
  end

  connection.post("zones/#{@domain}/#{record.name}/#{record.type}") do |req|
    req.body = {
      :type    => record.type,
      :zone    => @domain,
      :domain  => record.name,
      :ttl     => record.ttl,
      :answers => answers
    }
  end
end

#zoneObject



33
34
35
36
37
38
39
40
41
# File 'lib/dnsync/nsone.rb', line 33

def zone
  zone = connection.get("zones/#{@domain}").body
  
  records = zone['records'].map do |record|
    record_for(record['domain'], record['type'])
  end
  
  Zone.new(@domain, records)
end