Class: Pdns::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pdns/client.rb,
lib/pdns/client/error.rb,
lib/pdns/client/version.rb

Defined Under Namespace

Classes: Error, OptimisticLockFailure

Constant Summary collapse

VERSION =
"0.3.0"

Instance Method Summary collapse

Constructor Details

#initialize(url:, token:, http: HTTP) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
# File 'lib/pdns/client.rb', line 8

def initialize(url:, token:, http: HTTP)
  @url = url
  @http = http
  @token = token
end

Instance Method Details

#create_zone(zone_name, nameserver_addresses) ⇒ Object

Only for testing



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pdns/client.rb', line 30

def create_zone(zone_name, nameserver_addresses)
  unwrap {
    http.post(@url + "api/v1/servers/localhost/zones", json: {
      "kind": "Native",
      "masters": [],
      "id": "#{zone_name}",
      "name": "#{zone_name}.",
      "nameservers": nameserver_addresses,
      "records": [
          {
            "content": "ns.#{zone_name}. hostmaster.#{zone_name}. 1 1800 900 604800 86400",
              "disabled": false,
              "name": "#{zone_name}.",
              "ttl": 86400,
              "type": "SOA"
          },
      ]
    })
  }
end

#delete_record(zone_name, lvalue, type) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pdns/client.rb', line 14

def delete_record(zone_name, lvalue, type)
  unwrap {
    http.patch(@url + "api/v1/servers/localhost/zones/#{zone_name}", json: {
      "rrsets": [
        {
          "name": "#{lvalue}.#{zone_name}.",
          "type": type,
          "changetype": "DELETE",
          "records": [],
        }
      ]
    })
  }
end

#delete_zone(zone_name) ⇒ Object

Only for testing



52
53
54
# File 'lib/pdns/client.rb', line 52

def delete_zone(zone_name)
  unwrap { http.delete(@url + "api/v1/servers/localhost/zones/#{zone_name}") }
end

#get_zone(zone_name) ⇒ Object

Only for testing



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

def get_zone(zone_name)
  native = unwrap { http.get(@url + "api/v1/servers/localhost/zones/#{zone_name}") }

  dapi_compatible = native["rrsets"].map { |rrset|
    rrset["records"].map { |record|
      {
        "type" => rrset["type"],
        "name" => rrset["name"].chomp(".#{zone_name}."),
        "value" => record["content"],
      }
    }
  }.flatten
  {"records" => dapi_compatible}
end

#upsert_record(zone_name, lvalue, type, rvalues, ttl = 60) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pdns/client.rb', line 72

def upsert_record(zone_name, lvalue, type, rvalues, ttl = 60)
  name = if lvalue.nil? || (lvalue == '')
    "#{zone_name}."
  else
    "#{lvalue}.#{zone_name}."
  end
  rvalues = [rvalues].flatten
  unwrap {
    http.patch(@url + "api/v1/servers/localhost/zones/#{zone_name}", json: {
      "rrsets": [
        {
          "name": name,
          "type": type,
          "ttl": ttl,
          "changetype": "REPLACE",
          "records": rvalues.map do |rvalue|
            {
              "content": rvalue,
              "disabled": false,
              "name": name,
              "type": type,
              "priority": 0
            }
          end
        }
      ]
    })
  }
end