]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/Network/Plugin.pm
write configuration to networks.cfg.new
[pve-network.git] / PVE / Network / Network / Plugin.pm
CommitLineData
6bad73d0 1package PVE::Network::Network::Plugin;
6939693f
AD
2
3use strict;
4use warnings;
5
6use PVE::Tools;
7use PVE::JSONSchema;
8use PVE::Cluster;
9
10use Data::Dumper;
eec580bf 11use PVE::JSONSchema qw(get_standard_option);
6939693f
AD
12use base qw(PVE::SectionConfig);
13
6bad73d0 14PVE::Cluster::cfs_register_file('networks.cfg',
39d04c82
AD
15 sub { __PACKAGE__->parse_config(@_); });
16
17PVE::Cluster::cfs_register_file('networks.cfg.new',
6939693f
AD
18 sub { __PACKAGE__->parse_config(@_); },
19 sub { __PACKAGE__->write_config(@_); });
20
21my $defaultData = {
22
23 propertyList => {
24 type => {
25 description => "Plugin type.",
26 type => 'string', format => 'pve-configid',
27 type => 'string',
28 },
6bad73d0
AD
29 network => get_standard_option('pve-network-id',
30 { completion => \&PVE::Network::Network::complete_network }),
6939693f
AD
31 },
32};
33
34sub private {
35 return $defaultData;
36}
37
38sub parse_section_header {
39 my ($class, $line) = @_;
40
41 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
6bad73d0 42 my ($type, $networkid) = (lc($1), $2);
6939693f
AD
43 my $errmsg = undef; # set if you want to skip whole section
44 eval { PVE::JSONSchema::pve_verify_configid($type); };
45 $errmsg = $@ if $@;
46 my $config = {}; # to return additional attributes
6bad73d0 47 return ($type, $networkid, $errmsg, $config);
6939693f
AD
48 }
49 return undef;
50}
51
52sub generate_network_config {
53 my ($class, $plugin_config, $node, $data, $ctime) = @_;
54
55 die "please implement inside plugin";
56}
57
fe0c6b9e
AD
58sub on_delete_hook {
59 my ($class, $networkid, $scfg) = @_;
e8d5906e
AD
60
61 # do nothing by default
62}
63
64sub on_update_hook {
65 my ($class, $networkid, $scfg) = @_;
fe0c6b9e
AD
66
67 # do nothing by default
68}
69
6939693f
AD
70#helpers
71sub parse_tag_number_or_range {
72 my ($str, $max, $tag) = @_;
73
74 my @elements = split(/,/, $str);
75 my $count = 0;
76 my $allowed = undef;
77
78 die "extraneous commas in list\n" if $str ne join(',', @elements);
79 foreach my $item (@elements) {
80 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
81 $count += 2;
82 my ($port1, $port2) = ($1, $2);
83 die "invalid port '$port1'\n" if $port1 > $max;
84 die "invalid port '$port2'\n" if $port2 > $max;
85 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
86
87 if ($tag && $tag >= $port1 && $tag <= $port2){
88 $allowed = 1;
89 last;
90 }
91
92 } elsif ($item =~ m/^([0-9]+)$/) {
93 $count += 1;
94 my $port = $1;
95 die "invalid port '$port'\n" if $port > $max;
96
97 if ($tag && $tag == $port){
98 $allowed = 1;
99 last;
100 }
101 }
102 }
103 die "tag $tag is not allowed" if $tag && !$allowed;
104
105 return (scalar(@elements) > 1);
106}
107
1081;