[pve-devel] [PATCH pve-network 33/38] ipam: add hostname/description to ipam db
Alexandre Derumier
aderumier at odiso.com
Sun Nov 8 15:19:35 CET 2020
---
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
More information about the pve-devel
mailing list