Class: PDNS::Zone

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

Overview

Zone on a server.

Instance Attribute Summary collapse

Attributes inherited from API

#class, #url

Instance Method Summary collapse

Methods inherited from API

#create, #delete, #ensure_array, #get, #info

Constructor Details

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

Creates a Zone object.

  • http: An HTTP object for interaction with the PowerDNS server.

  • parent: This object’s parent.

  • id: ID of the zone.

  • info: Optional information of the zone.



38
39
40
41
42
43
44
45
# File 'lib/pdns_api/zone.rb', line 38

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)

The ID of the zone.



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

def id
  @id
end

Instance Method Details

#add(*rrsets) ⇒ Object

Adds records to the ones already existing in the zone.

The existing records are retrieved and merged with the ones given in rrsets.



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

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

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

  # Add these records to the rrset
  rrsets.map! do |rrset|
    # Get current data from rrset
    current = current_records(rrset, data)

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

#axfr_retrieveObject

Retrieves the data for a zone. Only works for domains for which the server is a slave. Returns the result of the retrieval.



78
79
80
# File 'lib/pdns_api/zone.rb', line 78

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

#change(rrsets) ⇒ Object

Modifies basic zone data (metadata). rrset is used as changeset for the update.



62
63
64
# File 'lib/pdns_api/zone.rb', line 62

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

#checkObject

Checks the zone for errors. Returns the result of the check.



94
95
96
# File 'lib/pdns_api/zone.rb', line 94

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

#cryptokeys(id = nil) ⇒ Object

Returns existing or creates a CryptoKey object.

If id is not set the current servers are returned in a hash containing CryptoKey objects.

If id is set a CryptoKey object with the provided ID is returned.



173
174
175
176
177
178
179
180
181
# File 'lib/pdns_api/zone.rb', line 173

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

Exports the zone as a bind zone file. Returns a hash containing the zone in :result.



85
86
87
88
89
# File 'lib/pdns_api/zone.rb', line 85

def export
  data = @http.get "#{@url}/export"
  data.delete(:error) if data[:error] == 'Non-JSON response'
  data
end

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

Returns existing metadata or creates a Metadata object.

If kind is not set the current metadata is returned in a hash.

If kind is set a Metadata object is returned using the provided kind. If value is set as well, a complete Metadata object is returned.



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

def (kind = nil, value = nil)
  return Metadata.new(@http, self, kind, value) 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 information (records) of a zone. Also formats records to match the API requirements.



50
51
52
53
54
55
56
57
# File 'lib/pdns_api/zone.rb', line 50

def modify(rrsets)
  rrsets.map! do |rrset|
    rrset = format_records(rrset) if rrset.key?(:records)
    rrset
  end

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

#notifyObject

Notifies slaves for a zone. Only works for domains for which the server is a master. Returns the result of the notification.



70
71
72
# File 'lib/pdns_api/zone.rb', line 70

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

#remove(*rrsets) ⇒ Object

Removes all records for a name/type combination from the zone.



136
137
138
139
140
141
142
143
# File 'lib/pdns_api/zone.rb', line 136

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

#update(*rrsets) ⇒ Object

Updates (replaces) records for a name/type combination in the zone.



124
125
126
127
128
129
130
131
132
# File 'lib/pdns_api/zone.rb', line 124

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