This API module provides CRUD functionality for fabrics. The list
endpoint works analogous to the existing SDN endpoints with their
pending / running parameters.

Co-authored-by: Gabriel Goller <g.gol...@proxmox.com>
Signed-off-by: Stefan Hanreich <s.hanre...@proxmox.com>
---
 src/PVE/API2/Network/SDN/Fabrics.pm        |   8 +
 src/PVE/API2/Network/SDN/Fabrics/Fabric.pm | 229 +++++++++++++++++++++
 src/PVE/API2/Network/SDN/Fabrics/Makefile  |   8 +
 3 files changed, 245 insertions(+)
 create mode 100644 src/PVE/API2/Network/SDN/Fabrics/Fabric.pm
 create mode 100644 src/PVE/API2/Network/SDN/Fabrics/Makefile

diff --git a/src/PVE/API2/Network/SDN/Fabrics.pm 
b/src/PVE/API2/Network/SDN/Fabrics.pm
index 9333700..4ec9b79 100644
--- a/src/PVE/API2/Network/SDN/Fabrics.pm
+++ b/src/PVE/API2/Network/SDN/Fabrics.pm
@@ -8,9 +8,16 @@ use PVE::Tools qw(extract_param);
 use PVE::Network::SDN;
 use PVE::Network::SDN::Fabrics;
 
+use PVE::API2::Network::SDN::Fabrics::Fabric;
+
 use PVE::RESTHandler;
 use base qw(PVE::RESTHandler);
 
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::Network::SDN::Fabrics::Fabric",
+    path => 'fabric',
+});
+
 __PACKAGE__->register_method ({
     name => 'index',
     path => '',
@@ -36,6 +43,7 @@ __PACKAGE__->register_method ({
        my ($param) = @_;
 
        my $res = [
+           { subdir => 'fabric' },
            { subdir => 'all' },
        ];
 
diff --git a/src/PVE/API2/Network/SDN/Fabrics/Fabric.pm 
b/src/PVE/API2/Network/SDN/Fabrics/Fabric.pm
new file mode 100644
index 0000000..028b352
--- /dev/null
+++ b/src/PVE/API2/Network/SDN/Fabrics/Fabric.pm
@@ -0,0 +1,229 @@
+package PVE::API2::Network::SDN::Fabrics::Fabric;
+
+use strict;
+use warnings;
+
+use PVE::Network::SDN;
+use PVE::Network::SDN::Fabrics;
+
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::Tools qw(extract_param);
+
+use PVE::RESTHandler;
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    name => 'index',
+    path => '',
+    method => 'GET',
+    permissions => {
+       description => "Only list entries where you have 'SDN.Audit' or 
'SDN.Allocate' permissions on '/sdn/fabrics/<fabric>'",
+       user => 'all'
+    },
+    description => "SDN Fabrics Index",
+    parameters => {
+       properties => {
+           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 => PVE::Network::SDN::Fabrics::fabric_properties(0),
+       },
+       links => [ { rel => 'child', href => "{id}" } ],
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $pending = extract_param($param, 'pending');
+       my $running = extract_param($param, 'running');
+
+       my $digest;
+       my $fabrics;
+
+       if ($pending) {
+           my $current_config = PVE::Network::SDN::Fabrics::config();
+           my $running_config = PVE::Network::SDN::Fabrics::config(1);
+
+           my $pending_fabrics = PVE::Network::SDN::pending_config(
+               { fabrics => { ids => $running_config->list_fabrics() }},
+               { ids => $current_config->list_fabrics() },
+               'fabrics'
+           );
+
+           $digest = $current_config->digest();
+           $fabrics = $pending_fabrics->{ids};
+       } elsif ($running) {
+           $fabrics = PVE::Network::SDN::Fabrics::config(1)
+               ->list_fabrics();
+       } else {
+           my $current_config = PVE::Network::SDN::Fabrics::config();
+
+           $digest = $current_config->{digest};
+           $fabrics = $current_config->list_fabrics();
+       }
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+       my $privs = ['SDN.Audit', 'SDN.Allocate'];
+
+       my @res;
+       for my $id (keys %$fabrics) {
+           next if !$rpcenv->check_any($authuser, "/sdn/fabrics/$id", $privs, 
1);
+           $fabrics->{$id}->{digest} = $digest if $digest;
+           push @res, $fabrics->{$id};
+       }
+
+       return \@res;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'get_fabric',
+    path => '{id}',
+    method => 'GET',
+    description => 'Update a fabric',
+    permissions => {
+       check => ['perm', '/sdn/fabrics/{id}', [ 'SDN.Audit', 'SDN.Allocate' ], 
any => 1],
+    },
+    parameters => {
+       properties => {
+           id => get_standard_option('pve-sdn-fabric-id'),
+       },
+    },
+    returns => {
+       type => 'object',
+       properties => PVE::Network::SDN::Fabrics::fabric_properties(0),
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $id = extract_param($param, 'id');
+
+       my $config = PVE::Network::SDN::Fabrics::config();
+
+       my $fabric = $config->get_fabric($id);
+       $fabric->{digest} = $config->digest();
+
+       return $fabric;
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'add_fabric',
+    path => '',
+    method => 'POST',
+    description => 'Add a fabric',
+    protected => 1,
+    permissions => {
+       check => ['perm', '/sdn/fabrics', [ 'SDN.Allocate' ]],
+    },
+    parameters => {
+       properties => PVE::Network::SDN::Fabrics::fabric_properties(0),
+    },
+    returns => {
+       type => 'null',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       PVE::Network::SDN::lock_sdn_config(sub {
+           my $config = PVE::Network::SDN::Fabrics::config();
+
+           my $digest = extract_param($param, 'digest');
+           PVE::Tools::assert_if_modified($config->digest(), $digest) if 
$digest;
+
+           $config->add_fabric($param);
+           PVE::Network::SDN::Fabrics::write_config($config);
+       }, "adding fabric failed");
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'update_fabric',
+    path => '{id}',
+    method => 'PUT',
+    description => 'Update a fabric',
+    protected => 1,
+    permissions => {
+       check => ['perm', '/sdn/fabrics/{id}', [ 'SDN.Allocate' ]],
+    },
+    parameters => {
+       properties => PVE::Network::SDN::Fabrics::fabric_properties(1),
+    },
+    returns => {
+       type => 'null',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       PVE::Network::SDN::lock_sdn_config(sub {
+           my $id = extract_param($param, 'id');
+
+           my $config = PVE::Network::SDN::Fabrics::config();
+
+           my $digest = extract_param($param, 'digest');
+           PVE::Tools::assert_if_modified($config->digest(), $digest) if 
$digest;
+
+           $config->update_fabric($id, $param);
+           PVE::Network::SDN::Fabrics::write_config($config);
+       }, "updating fabric failed");
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'delete_fabric',
+    path => '{id}',
+    method => 'DELETE',
+    description => 'Add a fabric',
+    protected => 1,
+    permissions => {
+       check => ['perm', '/sdn/fabrics/{id}', [ 'SDN.Allocate' ]],
+    },
+    parameters => {
+       properties => {
+           id => get_standard_option('pve-sdn-fabric-id'),
+       },
+    },
+    returns => {
+       type => 'null',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       PVE::Network::SDN::lock_sdn_config(sub {
+           my $id = extract_param($param, 'id');
+
+           my $rpcenv = PVE::RPCEnvironment::get();
+           my $authuser = $rpcenv->get_user();
+
+           my $config = PVE::Network::SDN::Fabrics::config();
+
+           my $nodes = $config->list_nodes_fabric($id);
+
+           for my $node_id (keys %$nodes) {
+               if (!$rpcenv->check_any($authuser, "/nodes/$node_id", 
['Sys.Modify'], 1)) {
+                   die "permission check failed: missing 'Sys.Modify' on node 
$node_id";
+               }
+           }
+
+           my $digest = extract_param($param, 'digest');
+           PVE::Tools::assert_if_modified($config->digest(), $digest) if 
$digest;
+
+           $config->delete_fabric($id);
+           PVE::Network::SDN::Fabrics::write_config($config);
+       }, "deleting fabric failed");
+    },
+});
+
+1;
diff --git a/src/PVE/API2/Network/SDN/Fabrics/Makefile 
b/src/PVE/API2/Network/SDN/Fabrics/Makefile
new file mode 100644
index 0000000..bd644f7
--- /dev/null
+++ b/src/PVE/API2/Network/SDN/Fabrics/Makefile
@@ -0,0 +1,8 @@
+SOURCES=Fabric.pm
+
+
+PERL5DIR=${DESTDIR}/usr/share/perl5
+
+.PHONY: install
+install:
+       for i in ${SOURCES}; do install -D -m 0644 $$i 
${PERL5DIR}/PVE/API2/Network/SDN/Fabrics/$$i; done
-- 
2.39.5


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to