]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Plugin.pm
api: add missing additional properties=0 on network reload/revert
[pve-network.git] / PVE / Network / SDN / Plugin.pm
CommitLineData
86d22462 1package PVE::Network::SDN::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
fe61b14c
AD
21PVE::JSONSchema::register_standard_option('pve-network-id', {
22 description => "The Network object identifier.",
23 type => 'string', format => 'pve-network-id',
24});
25
26PVE::JSONSchema::register_format('pve-network-id', \&parse_network_id);
27sub parse_network_id {
28 my ($networkid, $noerr) = @_;
29
30 if ($networkid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
31 return undef if $noerr;
32 die "network object ID '$networkid' contains illegal characters\n";
33 }
34 return $networkid;
35}
36
6939693f
AD
37my $defaultData = {
38
39 propertyList => {
40 type => {
41 description => "Plugin type.",
42 type => 'string', format => 'pve-configid',
43 type => 'string',
44 },
6bad73d0 45 network => get_standard_option('pve-network-id',
86d22462 46 { completion => \&PVE::Network::SDN::complete_network }),
6939693f
AD
47 },
48};
49
50sub private {
51 return $defaultData;
52}
53
54sub parse_section_header {
55 my ($class, $line) = @_;
56
57 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
6bad73d0 58 my ($type, $networkid) = (lc($1), $2);
6939693f
AD
59 my $errmsg = undef; # set if you want to skip whole section
60 eval { PVE::JSONSchema::pve_verify_configid($type); };
61 $errmsg = $@ if $@;
62 my $config = {}; # to return additional attributes
6bad73d0 63 return ($type, $networkid, $errmsg, $config);
6939693f
AD
64 }
65 return undef;
66}
67
68sub generate_network_config {
69 my ($class, $plugin_config, $node, $data, $ctime) = @_;
70
71 die "please implement inside plugin";
72}
73
fe0c6b9e
AD
74sub on_delete_hook {
75 my ($class, $networkid, $scfg) = @_;
e8d5906e
AD
76
77 # do nothing by default
78}
79
80sub on_update_hook {
81 my ($class, $networkid, $scfg) = @_;
fe0c6b9e
AD
82
83 # do nothing by default
84}
85
6939693f
AD
86#helpers
87sub parse_tag_number_or_range {
88 my ($str, $max, $tag) = @_;
89
90 my @elements = split(/,/, $str);
91 my $count = 0;
92 my $allowed = undef;
93
94 die "extraneous commas in list\n" if $str ne join(',', @elements);
95 foreach my $item (@elements) {
96 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
97 $count += 2;
98 my ($port1, $port2) = ($1, $2);
99 die "invalid port '$port1'\n" if $port1 > $max;
100 die "invalid port '$port2'\n" if $port2 > $max;
101 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
102
103 if ($tag && $tag >= $port1 && $tag <= $port2){
104 $allowed = 1;
105 last;
106 }
107
108 } elsif ($item =~ m/^([0-9]+)$/) {
109 $count += 1;
110 my $port = $1;
111 die "invalid port '$port'\n" if $port > $max;
112
113 if ($tag && $tag == $port){
114 $allowed = 1;
115 last;
116 }
117 }
118 }
119 die "tag $tag is not allowed" if $tag && !$allowed;
120
121 return (scalar(@elements) > 1);
122}
123
1241;