From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pve-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id CA75D1FF16B for <inbox@lore.proxmox.com>; Thu, 6 Mar 2025 10:19:10 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E79A13693F; Thu, 6 Mar 2025 10:19:05 +0100 (CET) From: Thomas Lamprecht <t.lamprecht@proxmox.com> To: pve-devel@lists.proxmox.com Date: Thu, 6 Mar 2025 10:18:18 +0100 Message-Id: <20250306091818.3841361-4-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250306091818.3841361-1-t.lamprecht@proxmox.com> References: <20250306091818.3841361-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.040 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH network 3/3] ipam dns: powerdns integration: factor out common API request code X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com> 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> --- 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