Class: PdnsRestApiClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/vagrant-powerdns/util/pdns_api.rb

Overview

A class to interact with the PowerDNS REST API doc.powerdns.com/md/httpapi/api_spec/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, api_key) ⇒ PdnsRestApiClient

Returns a new instance of PdnsRestApiClient.



24
25
26
27
28
29
30
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 24

def initialize(base_uri, api_key)
  self.class.base_uri base_uri

  self.class.headers 'X-API-Key' => api_key

  @api_key = api_key
end

Instance Attribute Details

#base_uriObject

Returns the value of attribute base_uri.



22
23
24
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 22

def base_uri
  @base_uri
end

Instance Method Details

#axfr_retrieve(zone_id, server_id = 'localhost') ⇒ Object



74
75
76
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 74

def axfr_retrieve(zone_id, server_id='localhost')
  self.class.put("/servers/#{server_id}/zones/#{zone_id}/axfr-retrieve")
end

#config(server_id) ⇒ Object



40
41
42
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 40

def config(server_id)
  self.class.get("/servers/#{server_id}/config")
end

#create_zone(domain, nsServers = [], kind = 'Native', masters = [], server_id = 'localhost') ⇒ Object



60
61
62
63
64
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 60

def create_zone(domain, nsServers=[], kind='Native', masters=[], server_id='localhost')
  zone = { name: domain, kind: kind, masters: masters, nameservers: nsServers }

  modify_zone(zone, server_id)
end

#delete_zone(zone_id, server_id = 'localhost') ⇒ Object



66
67
68
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 66

def delete_zone(zone_id, server_id='localhost')
  self.class.delete("/servers/#{server_id}/zones/#{zone_id}")
end

#disable_domain(domain:, zone_id:, ip:, server_id: 'localhost', comments: nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 146

def disable_domain(domain:, zone_id:, ip:, server_id:'localhost', comments: nil)
  @body = {
    rrsets: [
      {
        name: domain,
        type: "A",
        records: [
          {
            name: domain,
            type: "A",
            ttl: 300,
            content: ip,
            disabled: true
          }
        ],
        changetype: "replace"
      }
    ]
  }
  if !comments.nil?
     @body[:rrsets][0][:comments] = comments
  end
  self.class.patch("/servers/#{server_id}/zones/#{zone_id}", body: @body.to_json)
end

#get_comments(domain, zone_id, server_id = 'localhost') ⇒ Object



90
91
92
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 90

def get_comments(domain, zone_id, server_id='localhost')
  puts
end

#modify_domain(domain:, ip:, zone_id:, server_id: 'localhost', comments: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 94

def modify_domain(domain:, ip:, zone_id:, server_id:'localhost', comments: nil)
  #  {
  #      :rrsets => [
  #          [0] {
  #                 :name => "gundul.dev.jcamp.net",
  #                 :type => "A",
  #                 :records => [
  #                  [0] {
  #                          :name => "gundul.dev.jcamp.net",
  #                          :type => "A",
  #                           :ttl => 3600,
  #                      :disabled => false,
  #                       :content => "192.168.10.11"
  #                      }
  #                 ],
  #                 :comments => [
  #                  [0] {
  #                             :name => "gundul.dev.jcamp.net",
  #                             :type => "A",
  #                      :modified_at => 1461239952,
  #                          :account => "ayik",
  #                          :content => "NGopoE cuk..."
  #                      }
  #                 ],
  #                 :changetype => "replace"
  #              }
  #       ]
  #  }
  @body = {
    rrsets: [
      {
        name: domain,
        type: "A",
        records: [
          {
            name: domain,
            type: "A",
            ttl: 300,
            disabled: false,
            content: ip
          }
        ],
        changetype: "replace"
      }
    ]
  }
  if !comments.nil?
     @body[:rrsets][0][:comments] = comments
  end
  self.class.patch("/servers/#{server_id}/zones/#{zone_id}", body: @body.to_json)
end

#modify_zone(zone, server_id = 'localhost') ⇒ Object



56
57
58
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 56

def modify_zone(zone, server_id='localhost')
  self.class.post("/servers/#{server_id}/zones", body: zone.to_json)
end

#notify(zone_id, server_id = 'localhost') ⇒ Object



70
71
72
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 70

def notify(zone_id, server_id='localhost')
  self.class.put("/servers/#{server_id}/zones/#{zone_id}/notify")
end

#search_log(search_term, server_id = 'localhost') ⇒ Object



82
83
84
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 82

def search_log(search_term, server_id='localhost')
  self.class.get("/servers/#{server_id}/search-log", query: { q: search_term })
end

#server(server_id) ⇒ Object



36
37
38
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 36

def server(server_id)
  self.class.get("/servers/#{server_id}")
end

#serversObject



32
33
34
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 32

def servers
  self.class.get('/servers')
end

#setting(server_id, config_setting_name) ⇒ Object



44
45
46
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 44

def setting(server_id, config_setting_name)
  self.class.get("/servers/#{server_id}/config/#{config_setting_name}")
end

#stats(server_id = 'localhost') ⇒ Object



86
87
88
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 86

def stats(server_id='localhost')
  self.class.get("/servers/#{server_id}/statistics")
end

#zone(zone_id, server_id = 'localhost') ⇒ Object



52
53
54
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 52

def zone(zone_id, server_id='localhost')
  self.class.get("/servers/#{server_id}/zones/#{zone_id}")
end

#zone_export(zone_id, server_id = 'localhost') ⇒ Object



78
79
80
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 78

def zone_export(zone_id, server_id='localhost')
  self.class.get("/servers/#{server_id}/zones/#{zone_id}/export")
end

#zones(server_id = 'localhost') ⇒ Object



48
49
50
# File 'lib/vagrant-powerdns/util/pdns_api.rb', line 48

def zones(server_id='localhost')
  self.class.get("/servers/#{server_id}/zones")
end