[pve-devel] [PATCH pve-network v2 12/19] api: fabrics: add common helpers
Gabriel Goller
g.goller at proxmox.com
Fri Apr 4 18:28:48 CEST 2025
From: Stefan Hanreich <s.hanreich at 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 at proxmox.com>
Co-authored-by: Gabriel Goller <g.goller at proxmox.com>
Signed-off-by: Gabriel Goller <g.goller at 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
More information about the pve-devel
mailing list