From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH network 3/3] ipam dns: powerdns integration: factor out common API request code
Date: Thu, 6 Mar 2025 10:18:18 +0100 [thread overview]
Message-ID: <20250306091818.3841361-4-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20250306091818.3841361-1-t.lamprecht@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
prev parent reply other threads:[~2025-03-06 9:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-06 9:18 [pve-devel] applied-series: [PATCH network 0/3] smaller fixes and clean-ups Thomas Lamprecht
2025-03-06 9:18 ` [pve-devel] [PATCH network 1/3] fix missing use statements in core SDN module Thomas Lamprecht
2025-03-06 9:18 ` [pve-devel] [PATCH network 2/3] api request helper: fix conditional declaration Thomas Lamprecht
2025-03-06 9:18 ` Thomas Lamprecht [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250306091818.3841361-4-t.lamprecht@proxmox.com \
--to=t.lamprecht@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal