[pve-devel] [PATCH pve-network 12/17] api: fabrics: add common helpers

Fabian Grünbichler f.gruenbichler at proxmox.com
Wed Apr 2 12:41:17 CEST 2025


On March 28, 2025 6:13 pm, Gabriel Goller wrote:
> 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.
> 
> 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 | 80 ++++++++++++++++++++++
>  src/PVE/API2/Network/SDN/Fabrics/Makefile  |  9 +++
>  2 files changed, 89 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..9d68264f6252
> --- /dev/null
> +++ b/src/PVE/API2/Network/SDN/Fabrics/Common.pm
> @@ -0,0 +1,80 @@
> +package PVE::API2::Network::SDN::Fabrics::Common;
> +
> +use strict;
> +use warnings;
> +
> +use PVE::Network::SDN::Fabrics;

weird order: delete, add, get, edit ;)

> +
> +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);

write_config is only called here in this module.. couldn't we just adapt
it to return undef instead of repeating it at every call site here..

and/or we could have a single do_action helper since we effectively
repeat the same code 6 times here..

and since these are basically the API handlers, we could add the missing
bits here and than just call them to handle the API request?

> +    return undef;
> +}
> +
> +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);
> +    return undef;
> +}
> +
> +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);
> +
> +    return undef;
> +}
> +
> +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);
> +
> +    return undef;
> +}
> +
> +sub get_fabric {
> +    my ($type, $param) = @_;
> +
> +    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
> +    my $return_value = $fabrics->get_fabric($param->{fabric});
> +    return $return_value;

this could just be

my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
return $fabrics->get_fabric($param->{fabric});

?

> +}
> +
> +sub get_node {
> +    my ($type, $param) = @_;
> +
> +    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
> +    my $return_value = $fabrics->get_node($param->{fabric}, $param->{node});
> +    return $return_value;

same here

> +}
> +
> +sub edit_fabric {
> +    my ($type, $param) = @_;
> +
> +    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
> +    $fabrics->edit_fabric($param);
> +    PVE::Network::SDN::Fabrics::write_config($fabrics);
> +    return undef;
> +}
> +
> +sub edit_node {
> +    my ($type, $param) = @_;
> +
> +    my $fabrics = PVE::Network::SDN::Fabrics::config_for_protocol($type);
> +    $fabrics->edit_node($param);
> +    PVE::Network::SDN::Fabrics::write_config($fabrics);
> +    return undef;
> +}
> +
> +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..e433f2e7d0a6
> --- /dev/null
> +++ b/src/PVE/API2/Network/SDN/Fabrics/Makefile
> @@ -0,0 +1,9 @@
> +SOURCES=OpenFabric.pm Ospf.pm 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 at lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 
> 
> 




More information about the pve-devel mailing list