]> git.proxmox.com Git - pve-network.git/blobdiff - PVE/API2/Network/SDN/Zones.pm
subnets/ipam: allow same subnet on different zones
[pve-network.git] / PVE / API2 / Network / SDN / Zones.pm
index a4478130a4eea1a172d5a30600f9e2f7658d6744..5ae577b3840f9c5cc5a4db17b21cc3f8bc3c8dc0 100644 (file)
@@ -6,18 +6,23 @@ use warnings;
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
 use PVE::Cluster qw(cfs_read_file cfs_write_file);
+use PVE::Network::SDN;
 use PVE::Network::SDN::Vnets;
 use PVE::Network::SDN::Zones;
+use PVE::Network::SDN::Subnets;
+use PVE::Network::SDN::Dns;
 use PVE::Network::SDN::Zones::Plugin;
 use PVE::Network::SDN::Zones::VlanPlugin;
 use PVE::Network::SDN::Zones::QinQPlugin;
 use PVE::Network::SDN::Zones::VxlanPlugin;
 use PVE::Network::SDN::Zones::EvpnPlugin;
 use PVE::Network::SDN::Zones::FaucetPlugin;
+use PVE::Network::SDN::Zones::SimplePlugin;
 
 use Storable qw(dclone);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RPCEnvironment;
+use PVE::Exception qw(raise raise_param_exc);
 
 use PVE::RESTHandler;
 
@@ -32,6 +37,15 @@ my $api_sdn_zones_config = sub {
     $scfg->{zone} = $id;
     $scfg->{digest} = $cfg->{digest};
 
+    if ($scfg->{nodes}) {
+        $scfg->{nodes} = PVE::Network::SDN::Zones::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
+    }
+
+    my $pending = $scfg->{pending};
+    if ($pending->{nodes}) {
+        $pending->{nodes} = PVE::Network::SDN::Zones::Plugin->encode_value($scfg->{type}, 'nodes', $pending->{nodes});
+    }
+
     return $scfg;
 };
 
@@ -41,7 +55,7 @@ __PACKAGE__->register_method ({
     method => 'GET',
     description => "SDN zones index.",
     permissions => {
-       description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/cluster/sdn/zones/<zone>'",
+       description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/zones/<zone>'",
        user => 'all',
     },
     parameters => {
@@ -53,14 +67,32 @@ __PACKAGE__->register_method ({
                enum => $sdn_zones_type_enum,
                optional => 1,
            },
+           running => {
+               type => 'boolean',
+               optional => 1,
+               description => "Display running config.",
+           },
+           pending => {
+               type => 'boolean',
+               optional => 1,
+               description => "Display pending config.",
+           },
        },
     },
     returns => {
        type => 'array',
        items => {
            type => "object",
-           properties => { zone => { type => 'string'}, 
+           properties => { zone => { type => 'string'},
                            type => { type => 'string'},
+                           mtu => { type => 'integer', optional => 1 },
+                           dns => { type => 'string', optional => 1},
+                           reversedns => { type => 'string', optional => 1},
+                           dnszone => { type => 'string', optional => 1},
+                           ipam => { type => 'string', optional => 1},
+                           pending => { optional => 1},
+                           state => { type => 'string', optional => 1},
+                           nodes => { type => 'string', optional => 1},
                          },
        },
        links => [ { rel => 'child', href => "{zone}" } ],
@@ -71,14 +103,23 @@ __PACKAGE__->register_method ({
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
 
-
-       my $cfg = PVE::Network::SDN::Zones::config();
+       my $cfg = {};
+       if($param->{pending}) {
+           my $running_cfg = PVE::Network::SDN::config();
+           my $config = PVE::Network::SDN::Zones::config();
+           $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'zones');
+        } elsif ($param->{running}) {
+           my $running_cfg = PVE::Network::SDN::config();
+           $cfg = $running_cfg->{zones};
+        } else {
+           $cfg = PVE::Network::SDN::Zones::config();
+        }
 
        my @sids = PVE::Network::SDN::Zones::sdn_zones_ids($cfg);
        my $res = [];
        foreach my $id (@sids) {
-#          my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
-#          next if !$rpcenv->check_any($authuser, "/cluster/sdn/zones/$id", $privs, 1);
+           my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
+           next if !$rpcenv->check_any($authuser, "/sdn/zones/$id", $privs, 1);
 
            my $scfg = &$api_sdn_zones_config($cfg, $id);
            next if $param->{type} && $param->{type} ne $scfg->{type};
@@ -96,21 +137,41 @@ __PACKAGE__->register_method ({
     path => '{zone}',
     method => 'GET',
     description => "Read sdn zone configuration.",
-#    permissions => {
-#      check => ['perm', '/cluster/sdn/zones/{zone}', ['SDN.Allocate']],
-#   },
+    permissions => {
+       check => ['perm', '/sdn/zones/{zone}', ['SDN.Allocate']],
+   },
 
     parameters => {
        additionalProperties => 0,
        properties => {
            zone => get_standard_option('pve-sdn-zone-id'),
+           running => {
+               type => 'boolean',
+               optional => 1,
+               description => "Display running config.",
+           },
+           pending => {
+               type => 'boolean',
+               optional => 1,
+               description => "Display pending config.",
+           }
        },
     },
     returns => { type => 'object' },
     code => sub {
        my ($param) = @_;
 
-       my $cfg = PVE::Network::SDN::Zones::config();
+       my $cfg = {};
+       if($param->{pending}) {
+           my $running_cfg = PVE::Network::SDN::config();
+           my $config = PVE::Network::SDN::Zones::config();
+           $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'zones');
+        } elsif ($param->{running}) {
+           my $running_cfg = PVE::Network::SDN::config();
+           $cfg = $running_cfg->{zones};
+        } else {
+           $cfg = PVE::Network::SDN::Zones::config();
+        }
 
        return &$api_sdn_zones_config($cfg, $param->{zone});
     }});
@@ -121,9 +182,9 @@ __PACKAGE__->register_method ({
     path => '',
     method => 'POST',
     description => "Create a new sdn zone object.",
-#    permissions => {
-#      check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']],
-#    },
+    permissions => {
+       check => ['perm', '/sdn/zones', ['SDN.Allocate']],
+    },
     parameters => PVE::Network::SDN::Zones::Plugin->createSchema(),
     returns => { type => 'null' },
     code => sub {
@@ -135,68 +196,39 @@ __PACKAGE__->register_method ({
        my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($type);
        my $opts = $plugin->check_config($id, $param, 1, 1);
 
-        PVE::Network::SDN::Zones::lock_sdn_zones_config(
+        # create /etc/pve/sdn directory
+        PVE::Cluster::check_cfs_quorum();
+        mkdir("/etc/pve/sdn");
+
+        PVE::Network::SDN::lock_sdn_config(
            sub {
 
-               my $cfg = PVE::Network::SDN::Zones::config();
+               my $zone_cfg = PVE::Network::SDN::Zones::config();
+               my $controller_cfg = PVE::Network::SDN::Controllers::config();
+               my $dns_cfg = PVE::Network::SDN::Dns::config();
 
                my $scfg = undef;
-               if ($scfg = PVE::Network::SDN::Zones::sdn_zones_config($cfg, $id, 1)) {
+               if ($scfg = PVE::Network::SDN::Zones::sdn_zones_config($zone_cfg, $id, 1)) {
                    die "sdn zone object ID '$id' already defined\n";
                }
+               
+               my $dnsserver = $opts->{dns};
+               my $reversednsserver = $opts->{reversedns};
+               my $dnszone = $opts->{dnszone};
+               raise_param_exc({ dns => "$dnsserver don't exist"}) if $dnsserver && !$dns_cfg->{ids}->{$dnsserver};
+               raise_param_exc({ reversedns => "$reversednsserver don't exist"}) if $reversednsserver && !$dns_cfg->{ids}->{$reversednsserver};
+               raise_param_exc({ dnszone => "missing dns server"}) if $dnszone && !$dnsserver;
 
-               $cfg->{ids}->{$id} = $opts;
-               $plugin->on_update_hook($id, $cfg);
+               my $ipam = $opts->{ipam};
+               my $ipam_cfg = PVE::Network::SDN::Ipams::config();
+               raise_param_exc({ ipam => "$ipam not existing"}) if $ipam && !$ipam_cfg->{ids}->{$ipam};
 
-               PVE::Network::SDN::Zones::write_config($cfg);
+               $zone_cfg->{ids}->{$id} = $opts;
+               $plugin->on_update_hook($id, $zone_cfg, $controller_cfg);
 
-           }, "create sdn zone object failed");
+               PVE::Network::SDN::Zones::write_config($zone_cfg);
 
-       return undef;
-    }});
-
-__PACKAGE__->register_method ({
-    name => 'apply_configuration',
-    protected => 1,
-    path => '',
-    method => 'PUT',
-    description => "Apply sdn zone changes.",
-#    permissions => {
-#      check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']],
-#    },
-    parameters => {
-       additionalProperties => 0,
-    },
-    returns => { type => 'null' },
-    code => sub {
-       my ($param) = @_;
-
-       die "no sdn zone changes to apply" if !-e "/etc/pve/sdn/zones.cfg.new";
-       rename("/etc/pve/sdn/zones.cfg.new", "/etc/pve/sdn/zones.cfg")
-           || die "applying sdn/zones.cfg changes failed - $!\n";
-
-
-       return undef;
-    }});
-
-__PACKAGE__->register_method ({
-    name => 'revert_configuration',
-    protected => 1,
-    path => '',
-    method => 'DELETE',
-    description => "Revert sdn zone changes.",
-#    permissions => {
-#      check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']],
-#    },
-    parameters => {
-       additionalProperties => 0,
-    },
-    returns => { type => 'null' },
-    code => sub {
-       my ($param) = @_;
-
-       die "no sdn zones changes to revert" if !-e "/etc/pve/sdn/zones.cfg.new";
-       unlink "/etc/pve/sdn/zones.cfg.new";
+           }, "create sdn zone object failed");
 
        return undef;
     }});
@@ -207,9 +239,9 @@ __PACKAGE__->register_method ({
     path => '{zone}',
     method => 'PUT',
     description => "Update sdn zone object configuration.",
-#    permissions => {
-#      check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']],
-#    },
+    permissions => {
+       check => ['perm', '/sdn/zones', ['SDN.Allocate']],
+    },
     parameters => PVE::Network::SDN::Zones::Plugin->updateSchema(),
     returns => { type => 'null' },
     code => sub {
@@ -218,25 +250,48 @@ __PACKAGE__->register_method ({
        my $id = extract_param($param, 'zone');
        my $digest = extract_param($param, 'digest');
 
-        PVE::Network::SDN::Zones::lock_sdn_zones_config(
+        PVE::Network::SDN::lock_sdn_config(
         sub {
 
-           my $cfg = PVE::Network::SDN::Zones::config();
+           my $zone_cfg = PVE::Network::SDN::Zones::config();
+           my $controller_cfg = PVE::Network::SDN::Controllers::config();
+           my $dns_cfg = PVE::Network::SDN::Dns::config();
 
-           PVE::SectionConfig::assert_if_modified($cfg, $digest);
+           PVE::SectionConfig::assert_if_modified($zone_cfg, $digest);
 
-           my $scfg = PVE::Network::SDN::Zones::sdn_zones_config($cfg, $id);
+           my $scfg = PVE::Network::SDN::Zones::sdn_zones_config($zone_cfg, $id);
 
            my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($scfg->{type});
            my $opts = $plugin->check_config($id, $param, 0, 1);
 
+           if($opts->{ipam} ne $scfg->{ipam}) {
+
+               #don't allow ipam change if subnet are defined
+               my $subnets_cfg = PVE::Network::SDN::Subnets::config();
+               foreach my $subnetid (sort keys %{$subnets_cfg->{ids}}) {
+                   my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($subnets_cfg, $subnetid);
+                   raise_param_exc({ ipam => "can't change ipam if subnet if already defined for this zone"}) if $subnet->{zone} eq $id;
+               }
+           }
+
            foreach my $k (%$opts) {
                $scfg->{$k} = $opts->{$k};
            }
 
-           $plugin->on_update_hook($id, $cfg);
+           my $dnsserver = $opts->{dns};
+           my $reversednsserver = $opts->{reversedns};
+           my $dnszone = $opts->{dnszone};
+           raise_param_exc({ dns => "$dnsserver don't exist"}) if $dnsserver && !$dns_cfg->{ids}->{$dnsserver};
+           raise_param_exc({ reversedns => "$reversednsserver don't exist"}) if $reversednsserver && !$dns_cfg->{ids}->{$reversednsserver};
+           raise_param_exc({ dnszone => "missing dns server"}) if $dnszone && !$dnsserver;
+
+           my $ipam = $opts->{ipam};
+           my $ipam_cfg = PVE::Network::SDN::Ipams::config();
+           raise_param_exc({ ipam => "$ipam not existing"}) if $ipam && !$ipam_cfg->{ids}->{$ipam};
 
-           PVE::Network::SDN::Zones::write_config($cfg);
+           $plugin->on_update_hook($id, $zone_cfg, $controller_cfg);
+
+           PVE::Network::SDN::Zones::write_config($zone_cfg);
 
            }, "update sdn zone object failed");
 
@@ -249,9 +304,9 @@ __PACKAGE__->register_method ({
     path => '{zone}',
     method => 'DELETE',
     description => "Delete sdn zone object configuration.",
-#    permissions => {
-#      check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']],
-#    },
+    permissions => {
+       check => ['perm', '/sdn/zones', ['SDN.Allocate']],
+    },
     parameters => {
        additionalProperties => 0,
        properties => {
@@ -266,7 +321,7 @@ __PACKAGE__->register_method ({
 
        my $id = extract_param($param, 'zone');
 
-        PVE::Network::SDN::Zones::lock_sdn_zones_config(
+        PVE::Network::SDN::lock_sdn_config(
            sub {
 
                my $cfg = PVE::Network::SDN::Zones::config();