public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Alexandre Derumier <aderumier@odiso.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH pve-network 33/38] ipam: add hostname/description to ipam db
Date: Sun,  8 Nov 2020 15:19:35 +0100	[thread overview]
Message-ID: <20201108141940.1028443-34-aderumier@odiso.com> (raw)
In-Reply-To: <20201108141940.1028443-1-aderumier@odiso.com>

---
 PVE/Network/SDN/Ipams/NetboxPlugin.pm  |  8 +++---
 PVE/Network/SDN/Ipams/PVEPlugin.pm     | 16 ++++++++----
 PVE/Network/SDN/Ipams/PhpIpamPlugin.pm | 12 ++++++---
 PVE/Network/SDN/Ipams/Plugin.pm        |  2 +-
 PVE/Network/SDN/SubnetPlugin.pm        |  4 ++-
 PVE/Network/SDN/Subnets.pm             | 36 ++++++++++++++------------
 PVE/Network/SDN/Vnets.pm               |  8 +++---
 7 files changed, 50 insertions(+), 36 deletions(-)

diff --git a/PVE/Network/SDN/Ipams/NetboxPlugin.pm b/PVE/Network/SDN/Ipams/NetboxPlugin.pm
index d696b08..298634d 100644
--- a/PVE/Network/SDN/Ipams/NetboxPlugin.pm
+++ b/PVE/Network/SDN/Ipams/NetboxPlugin.pm
@@ -77,7 +77,7 @@ sub del_subnet {
 }
 
 sub add_ip {
-    my ($class, $plugin_config, $subnetid, $subnet, $ip, $is_gateway) = @_;
+    my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $description, $is_gateway) = @_;
 
     my $mask = $subnet->{mask};
     my $url = $plugin_config->{url};
@@ -85,7 +85,7 @@ sub add_ip {
     my $section = $plugin_config->{section};
     my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
 
-    my $params = { address => "$ip/$mask" };
+    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);
@@ -97,7 +97,7 @@ sub add_ip {
 }
 
 sub add_next_freeip {
-    my ($class, $plugin_config, $subnetid, $subnet) = @_;
+    my ($class, $plugin_config, $subnetid, $subnet, $hostname, $description) = @_;
 
     my $cidr = $subnet->{cidr};
 
@@ -107,7 +107,7 @@ sub add_next_freeip {
 
     my $internalid = get_prefix_id($url, $cidr, $headers);
 
-    my $params = {};
+    my $params = { dns_name => $hostname, description => $description };
 
     my $ip = undef;
     eval {
diff --git a/PVE/Network/SDN/Ipams/PVEPlugin.pm b/PVE/Network/SDN/Ipams/PVEPlugin.pm
index 601ad26..b0fd72f 100644
--- a/PVE/Network/SDN/Ipams/PVEPlugin.pm
+++ b/PVE/Network/SDN/Ipams/PVEPlugin.pm
@@ -78,7 +78,7 @@ sub del_subnet {
 }
 
 sub add_ip {
-    my ($class, $plugin_config, $subnetid, $subnet, $ip, $is_gateway) = @_;
+    my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $description, $is_gateway) = @_;
 
     my $cidr = $subnet->{cidr};
     my $zone = $subnet->{zone};
@@ -93,14 +93,17 @@ sub add_ip {
 	my $dbsubnet = $dbzone->{subnets}->{$cidr};
 
 	die "ip $ip already exist" if defined($dbsubnet->{ips}->{$ip});
-	$dbsubnet->{ips}->{$ip} = 1;
+	my $dbip = {};
+	$dbip->{hostname} = $hostname;
+	$dbip->{description} = $description;
+	$dbsubnet->{ips}->{$ip} = $dbip;
 	write_db($db);
     });
     die "$@" if $@;
 }
 
 sub add_next_freeip {
-    my ($class, $plugin_config, $subnetid, $subnet) = @_;
+    my ($class, $plugin_config, $subnetid, $subnet, $hostname, $description) = @_;
 
     my $cidr = $subnet->{cidr};
     my $network = $subnet->{network};
@@ -135,8 +138,11 @@ sub add_next_freeip {
 	}
 
 	die "can't find free ip in subnet $cidr" if !$freeip;
-  
-	$dbsubnet->{ips}->{$freeip} = 1;
+
+	my $dbip = {};
+	$dbip->{hostname} = $hostname;
+	$dbip->{description} = $description;
+	$dbsubnet->{ips}->{$freeip} = $dbip;
 	write_db($db);
     });
     die "$@" if $@;
diff --git a/PVE/Network/SDN/Ipams/PhpIpamPlugin.pm b/PVE/Network/SDN/Ipams/PhpIpamPlugin.pm
index f89ef29..6261764 100644
--- a/PVE/Network/SDN/Ipams/PhpIpamPlugin.pm
+++ b/PVE/Network/SDN/Ipams/PhpIpamPlugin.pm
@@ -95,7 +95,7 @@ sub del_subnet {
 }
 
 sub add_ip {
-    my ($class, $plugin_config, $subnetid, $subnet, $ip, $is_gateway) = @_;
+    my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $description, $is_gateway) = @_;
 
     my $cidr = $subnet->{cidr};
     my $url = $plugin_config->{url};
@@ -108,6 +108,8 @@ sub add_ip {
     my $params = { ip => $ip,
 		   subnetId => $internalid,
 		   is_gateway => $is_gateway,
+		   hostname => $hostname,
+		   description => $description,
 		  };
 
     eval {
@@ -120,7 +122,7 @@ sub add_ip {
 }
 
 sub add_next_freeip {
-    my ($class, $plugin_config, $subnetid, $subnet, $internalid, $hostname) = @_;
+    my ($class, $plugin_config, $subnetid, $subnet, $hostname, $description) = @_;
 
     my $cidr = $subnet->{cidr};  
     my $mask = $subnet->{mask};  
@@ -129,9 +131,11 @@ sub add_next_freeip {
     my $section = $plugin_config->{section};
     my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Token' => $token];
 
-    $internalid = get_internalid($url, $cidr, $headers) if !$internalid;
+    my $internalid = get_internalid($url, $cidr, $headers);
 
-    my $params = {};
+    my $params = { hostname => $hostname,
+		   description => $description,
+		  };
 
     my $ip = undef;
     eval {
diff --git a/PVE/Network/SDN/Ipams/Plugin.pm b/PVE/Network/SDN/Ipams/Plugin.pm
index 4c68287..065225c 100644
--- a/PVE/Network/SDN/Ipams/Plugin.pm
+++ b/PVE/Network/SDN/Ipams/Plugin.pm
@@ -75,7 +75,7 @@ sub del_subnet {
 }
 
 sub add_ip {
-    my ($class, $plugin_config, $subnetid, $subnet, $ip, $is_gateway) = @_;
+    my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $description, $is_gateway) = @_;
 
 }
 
diff --git a/PVE/Network/SDN/SubnetPlugin.pm b/PVE/Network/SDN/SubnetPlugin.pm
index 1444262..cb0f4ef 100644
--- a/PVE/Network/SDN/SubnetPlugin.pm
+++ b/PVE/Network/SDN/SubnetPlugin.pm
@@ -141,7 +141,9 @@ sub on_update_hook {
 	    warn if $@;
 	}
         if(!$old_gateway || $gateway && $gateway ne $old_gateway) {
-	    PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $gateway);
+	    my $hostname = "$vnetid-gw";
+	    my $description = "$vnetid gw";
+	    PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $gateway, $hostname, $description, 1);
 	}
 
 	#delete old gateway after update
diff --git a/PVE/Network/SDN/Subnets.pm b/PVE/Network/SDN/Subnets.pm
index bd1eb36..09aa942 100644
--- a/PVE/Network/SDN/Subnets.pm
+++ b/PVE/Network/SDN/Subnets.pm
@@ -115,11 +115,9 @@ my $get_reversedns_zone = sub {
 };
 
 my $add_dns_record = sub {
-    my ($zone, $dns, $hostname, $dnszoneprefix, $ip) = @_;
+    my ($zone, $dns, $hostname, $ip) = @_;
     return if !$zone || !$dns || !$hostname || !$ip;
 
-    $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
-
     my $dns_cfg = PVE::Network::SDN::Dns::config();
     my $plugin_config = $dns_cfg->{ids}->{$dns};
     my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($plugin_config->{type});
@@ -128,11 +126,10 @@ my $add_dns_record = sub {
 };
 
 my $add_dns_ptr_record = sub {
-    my ($reversezone, $zone, $dns, $hostname, $dnszoneprefix, $ip) = @_;
+    my ($reversezone, $zone, $dns, $hostname, $ip) = @_;
 
     return if !$zone || !$reversezone || !$dns || !$hostname || !$ip;
 
-    $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
     $hostname .= ".$zone";
     my $dns_cfg = PVE::Network::SDN::Dns::config();
     my $plugin_config = $dns_cfg->{ids}->{$dns};
@@ -141,12 +138,10 @@ my $add_dns_ptr_record = sub {
 };
 
 my $del_dns_record = sub {
-    my ($zone, $dns, $hostname, $dnszoneprefix, $ip) = @_;
+    my ($zone, $dns, $hostname, $ip) = @_;
 
     return if !$zone || !$dns || !$hostname || !$ip;
 
-    $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
-
     my $dns_cfg = PVE::Network::SDN::Dns::config();
     my $plugin_config = $dns_cfg->{ids}->{$dns};
     my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($plugin_config->{type});
@@ -165,10 +160,11 @@ my $del_dns_ptr_record = sub {
 };
 
 sub next_free_ip {
-    my ($zone, $subnetid, $subnet, $hostname) = @_;
+    my ($zone, $subnetid, $subnet, $hostname, $description) = @_;
 
     my $cidr = undef;
     my $ip = undef;
+    $description = '' if !$description;
 
     my $ipamid = $zone->{ipam};
     my $dns = $zone->{dns};
@@ -176,6 +172,8 @@ sub next_free_ip {
     my $reversedns = $zone->{reversedns};
     my $dnszoneprefix = $subnet->{dnszoneprefix};
 
+    $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
+
     #verify dns zones before ipam
     &$verify_dns_zone($dnszone, $dns);
 
@@ -184,7 +182,7 @@ sub next_free_ip {
 	my $plugin_config = $ipam_cfg->{ids}->{$ipamid};
 	my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
 	eval {
-	    $cidr = $plugin->add_next_freeip($plugin_config, $subnetid, $subnet);
+	    $cidr = $plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $description);
 	    ($ip, undef) = split(/\//, $cidr);
 	};
 	die $@ if $@;
@@ -194,9 +192,9 @@ sub next_free_ip {
 	my $reversednszone = &$get_reversedns_zone($subnetid, $subnet, $reversedns, $ip);
 
 	#add dns
-	&$add_dns_record($dnszone, $dns, $hostname, $dnszoneprefix, $ip);
+	&$add_dns_record($dnszone, $dns, $hostname, $ip);
 	#add reverse dns
-	&$add_dns_ptr_record($reversednszone, $dnszone, $reversedns, $hostname, $dnszoneprefix, $ip);
+	&$add_dns_ptr_record($reversednszone, $dnszone, $reversedns, $hostname, $ip);
     };
     if ($@) {
 	#rollback
@@ -210,7 +208,7 @@ sub next_free_ip {
 }
 
 sub add_ip {
-    my ($zone, $subnetid, $subnet, $ip, $hostname) = @_;
+    my ($zone, $subnetid, $subnet, $ip, $hostname, $description) = @_;
 
     return if !$subnet || !$ip; 
 
@@ -221,6 +219,8 @@ sub add_ip {
     my $reversednszone = &$get_reversedns_zone($subnetid, $subnet, $reversedns, $ip);
     my $dnszoneprefix = $subnet->{dnszoneprefix};
 
+    $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
+
     #verify dns zones before ipam
     &$verify_dns_zone($dnszone, $dns);
     &$verify_dns_zone($reversednszone, $reversedns);
@@ -230,16 +230,16 @@ sub add_ip {
 	my $plugin_config = $ipam_cfg->{ids}->{$ipamid};
 	my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
 	eval {
-	    $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip);
+	    $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $description);
 	};
 	die $@ if $@;
     }
 
     eval {
 	#add dns
-	&$add_dns_record($dnszone, $dns, $hostname, $dnszoneprefix, $ip);
+	&$add_dns_record($dnszone, $dns, $hostname, $ip);
 	#add reverse dns
-	&$add_dns_ptr_record($reversednszone, $dnszone, $reversedns, $hostname, $dnszoneprefix, $ip);
+	&$add_dns_ptr_record($reversednszone, $dnszone, $reversedns, $hostname, $ip);
     };
     if ($@) {
 	#rollback
@@ -262,6 +262,8 @@ sub del_ip {
     my $reversedns = $zone->{reversedns};
     my $reversednszone = &$get_reversedns_zone($subnetid, $subnet, $reversedns, $ip);
     my $dnszoneprefix = $subnet->{dnszoneprefix};
+    $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
+
 
     &$verify_dns_zone($dnszone, $dns);
     &$verify_dns_zone($reversednszone, $reversedns);
@@ -274,7 +276,7 @@ sub del_ip {
     }
 
     eval {
-	&$del_dns_record($dnszone, $dns, $hostname, $dnszoneprefix, $ip);
+	&$del_dns_record($dnszone, $dns, $hostname, $ip);
 	&$del_dns_ptr_record($reversednszone, $reversedns, $ip);
     };
     if ($@) {
diff --git a/PVE/Network/SDN/Vnets.pm b/PVE/Network/SDN/Vnets.pm
index 6d11003..5616419 100644
--- a/PVE/Network/SDN/Vnets.pm
+++ b/PVE/Network/SDN/Vnets.pm
@@ -78,7 +78,7 @@ sub get_subnets {
 }
 
 sub get_next_free_cidr {
-    my ($vnetid, $hostname, $ipversion) = @_;
+    my ($vnetid, $hostname, $description, $ipversion) = @_;
 
     my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
     my $zoneid = $vnet->{zone};
@@ -97,7 +97,7 @@ sub get_next_free_cidr {
 	$subnetcount++;
 	if ($zone->{ipam}) {
 	    eval {
-		$ip = PVE::Network::SDN::Subnets::next_free_ip($zone, $subnetid, $subnet, $hostname);
+		$ip = PVE::Network::SDN::Subnets::next_free_ip($zone, $subnetid, $subnet, $hostname, $description);
 	    };
 	    warn $@ if $@;
 	}
@@ -109,7 +109,7 @@ sub get_next_free_cidr {
 }
 
 sub add_cidr {
-    my ($vnetid, $cidr, $hostname) = @_;
+    my ($vnetid, $cidr, $hostname, $description) = @_;
 
     my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
     my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
@@ -120,7 +120,7 @@ sub add_cidr {
     die "ip address is not in cidr format" if !$mask;
     my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $mask, $subnets);
 
-    PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname);
+    PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $description);
 }
 
 sub del_cidr {
-- 
2.20.1




  parent reply	other threads:[~2020-11-08 14:20 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-08 14:19 [pve-devel] [PATCH pve-network 00/38] add subnet plugin Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 01/38] vnets: add subnets Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 02/38] add subnets verifications hooks Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 03/38] zones: simple|evpn: add gateway ip from subnets to vnet Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 04/38] zone: add vnet_update_hook Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 05/38] vnets: subnets: use cidr Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 06/38] subnet: fix on_delete_hook Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 07/38] api2: subnet create: convert cidr to subnetid Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 08/38] api2: increase version on apply/reload only Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 09/38] add ipams plugins Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 10/38] add pve internal ipam plugin Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 11/38] vnets: find_free_ip : add ipversion detection Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 12/38] vnets: add add_ip Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 13/38] vnets: add del_ip + rework add_ip/find_free_ip Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 14/38] add dns plugin Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 15/38] Fix vnet gateway for routed setup + /32 pointopoint subnet Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 16/38] ipam : pveplugin : fix find_next_free_ip Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 17/38] add vnet to subnets && remove subnetlist from vnet Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 18/38] zones: evpn|simple: add snat iptables rules Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 19/38] subnet: disable route option for now and add dns domain format Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 20/38] dns: fix reverse dns Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 21/38] subnets: move api to /sdn/vnet/<vnet>/subnets && make vnet option not optionnal Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 22/38] zones: evpn : fix raise exception Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 23/38] subnet: make ipam not optionnal and use pve ipam as default Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 24/38] don't allow subnets on vlanware vnet Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 25/38] generate sdn/.running-config on apply Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 26/38] api: add running/pending zones/vnets/subnets/controllers Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 27/38] small bugfixes Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 28/38] move dns options from subnets to zone Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 29/38] move ipam option from subnet " Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 30/38] subnets/ipam: allow same subnet on different zones Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 31/38] vnets: allow duplicate tags in differents zones Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 32/38] ipam: verify api access on create/update Alexandre Derumier
2020-11-08 14:19 ` Alexandre Derumier [this message]
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 34/38] update documentation Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 35/38] vnets: remove unused hash:diff Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 36/38] zones: auto find controller Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 37/38] sdn: controllers : add ebgp support Alexandre Derumier
2020-11-08 14:19 ` [pve-devel] [PATCH pve-network 38/38] update test documentation Alexandre Derumier
2020-11-22 18:17 ` [pve-devel] [PATCH pve-network 00/38] add subnet plugin Thomas Lamprecht
2020-11-23 10:04   ` alexandre derumier

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=20201108141940.1028443-34-aderumier@odiso.com \
    --to=aderumier@odiso.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