]> git.proxmox.com Git - pve-network.git/commitdiff
vnets: add subnets
authorAlexandre Derumier <aderumier@odiso.com>
Mon, 5 Oct 2020 15:08:39 +0000 (17:08 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 6 Oct 2020 15:30:04 +0000 (17:30 +0200)
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
PVE/API2/Network/SDN/Subnets.pm
PVE/API2/Network/SDN/Vnets.pm
PVE/Network/SDN/SubnetPlugin.pm
PVE/Network/SDN/VnetPlugin.pm

index 2d6adbd6605f60d49e5baabcf5d0c27d1457bb15..87dc119571aba5c8c4aec14eef667ace0b3f2c65 100644 (file)
@@ -9,6 +9,7 @@ use PVE::Cluster qw(cfs_read_file cfs_write_file);
 use PVE::Network::SDN;
 use PVE::Network::SDN::Subnets;
 use PVE::Network::SDN::SubnetPlugin;
+use PVE::Network::SDN::Vnets;
 
 use Storable qw(dclone);
 use PVE::JSONSchema qw(get_standard_option);
@@ -206,9 +207,11 @@ __PACKAGE__->register_method ({
 
                my $scfg = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id);
 
-               my $subnet_cfg = PVE::Network::SDN::Subnets::config();
+               my $subnets_cfg = PVE::Network::SDN::Subnets::config();
+               my $vnets_cfg = PVE::Network::SDN::Vnets::config();
 
                delete $cfg->{ids}->{$id};
+               PVE::Network::SDN::SubnetPlugin->on_delete_hook($id, $subnets_cfg, $vnets_cfg);
                PVE::Network::SDN::Subnets::write_config($cfg);
                PVE::Network::SDN::increase_version();
 
index c5860c8dff17cbde49f9f190e6fe3feede26cec9..23bc8bb6bd744d6864a12d3070687da346ffb17d 100644 (file)
@@ -11,6 +11,7 @@ use PVE::Network::SDN::Zones;
 use PVE::Network::SDN::Zones::Plugin;
 use PVE::Network::SDN::Vnets;
 use PVE::Network::SDN::VnetPlugin;
+use PVE::Network::SDN::Subnets;
 
 use Storable qw(dclone);
 use PVE::JSONSchema qw(get_standard_option);
@@ -132,7 +133,9 @@ __PACKAGE__->register_method ({
            my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
            $plugin->verify_tag($opts->{tag});
 
-           PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg);
+           my $subnet_cfg = PVE::Network::SDN::Subnets::config();
+
+           PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg, $subnet_cfg);
 
            PVE::Network::SDN::Vnets::write_config($cfg);
            PVE::Network::SDN::increase_version();
@@ -173,7 +176,9 @@ __PACKAGE__->register_method ({
            my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
            $plugin->verify_tag($opts->{tag});
 
-           PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg);
+           my $subnet_cfg = PVE::Network::SDN::Subnets::config();
+
+           PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg, $subnet_cfg);
 
            PVE::Network::SDN::Vnets::write_config($cfg);
            PVE::Network::SDN::increase_version();
index 8900681bd17b47f6d79ffb77bbd6eadedc45067b..ea47684bf7287c0bb9590de17c784457fb88de18 100644 (file)
@@ -112,4 +112,20 @@ sub on_update_hook {
     raise_param_exc({ gateway => "$gateway is not in subnet $subnet"}) if $gateway && !$subnet_matcher->($gateway);
 }
 
+sub on_delete_hook {
+    my ($class, $subnetid, $subnet_cfg, $vnet_cfg) = @_;
+
+    #verify if vnets have subnet
+    foreach my $vnetid (keys %{$vnet_cfg->{ids}}) {
+       my $vnet = $vnet_cfg->{ids}->{$vnetid};
+       my @subnets = PVE::Tools::split_list($vnet->{subnets}) if $vnet->{subnets};
+       foreach my $subnet (@subnets) {
+           my $id = $subnet =~ s/\//-/r;
+           raise_param_exc({ subnet => "$subnet is attached to vnet $vnetid"}) if $id eq $subnetid;
+       }
+    }
+
+    return;
+}
+
 1;
index 384358c29f1f00086bf6d74f7b24c480eda7b16d..6b2bcc85f5235583a2220387460ce64205649c1f 100644 (file)
@@ -68,16 +68,11 @@ sub properties {
             description => "alias name of the vnet",
            optional => 1,
         },
-        ipv4 => {
-            description => "Anycast router ipv4 address.",
-            type => 'string', format => 'CIDRv4',
-            optional => 1,
-        },
-       ipv6 => {
-           description => "Anycast router ipv6 address.",
-           type => 'string', format => 'CIDRv6',
+        subnets => {
+            type => 'string',
+            description => "Subnets list",
            optional => 1,
-       },
+        },
         mac => {
             type => 'string',
             description => "Anycast router mac address",
@@ -91,8 +86,7 @@ sub options {
         zone => { optional => 0},
         tag => { optional => 1},
         alias => { optional => 1 },
-        ipv4 => { optional => 1 },
-        ipv6 => { optional => 1 },
+        subnets => { optional => 1 },
         mac => { optional => 1 },
         vlanaware => { optional => 1 },
     };
@@ -105,7 +99,7 @@ sub on_delete_hook {
 }
 
 sub on_update_hook {
-    my ($class, $vnetid, $vnet_cfg) = @_;
+    my ($class, $vnetid, $vnet_cfg, $subnet_cfg) = @_;
     # verify that tag is not already defined in another vnet
     if (defined($vnet_cfg->{ids}->{$vnetid}->{tag})) {
        my $tag = $vnet_cfg->{ids}->{$vnetid}->{tag};
@@ -117,6 +111,13 @@ sub on_update_hook {
            }
        }
     }
+
+    #verify subnet
+    my @subnets = PVE::Tools::split_list($vnet_cfg->{ids}->{$vnetid}->{subnets}) if $vnet_cfg->{ids}->{$vnetid}->{subnets};
+    foreach my $subnet (@subnets) {
+       my $id = $subnet =~ s/\//-/r;
+       raise_param_exc({ subnet => "$subnet not existing"}) if !$subnet_cfg->{ids}->{$id};
+    }
 }
 
 1;