]> git.proxmox.com Git - pve-network.git/commitdiff
add network transport api
authorAlexandre Derumier <aderumier@odiso.com>
Tue, 2 Apr 2019 10:09:10 +0000 (12:09 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 2 Apr 2019 12:15:10 +0000 (14:15 +0200)
PVE/API2/Makefile [new file with mode: 0644]
PVE/API2/Network/Makefile [new file with mode: 0644]
PVE/API2/Network/Transport.pm [new file with mode: 0644]
PVE/Makefile
PVE/Network/Plugin.pm
PVE/Network/Transport.pm [new file with mode: 0644]
PVE/Network/VlanPlugin.pm
PVE/Network/VxlanMulticastPlugin.pm

diff --git a/PVE/API2/Makefile b/PVE/API2/Makefile
new file mode 100644 (file)
index 0000000..5bc7988
--- /dev/null
@@ -0,0 +1,5 @@
+
+
+.PHONY: install
+install:
+       make -C Network install
diff --git a/PVE/API2/Network/Makefile b/PVE/API2/Network/Makefile
new file mode 100644 (file)
index 0000000..92fa5a7
--- /dev/null
@@ -0,0 +1,8 @@
+SOURCES=Transport.pm
+
+
+PERL5DIR=${DESTDIR}/usr/share/perl5
+
+.PHONY: install
+install:
+       for i in ${SOURCES}; do install -D -m 0644 $$i ${PERL5DIR}/PVE/API2/Network/$$i; done
diff --git a/PVE/API2/Network/Transport.pm b/PVE/API2/Network/Transport.pm
new file mode 100644 (file)
index 0000000..bddae40
--- /dev/null
@@ -0,0 +1,235 @@
+package PVE::API2::Network::Transport;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param);
+use PVE::Cluster qw(cfs_read_file cfs_write_file);
+use PVE::Network::Transport;
+use PVE::Network::Plugin;
+use PVE::Network::VlanPlugin;
+use PVE::Network::VxlanMulticastPlugin;
+use Storable qw(dclone);
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::RPCEnvironment;
+
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+my $transport_type_enum = PVE::Network::Plugin->lookup_types();
+
+my $api_transport_config = sub {
+    my ($cfg, $transportid) = @_;
+
+    my $scfg = dclone(PVE::Network::Transport::transport_config($cfg, $transportid));
+    $scfg->{transport} = $transportid;
+    $scfg->{digest} = $cfg->{digest};
+
+    return $scfg;
+};
+
+__PACKAGE__->register_method ({
+    name => 'index', 
+    path => '',
+    method => 'GET',
+    description => "Transport index.",
+    permissions => { 
+       description => "Only list entries where you have 'NetworkTransport.Audit' or 'NetworkTransport.Allocate' permissions on '/networktransports/<transport>'",
+       user => 'all',
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           type => { 
+               description => "Only list transport of specific type",
+               type => 'string', 
+               enum => $transport_type_enum,
+               optional => 1,
+           },
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => { transport => { type => 'string'} },
+       },
+       links => [ { rel => 'child', href => "{transport}" } ],
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+
+       my $cfg = PVE::Network::Transport::config();
+
+       my @sids = PVE::Network::Transport::transports_ids($cfg);
+       my $res = [];
+       foreach my $transportid (@sids) {
+#          my $privs = [ 'NetworkTransport.Audit', 'NetworkTransport.Allocate' ];
+#          next if !$rpcenv->check_any($authuser, "/network/transports/$transportid", $privs, 1);
+
+           my $scfg = &$api_transport_config($cfg, $transportid);
+           next if $param->{type} && $param->{type} ne $scfg->{type};
+           push @$res, $scfg;
+       }
+
+       return $res;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'read', 
+    path => '{transport}',
+    method => 'GET',
+    description => "Read transport configuration.",
+#    permissions => { 
+#      check => ['perm', '/network/transports/{transport}', ['NetworkTransport.Allocate']],
+#   },
+
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           transport => get_standard_option('pve-transport-id'),
+       },
+    },
+    returns => { type => 'object' },
+    code => sub {
+       my ($param) = @_;
+
+       my $cfg = PVE::Network::Transport::config();
+
+       return &$api_transport_config($cfg, $param->{transport});
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'create',
+    protected => 1,
+    path => '', 
+    method => 'POST',
+    description => "Create a new network transport.",
+#    permissions => { 
+#      check => ['perm', '/network/transports', ['NetworkTransport.Allocate']],
+#    },
+    parameters => PVE::Network::Plugin->createSchema(),
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $type = extract_param($param, 'type');
+       my $transportid = extract_param($param, 'transport');
+
+       my $plugin = PVE::Network::Plugin->lookup($type);
+       my $opts = $plugin->check_config($transportid, $param, 1, 1);
+
+        PVE::Network::Transport::lock_transport_config(
+           sub {
+
+               my $cfg = PVE::Network::Transport::config();
+
+               if (my $scfg = PVE::Network::Transport::transport_config($cfg, $transportid, 1)) {
+                   die "network transport ID '$transportid' already defined\n";
+               }
+
+               $cfg->{ids}->{$transportid} = $opts;
+
+               #improveme:
+               #check local configuration of all nodes for conflict
+
+               PVE::Network::Transport::write_config($cfg);
+           
+           }, "create network transport failed");
+
+       return undef;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'update',
+    protected => 1,
+    path => '{transport}',
+    method => 'PUT',
+    description => "Update network transport configuration.",
+#    permissions => { 
+#      check => ['perm', '/network/transports', ['NetworkTransport.Allocate']],
+#    },
+    parameters => PVE::Network::Plugin->updateSchema(),
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $transportid = extract_param($param, 'transport');
+       my $digest = extract_param($param, 'digest');
+
+        PVE::Network::Transport::lock_transport_config(
+        sub {
+
+           my $cfg = PVE::Network::Transport::config();
+
+           PVE::SectionConfig::assert_if_modified($cfg, $digest);
+
+           my $scfg = PVE::Network::Transport::transport_config($cfg, $transportid);
+
+           my $plugin = PVE::Network::Plugin->lookup($scfg->{type});
+           my $opts = $plugin->check_config($transportid, $param, 0, 1);
+
+           foreach my $k (%$opts) {
+               $scfg->{$k} = $opts->{$k};
+           }
+           #improveme:
+            #add vlan/vxlan check on existingvnets
+           #check local configuration of all nodes for conflict
+           PVE::Network::Transport::write_config($cfg);
+
+           }, "update network transport failed");
+
+       return undef;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'delete',
+    protected => 1,
+    path => '{transport}', # /network/transports/{transport}
+    method => 'DELETE',
+    description => "Delete network transport configuration.",
+#    permissions => { 
+#      check => ['perm', '/network/transports', ['NetworkTransport.Allocate']],
+#    },
+    parameters => {
+       additionalProperties => 0,
+       properties => { 
+           transport => get_standard_option('pve-transport-id', {
+                completion => \&PVE::Network::Transport::complete_transport,
+            }),
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $transportid = extract_param($param, 'transport');
+
+        PVE::Network::Transport::lock_transport_config(
+           sub {
+
+               my $cfg = PVE::Network::Transport::config();
+
+               my $scfg = PVE::Network::Transport::transport_config($cfg, $transportid);
+
+#              my $plugin = PVE::Network::Plugin->lookup($scfg->{type});
+#              $plugin->on_delete_hook($transportid, $scfg);
+
+               delete $cfg->{ids}->{$transportid};
+               #improveme:
+               #check that vnet don't use this transport
+               PVE::Network::Transport::write_config($cfg);
+
+           }, "delete network transport failed");
+
+
+       return undef;
+    }});
+
+1;
index 626c2c5304de2c3796881c95d81406712dcfae71..1fb961dbc7fcbb3feafc5dcd66e98f06f8b55cb8 100644 (file)
@@ -1,3 +1,4 @@
 .PHONY: install
 install:
        make -C Network install
+       make -C API2 install
index b186d8f3a33f65f9549593b9da5992276f706ca2..36cd2eda8874c1113df0e1c7c558520fd43348aa 100644 (file)
@@ -8,7 +8,7 @@ use PVE::JSONSchema;
 use PVE::Cluster;
 
 use Data::Dumper;
-
+use PVE::JSONSchema qw(get_standard_option);
 use base qw(PVE::SectionConfig);
 
 PVE::Cluster::cfs_register_file('network/transports.cfg',
@@ -23,11 +23,8 @@ my $defaultData = {
            type => 'string', format => 'pve-configid',
            type => 'string',
        },
-       'uplink-id' => {
-           type => 'integer',
-           minimum => 1, maximum => 4096,
-           description => 'Uplink interface',
-       },
+        transport => get_standard_option('pve-transport-id',
+            { completion => \&PVE::Network::Transport::complete_transport }),
     },
 };
 
diff --git a/PVE/Network/Transport.pm b/PVE/Network/Transport.pm
new file mode 100644 (file)
index 0000000..1b8a2ca
--- /dev/null
@@ -0,0 +1,62 @@
+package PVE::Network::Transport;
+
+use strict;
+use warnings;
+use Data::Dumper;
+use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
+use PVE::Network::Plugin;
+use PVE::Network::VlanPlugin;
+use PVE::Network::VxlanMulticastPlugin;
+
+PVE::Network::VlanPlugin->register();
+PVE::Network::VxlanMulticastPlugin->register();
+PVE::Network::Plugin->init();
+
+
+sub transport_config {
+    my ($cfg, $transportid, $noerr) = @_;
+
+    die "no transport ID specified\n" if !$transportid;
+
+    my $scfg = $cfg->{ids}->{$transportid};
+    die "transport '$transportid' does not exists\n" if (!$noerr && !$scfg);
+
+    return $scfg;
+}
+
+sub config {
+
+    return cfs_read_file("network/transports.cfg");
+}
+
+sub write_config {
+    my ($cfg) = @_;
+
+    cfs_write_file("network/transports.cfg", $cfg);
+}
+
+sub lock_transport_config {
+    my ($code, $errmsg) = @_;
+
+    cfs_lock_file("network/transports.cfg", undef, $code);
+    my $err = $@;
+    if ($err) {
+        $errmsg ? die "$errmsg: $err" : die $err;
+    }
+}
+
+sub transports_ids {
+    my ($cfg) = @_;
+
+    return keys %{$cfg->{ids}};
+}
+
+sub complete_transport {
+    my ($cmdname, $pname, $cvalue) = @_;
+
+    my $cfg = PVE::Network::Transport::config();
+
+    return  $cmdname eq 'add' ? [] : [ PVE::Network::Transport::transports_ids($cfg) ];
+}
+
+1;
index 6f76f18366f9f4ea882e794bf7eb2d5d6a6e31f1..7d64549d6bff2686cc3dba3f055517a455595193 100644 (file)
@@ -21,6 +21,11 @@ sub pve_verify_network_vlanrange {
 
 sub properties {
     return {
+       'uplink-id' => {
+           type => 'integer',
+           minimum => 1, maximum => 4096,
+           description => 'Uplink interface',
+       },
        'vlan-allowed' => {
            type => 'string', format => 'pve-network-vlanrange',
            description => "Allowed vlan range",
@@ -42,7 +47,7 @@ sub properties {
 sub options {
 
     return {
-       'uplink-id' => { fixed => 1 },
+       'uplink-id' => { optional => 1 },
         'vlan-allowed' => { optional => 1 },
        'vlan-protocol' => { optional => 1 },
        'vlan-aware' => { optional => 1 },
index 87499d823c9bb2d209de156714044995576382df..3aa6e352bef0f77a749665e9194dbe2071a00e2c 100644 (file)
@@ -36,7 +36,7 @@ sub properties {
 sub options {
 
     return {
-       'uplink-id' => { fixed => 1 },
+       'uplink-id' => { optional => 1 },
         'multicast-address' => { fixed => 1 },
         'vxlan-allowed' => { optional => 1 },
     };