]>
Commit | Line | Data |
---|---|---|
8f119284 DM |
1 | package PVE::API2::Firewall::Groups; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use PVE::JSONSchema qw(get_standard_option); | |
0d22acb3 | 6 | use PVE::Exception qw(raise raise_param_exc); |
8f119284 DM |
7 | |
8 | use PVE::Firewall; | |
86791289 | 9 | use PVE::API2::Firewall::Rules; |
8f119284 DM |
10 | |
11 | use Data::Dumper; # fixme: remove | |
12 | ||
13 | use base qw(PVE::RESTHandler); | |
14 | ||
5d38d64f DM |
15 | my $get_security_group_list = sub { |
16 | my ($cluster_conf) = @_; | |
17 | ||
18 | my $res = []; | |
f76f3cae | 19 | foreach my $group (sort keys %{$cluster_conf->{groups}}) { |
5d38d64f | 20 | my $data = { |
9e553e57 | 21 | group => $group, |
5d38d64f DM |
22 | }; |
23 | if (my $comment = $cluster_conf->{group_comments}->{$group}) { | |
24 | $data->{comment} = $comment; | |
25 | } | |
26 | push @$res, $data; | |
27 | } | |
28 | ||
29 | my ($list, $digest) = PVE::Firewall::copy_list_with_digest($res); | |
30 | ||
31 | return wantarray ? ($list, $digest) : $list; | |
32 | }; | |
33 | ||
8f119284 | 34 | __PACKAGE__->register_method({ |
9567aa91 | 35 | name => 'list_security_groups', |
8f119284 DM |
36 | path => '', |
37 | method => 'GET', | |
38 | description => "List security groups.", | |
5c9da37b | 39 | permissions => { user => 'all' }, |
8f119284 DM |
40 | parameters => { |
41 | additionalProperties => 0, | |
e2beb7aa | 42 | properties => {}, |
8f119284 DM |
43 | }, |
44 | returns => { | |
45 | type => 'array', | |
46 | items => { | |
47 | type => "object", | |
d1c53b3e | 48 | properties => { |
9e553e57 | 49 | group => get_standard_option('pve-security-group-name'), |
2ba5e893 | 50 | digest => get_standard_option('pve-config-digest', { optional => 0} ), |
0d22acb3 DM |
51 | comment => { |
52 | type => 'string', | |
53 | optional => 1, | |
54 | } | |
d1c53b3e | 55 | }, |
8f119284 | 56 | }, |
9e553e57 | 57 | links => [ { rel => 'child', href => "{group}" } ], |
8f119284 DM |
58 | }, |
59 | code => sub { | |
60 | my ($param) = @_; | |
61 | ||
fca39c2c | 62 | my $cluster_conf = PVE::Firewall::load_clusterfw_conf(); |
8f119284 | 63 | |
5d38d64f | 64 | return &$get_security_group_list($cluster_conf); |
d1c53b3e DM |
65 | }}); |
66 | ||
9567aa91 DM |
67 | __PACKAGE__->register_method({ |
68 | name => 'create_security_group', | |
69 | path => '', | |
70 | method => 'POST', | |
71 | description => "Create new security group.", | |
72 | protected => 1, | |
5c9da37b DM |
73 | permissions => { |
74 | check => ['perm', '/', [ 'Sys.Modify' ]], | |
75 | }, | |
9567aa91 DM |
76 | parameters => { |
77 | additionalProperties => 0, | |
78 | properties => { | |
9e553e57 | 79 | group => get_standard_option('pve-security-group-name'), |
0d22acb3 DM |
80 | comment => { |
81 | type => 'string', | |
82 | optional => 1, | |
83 | }, | |
9567aa91 | 84 | rename => get_standard_option('pve-security-group-name', { |
0d22acb3 | 85 | description => "Rename/update an existing security group. You can set 'rename' to the same value as 'name' to update the 'comment' of an existing group.", |
9567aa91 DM |
86 | optional => 1, |
87 | }), | |
2ba5e893 | 88 | digest => get_standard_option('pve-config-digest'), |
9567aa91 DM |
89 | }, |
90 | }, | |
91 | returns => { type => 'null' }, | |
92 | code => sub { | |
93 | my ($param) = @_; | |
94 | ||
95 | my $cluster_conf = PVE::Firewall::load_clusterfw_conf(); | |
96 | ||
9567aa91 | 97 | if ($param->{rename}) { |
5d38d64f DM |
98 | my (undef, $digest) = &$get_security_group_list($cluster_conf); |
99 | PVE::Tools::assert_if_modified($digest, $param->{digest}); | |
100 | ||
9e553e57 | 101 | raise_param_exc({ group => "Security group '$param->{rename}' does not exists" }) |
9567aa91 | 102 | if !$cluster_conf->{groups}->{$param->{rename}}; |
5d38d64f | 103 | |
9567aa91 | 104 | my $data = delete $cluster_conf->{groups}->{$param->{rename}}; |
9e553e57 | 105 | $cluster_conf->{groups}->{$param->{group}} = $data; |
0d22acb3 | 106 | if (my $comment = delete $cluster_conf->{group_comments}->{$param->{rename}}) { |
9e553e57 | 107 | $cluster_conf->{group_comments}->{$param->{group}} = $comment; |
0d22acb3 | 108 | } |
9e553e57 | 109 | $cluster_conf->{group_comments}->{$param->{group}} = $param->{comment} if defined($param->{comment}); |
9567aa91 | 110 | } else { |
5d38d64f | 111 | foreach my $name (keys %{$cluster_conf->{groups}}) { |
9e553e57 DM |
112 | raise_param_exc({ group => "Security group '$name' already exists" }) |
113 | if $name eq $param->{group}; | |
5d38d64f DM |
114 | } |
115 | ||
9e553e57 DM |
116 | $cluster_conf->{groups}->{$param->{group}} = []; |
117 | $cluster_conf->{group_comments}->{$param->{group}} = $param->{comment} if defined($param->{comment}); | |
9567aa91 DM |
118 | } |
119 | ||
120 | PVE::Firewall::save_clusterfw_conf($cluster_conf); | |
121 | ||
122 | return undef; | |
123 | }}); | |
124 | ||
86791289 DM |
125 | __PACKAGE__->register_method ({ |
126 | subclass => "PVE::API2::Firewall::GroupRules", | |
d1c53b3e | 127 | path => '{group}', |
86791289 | 128 | }); |
9c7e0858 | 129 | |
8f119284 | 130 | 1; |