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.lampre...@proxmox.com> --- There might be other such bloat lurking, just stumbled into this by chance.. src/PVE/Network/SDN/Dns/PowerdnsPlugin.pm | 77 ++++++----------------- 1 file changed, 20 insertions(+), 57 deletions(-) diff --git a/src/PVE/Network/SDN/Dns/PowerdnsPlugin.pm b/src/PVE/Network/SDN/Dns/PowerdnsPlugin.pm index 74735e7..15afb4a 100644 --- a/src/PVE/Network/SDN/Dns/PowerdnsPlugin.pm +++ b/src/PVE/Network/SDN/Dns/PowerdnsPlugin.pm @@ -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 sub add_a_record { 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 $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 $fqdn = $hostname.".".$zone."."; @@ -87,21 +94,15 @@ sub add_a_record { }], }; - eval { - PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params, $fingerprint) - }; + eval { powerdns_api_request($plugin_config, 'PATCH', "/zones/$zone", $params) }; die "error add $fqdn to zone $zone: $@" if $@ && !$noerr; } sub add_ptr_record { 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 $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key]; $hostname .= "."; - my $fingerprint = $plugin_config->{fingerprint}; my $reverseip = Net::IP->new($ip)->reverse_ip(); @@ -124,21 +125,15 @@ sub add_ptr_record { }], }; - eval { - PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params, $fingerprint) - }; + eval { powerdns_api_request($plugin_config, 'PATCH', "/zones/$zone", $params) }; die "error add $reverseip to zone $zone: $@" if $@ && !$noerr; } sub del_a_record { 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 $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A"; - my $fingerprint = $plugin_config->{fingerprint}; my $zonecontent = get_zone_content($plugin_config, $zone); my $existing_rrset = get_zone_rrset($zonecontent, $fqdn); @@ -165,20 +160,13 @@ sub del_a_record { my $params = { rrsets => [ $rrset ] }; - eval { - PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params, $fingerprint) - }; + eval { powerdns_api_request($plugin_config, 'PATCH', "/zones/$zone", $params) }; die "error delete $fqdn from zone $zone: $@" if $@ && !$noerr; } sub del_ptr_record { 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 $type = "PTR"; @@ -192,26 +180,15 @@ sub del_ptr_record { }], }; - eval { - PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params, $fingerprint) - }; + eval { powerdns_api_request($plugin_config, 'PATCH', "/zones/$zone", $params) }; die "error delete $reverseip from zone $zone: $@" if $@ && !$noerr; } sub verify_zone { my ($class, $plugin_config, $zone, $noerr) = @_; - #verify that api is working - - 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) - }; + # verify that zone exists + eval { powerdns_api_request($plugin_config, 'GET', "/zones/$zone?rrsets=false") }; die "can't read zone $zone: $@" if $@ && !$noerr; } @@ -269,13 +246,7 @@ sub on_update_hook { my ($class, $plugin_config) = @_; # verify that api is working - - 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) }; + eval { powerdns_api_request($plugin_config, 'GET', '') }; die "dns api error: $@" if $@; } @@ -284,15 +255,7 @@ sub get_zone_content { my ($plugin_config, $zone) = @_; # verify that api is working - - 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) - }; + my $result = eval { powerdns_api_request($plugin_config, 'GET', "/zones/$zone") }; die "can't read zone $zone: $@" if $@; return $result; -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel