mirror of
git://git.proxmox.com/git/pve-network.git
synced 2025-04-30 19:27:11 +02:00
ipam dns: powerdns integration: factor out common API request code
No point in having the same generic code to prepare variables for an API request to PowerDNS 7 times basically duplicated. Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
f247794dda
commit
22c85db36f
|
@ -44,17 +44,24 @@ sub options {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my sub powerdns_api_request {
|
||||||
|
my ($config, $method, $path, $params) = @_;
|
||||||
|
|
||||||
|
return PVE::Network::SDN::api_request(
|
||||||
|
$method,
|
||||||
|
"$config->{url}${path}",
|
||||||
|
['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $config->{key}],
|
||||||
|
$params,
|
||||||
|
$config->{fingerprint},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
# Plugin implementation
|
# Plugin implementation
|
||||||
|
|
||||||
sub add_a_record {
|
sub add_a_record {
|
||||||
my ($class, $plugin_config, $zone, $hostname, $ip, $noerr) = @_;
|
my ($class, $plugin_config, $zone, $hostname, $ip, $noerr) = @_;
|
||||||
|
|
||||||
my $url = $plugin_config->{url};
|
|
||||||
my $key = $plugin_config->{key};
|
|
||||||
my $ttl = $plugin_config->{ttl} ? $plugin_config->{ttl} : 14400;
|
my $ttl = $plugin_config->{ttl} ? $plugin_config->{ttl} : 14400;
|
||||||
my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
|
|
||||||
my $fingerprint = $plugin_config->{fingerprint};
|
|
||||||
|
|
||||||
my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A";
|
my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A";
|
||||||
my $fqdn = $hostname.".".$zone.".";
|
my $fqdn = $hostname.".".$zone.".";
|
||||||
|
|
||||||
|
@ -87,21 +94,15 @@ sub add_a_record {
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
|
|
||||||
eval {
|
eval { powerdns_api_request($plugin_config, 'PATCH', "/zones/$zone", $params) };
|
||||||
PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params, $fingerprint)
|
|
||||||
};
|
|
||||||
die "error add $fqdn to zone $zone: $@" if $@ && !$noerr;
|
die "error add $fqdn to zone $zone: $@" if $@ && !$noerr;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub add_ptr_record {
|
sub add_ptr_record {
|
||||||
my ($class, $plugin_config, $zone, $hostname, $ip, $noerr) = @_;
|
my ($class, $plugin_config, $zone, $hostname, $ip, $noerr) = @_;
|
||||||
|
|
||||||
my $url = $plugin_config->{url};
|
|
||||||
my $key = $plugin_config->{key};
|
|
||||||
my $ttl = $plugin_config->{ttl} ? $plugin_config->{ttl} : 14400;
|
my $ttl = $plugin_config->{ttl} ? $plugin_config->{ttl} : 14400;
|
||||||
my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
|
|
||||||
$hostname .= ".";
|
$hostname .= ".";
|
||||||
my $fingerprint = $plugin_config->{fingerprint};
|
|
||||||
|
|
||||||
my $reverseip = Net::IP->new($ip)->reverse_ip();
|
my $reverseip = Net::IP->new($ip)->reverse_ip();
|
||||||
|
|
||||||
|
@ -124,21 +125,15 @@ sub add_ptr_record {
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
|
|
||||||
eval {
|
eval { powerdns_api_request($plugin_config, 'PATCH', "/zones/$zone", $params) };
|
||||||
PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params, $fingerprint)
|
|
||||||
};
|
|
||||||
die "error add $reverseip to zone $zone: $@" if $@ && !$noerr;
|
die "error add $reverseip to zone $zone: $@" if $@ && !$noerr;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub del_a_record {
|
sub del_a_record {
|
||||||
my ($class, $plugin_config, $zone, $hostname, $ip, $noerr) = @_;
|
my ($class, $plugin_config, $zone, $hostname, $ip, $noerr) = @_;
|
||||||
|
|
||||||
my $url = $plugin_config->{url};
|
|
||||||
my $key = $plugin_config->{key};
|
|
||||||
my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
|
|
||||||
my $fqdn = $hostname.".".$zone.".";
|
my $fqdn = $hostname.".".$zone.".";
|
||||||
my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A";
|
my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A";
|
||||||
my $fingerprint = $plugin_config->{fingerprint};
|
|
||||||
|
|
||||||
my $zonecontent = get_zone_content($plugin_config, $zone);
|
my $zonecontent = get_zone_content($plugin_config, $zone);
|
||||||
my $existing_rrset = get_zone_rrset($zonecontent, $fqdn);
|
my $existing_rrset = get_zone_rrset($zonecontent, $fqdn);
|
||||||
|
@ -165,20 +160,13 @@ sub del_a_record {
|
||||||
|
|
||||||
my $params = { rrsets => [ $rrset ] };
|
my $params = { rrsets => [ $rrset ] };
|
||||||
|
|
||||||
eval {
|
eval { powerdns_api_request($plugin_config, 'PATCH', "/zones/$zone", $params) };
|
||||||
PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params, $fingerprint)
|
|
||||||
};
|
|
||||||
die "error delete $fqdn from zone $zone: $@" if $@ && !$noerr;
|
die "error delete $fqdn from zone $zone: $@" if $@ && !$noerr;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub del_ptr_record {
|
sub del_ptr_record {
|
||||||
my ($class, $plugin_config, $zone, $ip, $noerr) = @_;
|
my ($class, $plugin_config, $zone, $ip, $noerr) = @_;
|
||||||
|
|
||||||
my $url = $plugin_config->{url};
|
|
||||||
my $key = $plugin_config->{key};
|
|
||||||
my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
|
|
||||||
my $fingerprint = $plugin_config->{fingerprint};
|
|
||||||
|
|
||||||
my $reverseip = Net::IP->new($ip)->reverse_ip();
|
my $reverseip = Net::IP->new($ip)->reverse_ip();
|
||||||
|
|
||||||
my $type = "PTR";
|
my $type = "PTR";
|
||||||
|
@ -192,26 +180,15 @@ sub del_ptr_record {
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
|
|
||||||
eval {
|
eval { powerdns_api_request($plugin_config, 'PATCH', "/zones/$zone", $params) };
|
||||||
PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params, $fingerprint)
|
|
||||||
};
|
|
||||||
die "error delete $reverseip from zone $zone: $@" if $@ && !$noerr;
|
die "error delete $reverseip from zone $zone: $@" if $@ && !$noerr;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub verify_zone {
|
sub verify_zone {
|
||||||
my ($class, $plugin_config, $zone, $noerr) = @_;
|
my ($class, $plugin_config, $zone, $noerr) = @_;
|
||||||
|
|
||||||
#verify that api is working
|
# verify that zone exists
|
||||||
|
eval { powerdns_api_request($plugin_config, 'GET', "/zones/$zone?rrsets=false") };
|
||||||
my $url = $plugin_config->{url};
|
|
||||||
my $key = $plugin_config->{key};
|
|
||||||
my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
|
|
||||||
my $fingerprint = $plugin_config->{fingerprint};
|
|
||||||
|
|
||||||
eval {
|
|
||||||
PVE::Network::SDN::api_request(
|
|
||||||
"GET", "$url/zones/$zone?rrsets=false", $headers, undef, $fingerprint)
|
|
||||||
};
|
|
||||||
die "can't read zone $zone: $@" if $@ && !$noerr;
|
die "can't read zone $zone: $@" if $@ && !$noerr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,13 +246,7 @@ sub on_update_hook {
|
||||||
my ($class, $plugin_config) = @_;
|
my ($class, $plugin_config) = @_;
|
||||||
|
|
||||||
# verify that api is working
|
# verify that api is working
|
||||||
|
eval { powerdns_api_request($plugin_config, 'GET', '') };
|
||||||
my $url = $plugin_config->{url};
|
|
||||||
my $key = $plugin_config->{key};
|
|
||||||
my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
|
|
||||||
my $fingerprint = $plugin_config->{fingerprint};
|
|
||||||
|
|
||||||
eval { PVE::Network::SDN::api_request("GET", "$url", $headers, undef, $fingerprint) };
|
|
||||||
die "dns api error: $@" if $@;
|
die "dns api error: $@" if $@;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,15 +255,7 @@ sub get_zone_content {
|
||||||
my ($plugin_config, $zone) = @_;
|
my ($plugin_config, $zone) = @_;
|
||||||
|
|
||||||
# verify that api is working
|
# verify that api is working
|
||||||
|
my $result = eval { powerdns_api_request($plugin_config, 'GET', "/zones/$zone") };
|
||||||
my $url = $plugin_config->{url};
|
|
||||||
my $key = $plugin_config->{key};
|
|
||||||
my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
|
|
||||||
my $fingerprint = $plugin_config->{fingerprint};
|
|
||||||
|
|
||||||
my $result = eval {
|
|
||||||
PVE::Network::SDN::api_request("GET", "$url/zones/$zone", $headers, undef, $fingerprint)
|
|
||||||
};
|
|
||||||
die "can't read zone $zone: $@" if $@;
|
die "can't read zone $zone: $@" if $@;
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
Loading…
Reference in a new issue