}
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};
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);
}
sub add_next_freeip {
- my ($class, $plugin_config, $subnetid, $subnet) = @_;
+ my ($class, $plugin_config, $subnetid, $subnet, $hostname, $description) = @_;
my $cidr = $subnet->{cidr};
my $internalid = get_prefix_id($url, $cidr, $headers);
- my $params = {};
+ my $params = { dns_name => $hostname, description => $description };
my $ip = undef;
eval {
}
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};
die "IP '$ip' already exist\n" if defined($dbsubnet->{ips}->{$ip});
- $dbsubnet->{ips}->{$ip} = 1;
+ $dbsubnet->{ips}->{$ip} = {
+ hostname => $hostname,
+ description => $description,
+ };
write_db($db);
});
}
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};
die "can't find free ip in subnet '$cidr'\n" if !$freeip;
- $dbsubnet->{ips}->{$freeip} = 1;
+ $dbsubnet->{ips}->{$freeip} = {
+ hostname => $hostname,
+ description => $description,
+ };
+
write_db($db);
});
die "$@" if $@;
}
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};
my $params = { ip => $ip,
subnetId => $internalid,
is_gateway => $is_gateway,
+ hostname => $hostname,
+ description => $description,
};
eval {
}
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};
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 {
}
sub add_ip {
- my ($class, $plugin_config, $subnetid, $subnet, $ip, $is_gateway) = @_;
+ my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $description, $is_gateway) = @_;
}
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
};
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});
};
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};
};
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});
};
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};
my $reversedns = $zone->{reversedns};
my $dnszoneprefix = $subnet->{dnszoneprefix};
+ $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
+
#verify dns zones before ipam
&$verify_dns_zone($dnszone, $dns);
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 $@;
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
}
sub add_ip {
- my ($zone, $subnetid, $subnet, $ip, $hostname) = @_;
+ my ($zone, $subnetid, $subnet, $ip, $hostname, $description) = @_;
return if !$subnet || !$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);
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
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);
}
eval {
- &$del_dns_record($dnszone, $dns, $hostname, $dnszoneprefix, $ip);
+ &$del_dns_record($dnszone, $dns, $hostname, $ip);
&$del_dns_ptr_record($reversednszone, $reversedns, $ip);
};
if ($@) {
}
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};
$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 $@;
}
}
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);
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 {