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

#change, #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.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pdns_api/zone.rb', line 95

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.



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

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

#checkObject

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



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

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.



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

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.



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

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.



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pdns_api/zone.rb', line 145

def (kind = nil, value = nil)
  return .new(@http, self, kind, value) unless kind.nil? || value.nil?
  return .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.



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

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

#remove(*rrsets) ⇒ Object

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



129
130
131
132
133
134
135
136
# File 'lib/pdns_api/zone.rb', line 129

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.



117
118
119
120
121
122
123
124
125
# File 'lib/pdns_api/zone.rb', line 117

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