pdns-ruby 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/LICENSE +20 -0
- data/README.md +108 -0
- data/lib/pdns_ruby/client.rb +108 -0
- data/lib/pdns_ruby/rrset.rb +47 -0
- data/lib/pdns_ruby/version.rb +5 -0
- data/lib/pdns_ruby/zone.rb +48 -0
- data/lib/pdns_ruby.rb +10 -0
- metadata +49 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: deba7d8f561778d0cf86565dd6ada6215ae23ba16752a3f0e60ed011e714ed79
|
|
4
|
+
data.tar.gz: e32a1ffa09201317c0116b020e2f987195d8c3ad7532036d9d3bb636a2d4f908
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 52603c97bef0ae508fbd0c709699391f77effe10212994b0da93e4e1b54ad3615c8e25054666222d5f929bc2e254d4241d69cc91defd920dbe7aa19b9ce8971d
|
|
7
|
+
data.tar.gz: 1633ba2fd6b9322474d2a284d51063b2cb77b71f868ee0707b424249f07806060e704691c558dd9692f7048e2984afc8c3b59da32ae00498c6a1e8c1be0bee02
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0 (2026-04-03)
|
|
4
|
+
|
|
5
|
+
Initial release.
|
|
6
|
+
|
|
7
|
+
- Zone management (list, get, create, delete)
|
|
8
|
+
- Record management via rrsets (add, update, delete, batch)
|
|
9
|
+
- Typed error classes (NotFoundError, AuthError, ValidationError)
|
|
10
|
+
- Zero external dependencies
|
|
11
|
+
- PowerDNS 4.x API support
|
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2026 Vibol Teav
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# pdns-ruby
|
|
2
|
+
|
|
3
|
+
A simple Ruby client for the [PowerDNS](https://www.powerdns.com/) HTTP API. Zero dependencies beyond Ruby's standard library.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem 'pdns-ruby'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
require 'pdns_ruby'
|
|
15
|
+
|
|
16
|
+
client = PdnsRuby::Client.new(
|
|
17
|
+
host: 'http://localhost:8081',
|
|
18
|
+
api_key: 'your-api-key'
|
|
19
|
+
)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Zones
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
# list all zones
|
|
26
|
+
client.zones
|
|
27
|
+
|
|
28
|
+
# get a zone
|
|
29
|
+
zone = client.zone('example.com')
|
|
30
|
+
|
|
31
|
+
# check if zone exists
|
|
32
|
+
client.zone_exists?('example.com')
|
|
33
|
+
|
|
34
|
+
# create a zone
|
|
35
|
+
zone = client.create_zone('example.com', nameservers: ['ns1.example.com', 'ns2.example.com'])
|
|
36
|
+
|
|
37
|
+
# delete a zone
|
|
38
|
+
client.delete_zone('example.com')
|
|
39
|
+
# or
|
|
40
|
+
zone.delete!
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Records
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
zone = client.zone('example.com')
|
|
47
|
+
|
|
48
|
+
# add/update a record
|
|
49
|
+
zone.add_record('www.example.com.', 'A', '1.2.3.4', ttl: 3600)
|
|
50
|
+
|
|
51
|
+
# add MX record
|
|
52
|
+
zone.add_record('example.com.', 'MX', '10 mail.example.com.', ttl: 3600)
|
|
53
|
+
|
|
54
|
+
# delete a record
|
|
55
|
+
zone.delete_record('www.example.com.', 'A')
|
|
56
|
+
|
|
57
|
+
# get all rrsets
|
|
58
|
+
zone.rrsets
|
|
59
|
+
|
|
60
|
+
# find specific rrset
|
|
61
|
+
zone.find_rrset('www.example.com.', 'A')
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Batch Updates
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
rrsets = [
|
|
68
|
+
PdnsRuby::Rrset.replace(
|
|
69
|
+
name: 'www.example.com.',
|
|
70
|
+
type: 'A',
|
|
71
|
+
ttl: 3600,
|
|
72
|
+
records: [{ content: '1.2.3.4' }, { content: '5.6.7.8' }]
|
|
73
|
+
),
|
|
74
|
+
PdnsRuby::Rrset.delete(
|
|
75
|
+
name: 'old.example.com.',
|
|
76
|
+
type: 'CNAME'
|
|
77
|
+
)
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
client.update_rrsets('example.com', rrsets)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Error Handling
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
begin
|
|
87
|
+
client.zone('nonexistent.com')
|
|
88
|
+
rescue PdnsRuby::NotFoundError => e
|
|
89
|
+
puts "Zone not found"
|
|
90
|
+
rescue PdnsRuby::AuthError => e
|
|
91
|
+
puts "Invalid API key"
|
|
92
|
+
rescue PdnsRuby::ApiError => e
|
|
93
|
+
puts "API error: #{e.message}"
|
|
94
|
+
end
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Features
|
|
98
|
+
|
|
99
|
+
- Zero external dependencies — uses only Ruby's `net/http` and `json`
|
|
100
|
+
- Zone management (list, get, create, delete)
|
|
101
|
+
- Record management via rrsets (add, update, delete, batch)
|
|
102
|
+
- Automatic trailing dot normalization for domain names
|
|
103
|
+
- Typed error classes for easy error handling
|
|
104
|
+
- Works with PowerDNS 4.x API
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
MIT
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PdnsRuby
|
|
4
|
+
class Client
|
|
5
|
+
attr_reader :host, :api_key, :server_id
|
|
6
|
+
|
|
7
|
+
def initialize(host:, api_key:, server_id: 'localhost')
|
|
8
|
+
@host = host.chomp('/')
|
|
9
|
+
@api_key = api_key
|
|
10
|
+
@server_id = server_id
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def zones
|
|
14
|
+
response = get("/api/v1/servers/#{server_id}/zones")
|
|
15
|
+
response.map { |z| Zone.new(self, z) }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def zone(name)
|
|
19
|
+
data = get("/api/v1/servers/#{server_id}/zones/#{normalize(name)}")
|
|
20
|
+
Zone.new(self, data)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def zone_exists?(name)
|
|
24
|
+
zone(name)
|
|
25
|
+
true
|
|
26
|
+
rescue NotFoundError
|
|
27
|
+
false
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def create_zone(name, nameservers: [], kind: 'Native', soa_edit_api: 'DEFAULT')
|
|
31
|
+
data = post("/api/v1/servers/#{server_id}/zones", {
|
|
32
|
+
name: normalize(name),
|
|
33
|
+
nameservers: nameservers.map { |ns| normalize(ns) },
|
|
34
|
+
rrsets: [],
|
|
35
|
+
kind: kind,
|
|
36
|
+
soa_edit_api: soa_edit_api
|
|
37
|
+
})
|
|
38
|
+
Zone.new(self, data)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def delete_zone(name)
|
|
42
|
+
delete("/api/v1/servers/#{server_id}/zones/#{normalize(name)}")
|
|
43
|
+
true
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def update_rrsets(zone_name, rrsets)
|
|
47
|
+
patch("/api/v1/servers/#{server_id}/zones/#{normalize(zone_name)}", {
|
|
48
|
+
rrsets: rrsets.map(&:to_h)
|
|
49
|
+
})
|
|
50
|
+
true
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# HTTP methods
|
|
54
|
+
|
|
55
|
+
def get(path)
|
|
56
|
+
request(Net::HTTP::Get, path)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def post(path, body)
|
|
60
|
+
request(Net::HTTP::Post, path, body)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def patch(path, body)
|
|
64
|
+
request(Net::HTTP::Patch, path, body)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def delete(path)
|
|
68
|
+
request(Net::HTTP::Delete, path)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def request(method_class, path, body = nil)
|
|
74
|
+
uri = URI("#{host}#{path}")
|
|
75
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
76
|
+
http.use_ssl = uri.scheme == 'https'
|
|
77
|
+
|
|
78
|
+
req = method_class.new(uri)
|
|
79
|
+
req['X-API-Key'] = api_key
|
|
80
|
+
req['Content-Type'] = 'application/json'
|
|
81
|
+
req.body = body.to_json if body
|
|
82
|
+
|
|
83
|
+
response = http.request(req)
|
|
84
|
+
|
|
85
|
+
case response.code.to_i
|
|
86
|
+
when 200..299
|
|
87
|
+
response.body && !response.body.empty? ? JSON.parse(response.body) : nil
|
|
88
|
+
when 404
|
|
89
|
+
raise NotFoundError, "Not found: #{path}"
|
|
90
|
+
when 401, 403
|
|
91
|
+
raise AuthError, "Unauthorized: #{response.body}"
|
|
92
|
+
when 422
|
|
93
|
+
raise ValidationError, "Validation failed: #{response.body}"
|
|
94
|
+
else
|
|
95
|
+
raise ApiError, "API error #{response.code}: #{response.body}"
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def normalize(name)
|
|
100
|
+
name.end_with?('.') ? name : "#{name}."
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
class ApiError < StandardError; end
|
|
105
|
+
class NotFoundError < ApiError; end
|
|
106
|
+
class AuthError < ApiError; end
|
|
107
|
+
class ValidationError < ApiError; end
|
|
108
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PdnsRuby
|
|
4
|
+
class Rrset
|
|
5
|
+
attr_accessor :name, :type, :ttl, :changetype, :records
|
|
6
|
+
|
|
7
|
+
def initialize(name:, type:, ttl: 3600, changetype: 'REPLACE', records: [])
|
|
8
|
+
@name = name
|
|
9
|
+
@type = type
|
|
10
|
+
@ttl = ttl
|
|
11
|
+
@changetype = changetype
|
|
12
|
+
@records = records
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.replace(name:, type:, ttl: 3600, records: [])
|
|
16
|
+
new(name: name, type: type, ttl: ttl, changetype: 'REPLACE', records: records.map { |r|
|
|
17
|
+
r.is_a?(Hash) ? { content: r[:content], disabled: r.fetch(:disabled, false) } : r
|
|
18
|
+
})
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.delete(name:, type:)
|
|
22
|
+
new(name: name, type: type, ttl: 0, changetype: 'DELETE', records: [])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.from_api(data)
|
|
26
|
+
new(
|
|
27
|
+
name: data['name'] || data[:name],
|
|
28
|
+
type: data['type'] || data[:type],
|
|
29
|
+
ttl: data['ttl'] || data[:ttl] || 3600,
|
|
30
|
+
changetype: 'REPLACE',
|
|
31
|
+
records: (data['records'] || data[:records] || []).map { |r|
|
|
32
|
+
{ content: r['content'] || r[:content], disabled: r['disabled'] || r[:disabled] || false }
|
|
33
|
+
}
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def to_h
|
|
38
|
+
{
|
|
39
|
+
name: name,
|
|
40
|
+
type: type,
|
|
41
|
+
ttl: ttl,
|
|
42
|
+
changetype: changetype,
|
|
43
|
+
records: records
|
|
44
|
+
}
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PdnsRuby
|
|
4
|
+
class Zone
|
|
5
|
+
attr_reader :client, :name, :kind, :serial, :dnssec, :account
|
|
6
|
+
|
|
7
|
+
def initialize(client, data)
|
|
8
|
+
@client = client
|
|
9
|
+
@name = data['name'] || data[:name]
|
|
10
|
+
@kind = data['kind'] || data[:kind]
|
|
11
|
+
@serial = data['serial'] || data[:serial]
|
|
12
|
+
@dnssec = data['dnssec'] || data[:dnssec]
|
|
13
|
+
@account = data['account'] || data[:account]
|
|
14
|
+
@data = data
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def rrsets
|
|
18
|
+
zone_data = client.zone(@name.chomp('.'))
|
|
19
|
+
(@data['rrsets'] || @data[:rrsets] || []).map { |r| Rrset.from_api(r) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def find_rrset(name, type)
|
|
23
|
+
rrsets.find { |r| r.name == name && r.type == type }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def update(rrsets)
|
|
27
|
+
client.update_rrsets(@name.chomp('.'), rrsets)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def add_record(name, type, content, ttl: 3600)
|
|
31
|
+
rrset = Rrset.replace(name: name, type: type, ttl: ttl, records: [{ content: content }])
|
|
32
|
+
update([rrset])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def delete_record(name, type)
|
|
36
|
+
rrset = Rrset.delete(name: name, type: type)
|
|
37
|
+
update([rrset])
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def delete!
|
|
41
|
+
client.delete_zone(@name.chomp('.'))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def to_h
|
|
45
|
+
@data
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
data/lib/pdns_ruby.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pdns-ruby
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Vibol Teav
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: A simple Ruby client for managing zones and records via the PowerDNS
|
|
13
|
+
HTTP API. Supports zone CRUD, rrset management, and record operations.
|
|
14
|
+
email:
|
|
15
|
+
- vibolteav@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- CHANGELOG.md
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/pdns_ruby.rb
|
|
24
|
+
- lib/pdns_ruby/client.rb
|
|
25
|
+
- lib/pdns_ruby/rrset.rb
|
|
26
|
+
- lib/pdns_ruby/version.rb
|
|
27
|
+
- lib/pdns_ruby/zone.rb
|
|
28
|
+
homepage: https://github.com/tvcam/pdns-ruby
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata: {}
|
|
32
|
+
rdoc_options: []
|
|
33
|
+
require_paths:
|
|
34
|
+
- lib
|
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '2.5'
|
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '0'
|
|
45
|
+
requirements: []
|
|
46
|
+
rubygems_version: 4.0.6
|
|
47
|
+
specification_version: 4
|
|
48
|
+
summary: Ruby client for the PowerDNS API
|
|
49
|
+
test_files: []
|