From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 568E21FF183 for ; Wed, 2 Jul 2025 16:53:39 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 5BA421ECE3; Wed, 2 Jul 2025 16:51:49 +0200 (CEST) From: Gabriel Goller To: pve-devel@lists.proxmox.com Date: Wed, 2 Jul 2025 16:50:35 +0200 Message-Id: <20250702145101.894299-51-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250702145101.894299-1-g.goller@proxmox.com> References: <20250702145101.894299-1-g.goller@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -1.017 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_MAILER 2 Automated Mailer Tag Left in Email SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH pve-network v4 15/21] api: fabrics: add fabric submodule X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" From: Stefan Hanreich 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 Signed-off-by: Stefan Hanreich --- src/PVE/API2/Network/SDN/Fabrics.pm | 9 +- src/PVE/API2/Network/SDN/Fabrics/Fabric.pm | 239 +++++++++++++++++++++ src/PVE/API2/Network/SDN/Fabrics/Makefile | 8 + 3 files changed, 255 insertions(+), 1 deletion(-) 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 a4a972d65cc2..18e51a9d471e 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,7 +43,7 @@ __PACKAGE__->register_method({ my ($param) = @_; my $res = [ - { subdir => 'all' }, + { subdir => 'fabric' }, { subdir => 'all' }, ]; return $res; 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 000000000000..aa546bcf2cfc --- /dev/null +++ b/src/PVE/API2/Network/SDN/Fabrics/Fabric.pm @@ -0,0 +1,239 @@ +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/'", + 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 000000000000..bd644f76888e --- /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