]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Dns/PowerdnsPlugin.pm
add DNS plugin
[pve-network.git] / PVE / Network / SDN / Dns / PowerdnsPlugin.pm
1 package PVE::Network::SDN::Dns::PowerdnsPlugin;
2
3 use strict;
4 use warnings;
5 use PVE::INotify;
6 use PVE::Cluster;
7 use PVE::Tools;
8 use JSON;
9 use Net::IP;
10
11 use base('PVE::Network::SDN::Dns::Plugin');
12
13 sub type {
14 return 'powerdns';
15 }
16
17 sub properties {
18 return {
19 url => {
20 type => 'string',
21 },
22 key => {
23 type => 'string',
24 },
25 };
26 }
27
28 sub options {
29
30 return {
31 url => { optional => 0},
32 key => { optional => 0 },
33 ttl => { optional => 1 },
34 };
35 }
36
37 # Plugin implementation
38
39 sub add_a_record {
40 my ($class, $plugin_config, $zone, $hostname, $ip) = @_;
41
42 my $url = $plugin_config->{url};
43 my $key = $plugin_config->{key};
44 my $ttl = $plugin_config->{ttl} ? $plugin_config->{ttl} : 14400;
45 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
46
47 my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A";
48 my $fqdn = $hostname.".".$zone.".";
49
50
51 my $record = { content => $ip,
52 disabled => JSON::false,
53 name => $fqdn,
54 type => $type,
55 priority => 0 };
56
57 my $rrset = { name => $fqdn,
58 type => $type,
59 ttl => $ttl,
60 changetype => "REPLACE",
61 records => [ $record ] };
62
63
64 my $params = { rrsets => [ $rrset ] };
65
66 eval {
67 PVE::Network::SDN::Dns::Plugin::api_request("PATCH", "$url/zones/$zone", $headers, $params);
68 };
69
70 if ($@) {
71 die "error add $fqdn to zone $zone: $@";
72 }
73 }
74
75 sub add_ptr_record {
76 my ($class, $plugin_config, $zone, $hostname, $ip) = @_;
77
78 my $url = $plugin_config->{url};
79 my $key = $plugin_config->{key};
80 my $ttl = $plugin_config->{ttl} ? $plugin_config->{ttl} : 14400;
81 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
82 $hostname .= ".";
83
84 my $reverseip = join(".", reverse(split(/\./, $ip))).".in-addr.arpa.";
85 my $type = "PTR";
86
87 my $record = { content => $hostname,
88 disabled => JSON::false,
89 name => $reverseip,
90 type => $type,
91 priority => 0 };
92
93 my $rrset = { name => $reverseip,
94 type => $type,
95 ttl => $ttl,
96 changetype => "REPLACE",
97 records => [ $record ] };
98
99
100 my $params = { rrsets => [ $rrset ] };
101
102 eval {
103 PVE::Network::SDN::Dns::Plugin::api_request("PATCH", "$url/zones/$zone", $headers, $params);
104 };
105
106 if ($@) {
107 die "error add $reverseip to zone $zone: $@";
108 }
109 }
110
111 sub del_a_record {
112 my ($class, $plugin_config, $zone, $hostname, $ip) = @_;
113
114 my $url = $plugin_config->{url};
115 my $key = $plugin_config->{key};
116 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
117 my $fqdn = $hostname.".".$zone.".";
118 my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A";
119
120 my $rrset = { name => $fqdn,
121 type => $type,
122 changetype => "DELETE",
123 records => [] };
124
125 my $params = { rrsets => [ $rrset ] };
126
127 eval {
128 PVE::Network::SDN::Dns::Plugin::api_request("PATCH", "$url/zones/$zone", $headers, $params);
129 };
130
131 if ($@) {
132 die "error delete $fqdn from zone $zone: $@";
133 }
134 }
135
136 sub del_ptr_record {
137 my ($class, $plugin_config, $zone, $ip) = @_;
138
139 my $url = $plugin_config->{url};
140 my $key = $plugin_config->{key};
141 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
142
143 my $reverseip = join(".", reverse(split(/\./, $ip))).".in-addr.arpa.";
144 my $type = "PTR";
145
146 my $rrset = { name => $reverseip,
147 type => $type,
148 changetype => "DELETE",
149 records => [] };
150
151 my $params = { rrsets => [ $rrset ] };
152
153 eval {
154 PVE::Network::SDN::Dns::Plugin::api_request("PATCH", "$url/zones/$zone", $headers, $params);
155 };
156
157 if ($@) {
158 die "error delete $reverseip from zone $zone: $@";
159 }
160 }
161
162 sub verify_zone {
163 my ($class, $plugin_config, $zone) = @_;
164
165 #verify that api is working
166
167 my $url = $plugin_config->{url};
168 my $key = $plugin_config->{key};
169 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
170
171 eval {
172 PVE::Network::SDN::Dns::Plugin::api_request("GET", "$url/zones/$zone", $headers);
173 };
174
175 if ($@) {
176 die "can't read zone $zone: $@";
177 }
178 }
179
180
181 sub on_update_hook {
182 my ($class, $plugin_config) = @_;
183
184 #verify that api is working
185
186 my $url = $plugin_config->{url};
187 my $key = $plugin_config->{key};
188 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
189
190 eval {
191 PVE::Network::SDN::Dns::Plugin::api_request("GET", "$url", $headers);
192 };
193
194 if ($@) {
195 die "dns api error: $@";
196 }
197 }
198
199 1;
200
201