[pve-devel] [PATCH pve-network 04/15] dns/ipam : move api_request helper to sdn module

Alexandre Derumier aderumier at odiso.com
Tue Jan 5 10:35:25 CET 2021


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/Network/SDN.pm                     | 47 +++++++++++++++++++++++++-
 PVE/Network/SDN/Dns/Plugin.pm          | 37 --------------------
 PVE/Network/SDN/Dns/PowerdnsPlugin.pm  | 12 +++----
 PVE/Network/SDN/Ipams/NetboxPlugin.pm  | 18 +++++-----
 PVE/Network/SDN/Ipams/PhpIpamPlugin.pm | 18 +++++-----
 PVE/Network/SDN/Ipams/Plugin.pm        | 37 --------------------
 6 files changed, 70 insertions(+), 99 deletions(-)

diff --git a/PVE/Network/SDN.pm b/PVE/Network/SDN.pm
index a20233a..befaaee 100644
--- a/PVE/Network/SDN.pm
+++ b/PVE/Network/SDN.pm
@@ -232,5 +232,50 @@ sub encode_value {
     return $value;
 }
 
-1;
 
+#helpers
+sub api_request {
+    my ($method, $url, $headers, $data) = @_;
+
+    my $encoded_data = to_json($data) if $data;
+
+    my $req = HTTP::Request->new($method,$url, $headers, $encoded_data);
+
+    my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 30);
+    my $proxy = undef;
+
+    if ($proxy) {
+        $ua->proxy(['http', 'https'], $proxy);
+    } else {
+        $ua->env_proxy;
+    }
+
+    $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
+
+    my $response = $ua->request($req);
+    my $code = $response->code;
+
+    if ($code !~ /^2(\d+)$/) {
+        my $msg = $response->message || 'unknown';
+        die "Invalid response from server: $code $msg\n";
+    }
+
+    my $raw = '';
+    if (defined($response->decoded_content)) {
+	$raw = $response->decoded_content;
+    } else {
+	$raw = $response->content;
+    }
+
+    return if $raw eq '';
+
+    my $json = '';
+    eval {
+	$json = from_json($raw);
+    };
+    die "api response is not a json" if $@;
+
+    return $json;
+}
+
+1;
diff --git a/PVE/Network/SDN/Dns/Plugin.pm b/PVE/Network/SDN/Dns/Plugin.pm
index be399b0..ef866b7 100644
--- a/PVE/Network/SDN/Dns/Plugin.pm
+++ b/PVE/Network/SDN/Dns/Plugin.pm
@@ -8,7 +8,6 @@ use PVE::JSONSchema;
 use PVE::Cluster;
 use HTTP::Request;
 use LWP::UserAgent;
-use JSON;
 
 use Data::Dumper;
 use PVE::JSONSchema qw(get_standard_option);
@@ -79,40 +78,4 @@ sub on_update_hook {
     my ($class, $plugin_config) = @_;
 }
 
-#helpers
-sub api_request {
-    my ($method, $url, $headers, $data) = @_;
-
-    my $encoded_data = to_json($data) if $data;
-
-    my $req = HTTP::Request->new($method,$url, $headers, $encoded_data);
-
-    my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 30);
-    my $proxy = undef;
-
-    if ($proxy) {
-        $ua->proxy(['http', 'https'], $proxy);
-    } else {
-        $ua->env_proxy;
-    }
-
-    $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
-
-    my $response = $ua->request($req);
-    my $code = $response->code;
-
-    if ($code !~ /^2(\d+)$/) {
-        my $msg = $response->message || 'unknown';
-        die "Invalid response from server: $code $msg\n";
-    }
-
-    my $raw = '';
-    if (defined($response->decoded_content)) {
-	$raw = $response->decoded_content;
-    } else {
-	$raw = $response->content;
-    }
-    return from_json($raw) if $raw ne '';
-}
-
 1;
diff --git a/PVE/Network/SDN/Dns/PowerdnsPlugin.pm b/PVE/Network/SDN/Dns/PowerdnsPlugin.pm
index 3fbd595..4c00a26 100644
--- a/PVE/Network/SDN/Dns/PowerdnsPlugin.pm
+++ b/PVE/Network/SDN/Dns/PowerdnsPlugin.pm
@@ -69,7 +69,7 @@ sub add_a_record {
     my $params = { rrsets => [ $rrset ] };
 
     eval {
-	PVE::Network::SDN::Dns::Plugin::api_request("PATCH", "$url/zones/$zone", $headers, $params);
+	PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params);
     };
 
     if ($@) {
@@ -106,7 +106,7 @@ sub add_ptr_record {
     my $params = { rrsets => [ $rrset ] };
 
     eval {
-	PVE::Network::SDN::Dns::Plugin::api_request("PATCH", "$url/zones/$zone", $headers, $params);
+	PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params);
     };
 
     if ($@) {
@@ -131,7 +131,7 @@ sub del_a_record {
     my $params = { rrsets => [ $rrset ] };
 
     eval {
-	PVE::Network::SDN::Dns::Plugin::api_request("PATCH", "$url/zones/$zone", $headers, $params);
+	PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params);
     };
 
     if ($@) {
@@ -158,7 +158,7 @@ sub del_ptr_record {
     my $params = { rrsets => [ $rrset ] };
 
     eval {
-	PVE::Network::SDN::Dns::Plugin::api_request("PATCH", "$url/zones/$zone", $headers, $params);
+	PVE::Network::SDN::api_request("PATCH", "$url/zones/$zone", $headers, $params);
     };
 
     if ($@) {
@@ -176,7 +176,7 @@ sub verify_zone {
     my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
 
     eval {
-        PVE::Network::SDN::Dns::Plugin::api_request("GET", "$url/zones/$zone", $headers);
+        PVE::Network::SDN::api_request("GET", "$url/zones/$zone", $headers);
     };
 
     if ($@) {
@@ -241,7 +241,7 @@ sub on_update_hook {
     my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'X-API-Key' => $key];
 
     eval {
-	PVE::Network::SDN::Dns::Plugin::api_request("GET", "$url", $headers);
+	PVE::Network::SDN::api_request("GET", "$url", $headers);
     };
 
     if ($@) {
diff --git a/PVE/Network/SDN/Ipams/NetboxPlugin.pm b/PVE/Network/SDN/Ipams/NetboxPlugin.pm
index f699daa..eae2e59 100644
--- a/PVE/Network/SDN/Ipams/NetboxPlugin.pm
+++ b/PVE/Network/SDN/Ipams/NetboxPlugin.pm
@@ -44,7 +44,7 @@ sub add_subnet {
 	my $params = { prefix => $cidr };
 
 	eval {
-		my $result = PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/ipam/prefixes/", $headers, $params);
+		my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/", $headers, $params);
 	};
 	if ($@) {
 	    die "error add subnet to ipam: $@";
@@ -68,7 +68,7 @@ sub del_subnet {
     return; #fixme: check that prefix is empty exluding gateway, before delete
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("DELETE", "$url/ipam/prefixes/$internalid/", $headers);
+	PVE::Network::SDN::api_request("DELETE", "$url/ipam/prefixes/$internalid/", $headers);
     };
     if ($@) {
 	die "error deleting subnet from ipam: $@";
@@ -89,7 +89,7 @@ sub add_ip {
     my $params = { address => "$ip/$mask", dns_name => $hostname, description => $description };
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/ipam/ip-addresses/", $headers, $params);
+	PVE::Network::SDN::api_request("POST", "$url/ipam/ip-addresses/", $headers, $params);
     };
 
     if ($@) {
@@ -113,7 +113,7 @@ sub update_ip {
     die "can't find ip $ip in ipam" if !$ip_id;
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("PATCH", "$url/ipam/ip-addresses/$ip_id/", $headers, $params);
+	PVE::Network::SDN::api_request("PATCH", "$url/ipam/ip-addresses/$ip_id/", $headers, $params);
     };
     if ($@) {
 	die "error update ip $ip : $@";
@@ -136,7 +136,7 @@ sub add_next_freeip {
 
     my $ip = undef;
     eval {
-	my $result = PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
+	my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
 	$ip = $result->{address};
     };
 
@@ -160,7 +160,7 @@ sub del_ip {
     die "can't find ip $ip in ipam" if !$ip_id;
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers);
+	PVE::Network::SDN::api_request("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers);
     };
     if ($@) {
 	die "error delete ip $ip : $@";
@@ -176,7 +176,7 @@ sub verify_api {
 
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/aggregates/", $headers);
+	PVE::Network::SDN::api_request("GET", "$url/ipam/aggregates/", $headers);
     };
     if ($@) {
 	die "Can't connect to netbox api: $@";
@@ -194,7 +194,7 @@ sub on_update_hook {
 sub get_prefix_id {
     my ($url, $cidr, $headers) = @_;
 
-    my $result = PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/prefixes/?q=$cidr", $headers);
+    my $result = PVE::Network::SDN::api_request("GET", "$url/ipam/prefixes/?q=$cidr", $headers);
     my $data = @{$result->{results}}[0];
     my $internalid = $data->{id};
     return $internalid;
@@ -202,7 +202,7 @@ sub get_prefix_id {
 
 sub get_ip_id {
     my ($url, $ip, $headers) = @_;
-    my $result = PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/ip-addresses/?q=$ip", $headers);
+    my $result = PVE::Network::SDN::api_request("GET", "$url/ipam/ip-addresses/?q=$ip", $headers);
     my $data = @{$result->{results}}[0];
     my $ip_id = $data->{id};
     return $ip_id;
diff --git a/PVE/Network/SDN/Ipams/PhpIpamPlugin.pm b/PVE/Network/SDN/Ipams/PhpIpamPlugin.pm
index b5ff38c..7147916 100644
--- a/PVE/Network/SDN/Ipams/PhpIpamPlugin.pm
+++ b/PVE/Network/SDN/Ipams/PhpIpamPlugin.pm
@@ -62,7 +62,7 @@ sub add_subnet {
 		  };
 
 	eval {
-		PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/subnets/", $headers, $params);
+		PVE::Network::SDN::api_request("POST", "$url/subnets/", $headers, $params);
 	};
 	if ($@) {
 	    die "error add subnet to ipam: $@";
@@ -86,7 +86,7 @@ sub del_subnet {
     return; #fixme: check that prefix is empty exluding gateway, before delete
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("DELETE", "$url/subnets/$internalid", $headers);
+	PVE::Network::SDN::api_request("DELETE", "$url/subnets/$internalid", $headers);
     };
     if ($@) {
 	die "error deleting subnet from ipam: $@";
@@ -114,7 +114,7 @@ sub add_ip {
     $params->{mac} = $mac if $mac;
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/addresses/", $headers, $params);
+	PVE::Network::SDN::api_request("POST", "$url/addresses/", $headers, $params);
     };
 
     if ($@) {
@@ -142,7 +142,7 @@ sub update_ip {
     $params->{mac} = $mac if $mac;
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("PATCH", "$url/addresses/$ip_id", $headers, $params);
+	PVE::Network::SDN::api_request("PATCH", "$url/addresses/$ip_id", $headers, $params);
     };
 
     if ($@) {
@@ -170,7 +170,7 @@ sub add_next_freeip {
 
     my $ip = undef;
     eval {
-	my $result = PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/addresses/first_free/$internalid/", $headers, $params);
+	my $result = PVE::Network::SDN::api_request("POST", "$url/addresses/first_free/$internalid/", $headers, $params);
 	$ip = $result->{data};
     };
 
@@ -194,7 +194,7 @@ sub del_ip {
     return if !$ip_id;
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("DELETE", "$url/addresses/$ip_id", $headers);
+	PVE::Network::SDN::api_request("DELETE", "$url/addresses/$ip_id", $headers);
     };
     if ($@) {
 	die "error delete ip $ip: $@";
@@ -210,7 +210,7 @@ sub verify_api {
     my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Token' => $token];
 
     eval {
-	PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/sections/$sectionid", $headers);
+	PVE::Network::SDN::api_request("GET", "$url/sections/$sectionid", $headers);
     };
     if ($@) {
 	die "Can't connect to phpipam api: $@";
@@ -229,7 +229,7 @@ sub on_update_hook {
 sub get_internalid {
     my ($url, $cidr, $headers) = @_;
 
-    my $result = PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/subnets/cidr/$cidr", $headers);
+    my $result = PVE::Network::SDN::api_request("GET", "$url/subnets/cidr/$cidr", $headers);
     my $data = @{$result->{data}}[0];
     my $internalid = $data->{id};
     return $internalid;
@@ -237,7 +237,7 @@ sub get_internalid {
 
 sub get_ip_id {
     my ($url, $ip, $headers) = @_;
-    my $result = PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/addresses/search/$ip", $headers);
+    my $result = PVE::Network::SDN::api_request("GET", "$url/addresses/search/$ip", $headers);
     my $data = @{$result->{data}}[0];
     my $ip_id = $data->{id};
     return $ip_id;
diff --git a/PVE/Network/SDN/Ipams/Plugin.pm b/PVE/Network/SDN/Ipams/Plugin.pm
index 39675f2..a4c7dcb 100644
--- a/PVE/Network/SDN/Ipams/Plugin.pm
+++ b/PVE/Network/SDN/Ipams/Plugin.pm
@@ -108,41 +108,4 @@ sub on_update_hook {
     my ($class, $plugin_config)  = @_;
 }
 
-
-#helpers
-sub api_request {
-    my ($method, $url, $headers, $data) = @_;
-
-    my $encoded_data = to_json($data) if $data;
-
-    my $req = HTTP::Request->new($method,$url, $headers, $encoded_data);
-
-    my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 30);
-    my $proxy = undef;
-
-    if ($proxy) {
-        $ua->proxy(['http', 'https'], $proxy);
-    } else {
-        $ua->env_proxy;
-    }
-
-    $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
-
-    my $response = $ua->request($req);
-    my $code = $response->code;
-
-    if ($code !~ /^2(\d+)$/) {
-        my $msg = $response->message || 'unknown';
-        die "Invalid response from server: $code $msg\n";
-    }
-
-    my $raw = '';
-    if (defined($response->decoded_content)) {
-	$raw = $response->decoded_content;
-    } else {
-	$raw = $response->content;
-    }
-    return from_json($raw) if $raw ne '';
-}
-
 1;
-- 
2.20.1




More information about the pve-devel mailing list