From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 9B4281FF15C
	for <inbox@lore.proxmox.com>; Fri,  4 Apr 2025 18:31:20 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 7A31E37B88;
	Fri,  4 Apr 2025 18:29:51 +0200 (CEST)
From: Gabriel Goller <g.goller@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Fri,  4 Apr 2025 18:28:48 +0200
Message-Id: <20250404162908.563060-38-g.goller@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250404162908.563060-1-g.goller@proxmox.com>
References: <20250404162908.563060-1-g.goller@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.023 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
 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 v2 12/19] api: fabrics: add common
 helpers
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

From: Stefan Hanreich <s.hanreich@proxmox.com>

Since the perlmod API for both the openfabric and ospf are the same,
add helpers for all CRUD operations that will be supported by the
openfabric and ospf endpoints, so they can share the same code.

Additionally we define some standard options for common data types
for fabrics, that will be used by the upcoming fabric API modules.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
Co-authored-by: Gabriel Goller <g.goller@proxmox.com>
Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---
 src/PVE/API2/Network/SDN/Fabrics/Common.pm | 126 +++++++++++++++++++++
 src/PVE/API2/Network/SDN/Fabrics/Makefile  |   9 ++
 2 files changed, 135 insertions(+)
 create mode 100644 src/PVE/API2/Network/SDN/Fabrics/Common.pm
 create mode 100644 src/PVE/API2/Network/SDN/Fabrics/Makefile

diff --git a/src/PVE/API2/Network/SDN/Fabrics/Common.pm b/src/PVE/API2/Network/SDN/Fabrics/Common.pm
new file mode 100644
index 000000000000..ddd57cafd4ce
--- /dev/null
+++ b/src/PVE/API2/Network/SDN/Fabrics/Common.pm
@@ -0,0 +1,126 @@
+package PVE::API2::Network::SDN::Fabrics::Common;
+
+use strict;
+use warnings;
+
+use PVE::JSONSchema;
+use PVE::Tools qw(extract_param);
+
+use PVE::Network::SDN::Fabrics;
+
+PVE::JSONSchema::register_format('pve-sdn-fabric-id', sub {
+    my ($id, $noerr) = @_;
+
+    if ($id !~ m/^[a-z][a-z0-9]{1,7}$/i) {
+	return undef if $noerr;
+	die "zone ID '$id' contains illegal characters\n";
+    }
+
+    return $id;
+});
+
+PVE::JSONSchema::register_standard_option('pve-sdn-fabric-id', {
+    description => "Identifier for SDN fabrics",
+    type => 'string',
+    format => 'pve-sdn-fabric-id',
+});
+
+PVE::JSONSchema::register_standard_option('pve-sdn-fabric-node-id', {
+    description => "Identifier for nodes in an SDN fabric",
+    type => 'string',
+    format => 'pve-node',
+});
+
+PVE::JSONSchema::register_standard_option('pve-sdn-fabric-section-type', {
+    description => "Type of configuration entry in an SDN Fabric section config",
+    type => 'string',
+    enum => ['fabric', 'node'],
+});
+
+PVE::JSONSchema::register_standard_option('pve-sdn-fabric-loopback-prefix', {
+    type => 'string',
+    format => 'CIDRv4',
+    description => 'The IP prefix for Loopback IPs',
+});
+
+sub get_fabric {
+    my ($type, $param) = @_;
+
+    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
+
+    my $config = $fabrics->get_fabric($param->{fabric_id});
+    $config->{digest} = Digest::SHA::sha1_hex($config);
+
+    return $config;
+}
+
+sub get_node {
+    my ($type, $param) = @_;
+
+    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
+
+    my $config = $fabrics->get_node($param->{fabric_id}, $param->{node_id});
+    $config->{digest} = Digest::SHA::sha1_hex($config);
+
+    return $config;
+}
+
+sub add_node {
+    my ($type, $param) = @_;
+
+    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
+    $fabrics->add_node($param);
+    PVE::Network::SDN::Fabrics::write_config($fabrics);
+}
+
+sub add_fabric {
+    my ($type, $param) = @_;
+
+    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
+    $fabrics->add_fabric($param);
+    PVE::Network::SDN::Fabrics::write_config($fabrics);
+}
+
+sub edit_fabric {
+    my ($type, $param) = @_;
+
+    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
+    my $config = $fabrics->get_fabric($param->{fabric_id});
+
+    my $digest = extract_param($param, 'digest');
+    PVE::Tools::assert_if_modified($param->{digest}, $digest);
+
+    $fabrics->edit_fabric($param);
+    PVE::Network::SDN::Fabrics::write_config($fabrics);
+}
+
+sub edit_node {
+    my ($type, $param) = @_;
+
+    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
+    my $config = $fabrics->get_node($param->{fabric_id}, $param->{node_id});
+
+    my $digest = extract_param($param, 'digest');
+    PVE::Tools::assert_if_modified($param->{digest}, $digest);
+
+    $fabrics->edit_node($param);
+    PVE::Network::SDN::Fabrics::write_config($fabrics);
+}
+
+sub delete_fabric {
+    my ($type, $param) = @_;
+
+    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
+    $fabrics->delete_fabric($param);
+    PVE::Network::SDN::Fabrics::write_config($fabrics);
+}
+
+sub delete_node {
+    my ($type, $param) = @_;
+
+    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
+    $fabrics->delete_node($param);
+    PVE::Network::SDN::Fabrics::write_config($fabrics);
+}
+
+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..d4267b63997c
--- /dev/null
+++ b/src/PVE/API2/Network/SDN/Fabrics/Makefile
@@ -0,0 +1,9 @@
+SOURCES=Common.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