Class: PDNS::Zone

Inherits:
API
  • Object
show all
Defined in:
lib/pdns_api/zone.rb

Overview

Zone

Instance Attribute Summary collapse

Attributes inherited from API

#class, #url, #version

Instance Method Summary collapse

Methods inherited from API

#create, #delete, #ensure_array, #get, hash_string_to_sym, hash_sym_to_string, #info

Constructor Details

#initialize(http, parent, id, info = {}) ⇒ Zone

Returns a new instance of Zone.



10
11
12
13
14
15
16
17
# File 'lib/pdns_api/zone.rb', line 10

def initialize(http, parent, id, info = {})
  @class  = :zones
  @http   = http
  @parent = parent
  @id     = id
  @info   = info
  @url    = "#{parent.url}/#{@class}/#{id}"
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/pdns_api/zone.rb', line 8

def id
  @id
end

Instance Method Details

#add(*rrsets) ⇒ Object

Add records to the ones already existing Only works from API v1 and up



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/pdns_api/zone.rb', line 122

def add(*rrsets)
  # Get current zone data
  data = get

  # Return any errors
  return data if data.key?(:error)

  # Run v0 version
  return add_v0(rrsets, data) if @http.version == 0

  # Add these records to the rrset
  rrsets.map! do |rrset|
    # Get current data from rrset
    current = data[:rrsets].select { |r| r[:name] == rrset[:name] && r[:type] == rrset[:type] }

    # Merge data
    rrset[:records]    = current.first[:records] + ensure_array(rrset[:records])
    rrset[:changetype] = 'REPLACE'
    rrset
  end
  modify(rrsets)
end

#add_v0(rrsets, data) ⇒ Object

Add records to the ones already existing Only works from API v1 and down



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/pdns_api/zone.rb', line 147

def add_v0(rrsets, data)
  # Add these records to the rrset
  rrsets.map! do |rrset|
    current = data[:records].select do |r|
      r[:name] == rrset[:name] && r[:type] == rrset[:type]
    end
    current.map! do |record|
      {
        content:  record[:content],
        disabled: record[:disabled]
      }
    end
    rrset[:records] = current + ensure_array(rrset[:records])
    rrset[:changetype] = 'REPLACE'
    rrset
  end
  modify(rrsets)
end

#axfr_retrieveObject

Get the AXFR for a zone



41
42
43
# File 'lib/pdns_api/zone.rb', line 41

def axfr_retrieve
  @http.put "#{@url}/axfr-retrieve"
end

#change(rrsets) ⇒ Object

Modifies basic zone data (metadata).



29
30
31
# File 'lib/pdns_api/zone.rb', line 29

def change(rrsets)
  @http.put(@url, rrsets)
end

#checkObject

Check a zone



51
52
53
# File 'lib/pdns_api/zone.rb', line 51

def check
  @http.get "#{@url}/check"
end

#cryptokeys(id = nil) ⇒ Object

Change cryptokeys for a zone



73
74
75
76
77
78
79
80
81
# File 'lib/pdns_api/zone.rb', line 73

def cryptokeys(id = nil)
  return CryptoKey.new(@http, self, id) unless id.nil?

  # Get all current metadata
  cryptokeys = @http.get("#{@url}/cryptokeys")

  # Convert cryptokeys to hash
  cryptokeys.map! { |c| [c[:id], CryptoKey.new(@http, self, c[:id], c)] }.to_h
end

#exportObject

Export a zone



46
47
48
# File 'lib/pdns_api/zone.rb', line 46

def export
  @http.get "#{@url}/export"
end

#format_records(rrset) ⇒ Object

Add required items to records in an rrset



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pdns_api/zone.rb', line 99

def format_records(rrset)
  # Abort if rrset is something else than an array
  abort('Error: records needs to be array') unless rrset[:records].is_a? Array

  # Ensure existence of required keys
  rrset[:records].map! do |record|
    # Format the record content
    record = format_single_record(record)

    # Add disabled from the rrset
    record[:disabled] ||= !!rrset[:disabled]

    # Return record
    next record unless @http.version == 0

    # But add some more for APIv0
    record.merge(name: rrset[:name], type: rrset[:type], ttl: rrset[:ttl])
  end
  rrset
end

#format_single_record(record) ⇒ Object

Additional methods



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pdns_api/zone.rb', line 85

def format_single_record(record)
  # Ensure content
  record = { content: record } if record.is_a? String

  # Add disabled and set_ptr
  record[:disabled] = !!record[:disabled]
  record[:set_ptr]  = !!record[:set_ptr]

  # Replace some symbols
  record[:'set-ptr'] = record.delete :set_ptr
  record
end

#metadata(kind = nil, value = nil) ⇒ Object

Manipulate metadata for a zone



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

def (kind = nil, value = nil)
  return Metadata.new(@http, self, kind, value).create unless kind.nil? || value.nil?
  return Metadata.new(@http, self, kind) unless kind.nil?

  # Get all current metadata
   = @http.get("#{@url}/metadata")

  # Check for errors
  return  if .is_a?(Hash) && .key?(:error)

  # Convert metadata to hash
  .map! { |c| [c[:kind], c[:metadata]] }.to_h
end

#modify(rrsets) ⇒ Object

Modifies present RRsets and comments.



22
23
24
25
26
# File 'lib/pdns_api/zone.rb', line 22

def modify(rrsets)
  rrsets.map! { |rrset| format_records(rrset) if rrset.key?(:records) }

  @http.patch(@url, rrsets: rrsets)
end

#notifyObject

Notify slaves for a zone



36
37
38
# File 'lib/pdns_api/zone.rb', line 36

def notify
  @http.put "#{@url}/notify"
end

#remove(*rrsets) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/pdns_api/zone.rb', line 176

def remove(*rrsets)
  # Set type and format records
  rrsets.map! do |rrset|
    rrset[:changetype] = 'DELETE'
    rrset
  end
  modify(rrsets)
end

#update(*rrsets) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/pdns_api/zone.rb', line 166

def update(*rrsets)
  # Set type and format records
  rrsets.map! do |rrset|
    rrset[:changetype] = 'REPLACE'
    rrset[:records] = ensure_array(rrset[:records])
    rrset
  end
  modify(rrsets)
end