Class: DNSimple::Pdns

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsimple-pdns.rb

Defined Under Namespace

Classes: API

Instance Method Summary collapse

Constructor Details

#initialize(db_path, nameservers) ⇒ Pdns

Returns a new instance of Pdns.



6
7
8
9
# File 'lib/dnsimple-pdns.rb', line 6

def initialize( db_path, nameservers )
  @db_path = db_path
  @nameservers = nameservers
end

Instance Method Details

#create_domain(domain) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dnsimple-pdns.rb', line 15

def create_domain( domain )

  nameserver = @nameservers.first
  email = "dnsadmin.#{domain}"
  serial = Time.now.strftime( "%Y%m%d%H%M%S" )
  ttl = 3600

  begin
    db.execute "insert into domains " +
      "(name, type) values ('#{domain}', 'NATIVE')"

    # Start of authority (SOA) record
    db.execute "insert into records " +
      "(domain_id, name, type, content, ttl, prio) " +
      "select id, '#{domain}', 'SOA', " +
      "'#{nameserver} #{email} #{serial} 3600 600 86400', #{ttl}, 0 " +
      "from domains where name='#{domain}'"

    # Name server record
    db.execute "insert into records " +
      "(domain_id, name, type, content, ttl, prio) " +
      "select id, '#{domain}', 'NS', '#{nameserver}', #{ttl}, 0 " +
      "from domains where name='#{domain}'"
  rescue SQLite3::ConstraintException
    "Domain already exists."
  end

  "Domain created."
end

#create_record(params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dnsimple-pdns.rb', line 57

def create_record( params )
  name = record_name( params )
  type = params[:record_type]
  content = params[:content]
  ttl = params[:ttl]
  prio = params[:prio] ||= 0

  db.execute "insert into records " +
    "(domain_id, name, type, content, ttl, prio) " +
    "select id, '#{name}', '#{type}', '#{content}', #{ttl}, #{prio} " +
    "from domains where name = '#{params[:domain]}'"

  "Record created."
end

#dbObject



11
12
13
# File 'lib/dnsimple-pdns.rb', line 11

def db
  @db ||= SQLite3::Database.new @db_path
end

#delete_domain(domain) ⇒ Object



45
46
47
48
49
# File 'lib/dnsimple-pdns.rb', line 45

def delete_domain( domain )
  db.execute "delete from records where name = '#{domain}'"
  db.execute "delete from domains where name = '#{domain}'"
  "Domain deleted."
end

#delete_record(params) ⇒ Object



89
90
91
92
93
94
# File 'lib/dnsimple-pdns.rb', line 89

def delete_record( params )
  db.execute "delete from records " +
    "where type = '#{params[:record_type]}' " +
    "and name = '#{record_name( params )}'"
  "Record deleted."
end

#list_name_servers(domain) ⇒ Object



51
52
53
54
55
# File 'lib/dnsimple-pdns.rb', line 51

def list_name_servers( domain )
  db.execute( "select content from records " +
              "where type = 'NS' and " +
              "name = '#{domain}'" ).flatten
end

#update_record(params) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dnsimple-pdns.rb', line 72

def update_record( params )
  name = record_name( params )
  type = params[:record_type]
  previous_content = params[:previous_content]
  content = params[:content]
  ttl = params[:ttl]
  prio = params[:prio] ||= 0

  db.execute "update records set " +
    "content = '#{content}', ttl = #{ttl}, prio = #{prio} " +
    "where type = '#{type}' " +
    "and content = '#{previous_content}' " +
    "and name = '#{name}'"

  "Record updated."
end