]>
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 | ||
15 | __PACKAGE__->register_method({ | |
9567aa91 | 16 | name => 'list_security_groups', |
8f119284 DM |
17 | path => '', |
18 | method => 'GET', | |
19 | description => "List security groups.", | |
8f119284 DM |
20 | parameters => { |
21 | additionalProperties => 0, | |
8f119284 DM |
22 | }, |
23 | returns => { | |
24 | type => 'array', | |
25 | items => { | |
26 | type => "object", | |
d1c53b3e | 27 | properties => { |
387d0ffc | 28 | name => get_standard_option('pve-security-group-name'), |
2ba5e893 | 29 | digest => get_standard_option('pve-config-digest', { optional => 0} ), |
0d22acb3 DM |
30 | comment => { |
31 | type => 'string', | |
32 | optional => 1, | |
33 | } | |
d1c53b3e | 34 | }, |
8f119284 DM |
35 | }, |
36 | links => [ { rel => 'child', href => "{name}" } ], | |
37 | }, | |
38 | code => sub { | |
39 | my ($param) = @_; | |
40 | ||
fca39c2c | 41 | my $cluster_conf = PVE::Firewall::load_clusterfw_conf(); |
8f119284 | 42 | |
2ba5e893 DM |
43 | my $digest = $cluster_conf->{digest}; |
44 | ||
8f119284 | 45 | my $res = []; |
c6f5cc88 | 46 | foreach my $group (keys %{$cluster_conf->{groups}}) { |
0d22acb3 DM |
47 | my $data = { |
48 | name => $group, | |
2ba5e893 | 49 | digest => $digest, |
0d22acb3 DM |
50 | count => scalar(@{$cluster_conf->{groups}->{$group}}) |
51 | }; | |
52 | if (my $comment = $cluster_conf->{group_comments}->{$group}) { | |
53 | $data->{comment} = $comment; | |
54 | } | |
55 | push @$res, $data; | |
d1c53b3e DM |
56 | } |
57 | ||
58 | return $res; | |
59 | }}); | |
60 | ||
9567aa91 DM |
61 | __PACKAGE__->register_method({ |
62 | name => 'create_security_group', | |
63 | path => '', | |
64 | method => 'POST', | |
65 | description => "Create new security group.", | |
66 | protected => 1, | |
67 | parameters => { | |
68 | additionalProperties => 0, | |
69 | properties => { | |
70 | name => get_standard_option('pve-security-group-name'), | |
0d22acb3 DM |
71 | comment => { |
72 | type => 'string', | |
73 | optional => 1, | |
74 | }, | |
9567aa91 | 75 | rename => get_standard_option('pve-security-group-name', { |
0d22acb3 | 76 | 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 |
77 | optional => 1, |
78 | }), | |
2ba5e893 | 79 | digest => get_standard_option('pve-config-digest'), |
9567aa91 DM |
80 | }, |
81 | }, | |
82 | returns => { type => 'null' }, | |
83 | code => sub { | |
84 | my ($param) = @_; | |
85 | ||
86 | my $cluster_conf = PVE::Firewall::load_clusterfw_conf(); | |
87 | ||
2ba5e893 DM |
88 | my $digest = $cluster_conf->{digest}; |
89 | ||
90 | PVE::Tools::assert_if_modified($digest, $param->{digest}); | |
91 | ||
9567aa91 DM |
92 | foreach my $name (keys %{$cluster_conf->{groups}}) { |
93 | raise_param_exc({ name => "Security group '$name' already exists" }) | |
0d22acb3 | 94 | if !$param->{rename} && $name eq $param->{name}; |
9567aa91 DM |
95 | } |
96 | ||
97 | if ($param->{rename}) { | |
98 | raise_param_exc({ name => "Security group '$param->{rename}' does not exists" }) | |
99 | if !$cluster_conf->{groups}->{$param->{rename}}; | |
100 | my $data = delete $cluster_conf->{groups}->{$param->{rename}}; | |
101 | $cluster_conf->{groups}->{$param->{name}} = $data; | |
0d22acb3 DM |
102 | if (my $comment = delete $cluster_conf->{group_comments}->{$param->{rename}}) { |
103 | $cluster_conf->{group_comments}->{$param->{name}} = $comment; | |
104 | } | |
105 | $cluster_conf->{group_comments}->{$param->{name}} = $param->{comment} if defined($param->{comment}); | |
9567aa91 DM |
106 | } else { |
107 | $cluster_conf->{groups}->{$param->{name}} = []; | |
0d22acb3 | 108 | $cluster_conf->{group_comments}->{$param->{name}} = $param->{comment} if defined($param->{comment}); |
9567aa91 DM |
109 | } |
110 | ||
111 | PVE::Firewall::save_clusterfw_conf($cluster_conf); | |
112 | ||
113 | return undef; | |
114 | }}); | |
115 | ||
9567aa91 DM |
116 | __PACKAGE__->register_method({ |
117 | name => 'delete_security_group', | |
118 | path => '{name}', | |
119 | method => 'DELETE', | |
120 | description => "Delete security group.", | |
121 | protected => 1, | |
122 | parameters => { | |
123 | additionalProperties => 0, | |
124 | properties => { | |
125 | name => get_standard_option('pve-security-group-name'), | |
2ba5e893 | 126 | digest => get_standard_option('pve-config-digest'), |
9567aa91 DM |
127 | } |
128 | }, | |
129 | returns => { type => 'null' }, | |
130 | code => sub { | |
131 | my ($param) = @_; | |
132 | ||
133 | my $cluster_conf = PVE::Firewall::load_clusterfw_conf(); | |
134 | ||
2ba5e893 DM |
135 | PVE::Tools::assert_if_modified($cluster_conf->{digest}, $param->{digest}); |
136 | ||
9567aa91 DM |
137 | return undef if !$cluster_conf->{groups}->{$param->{name}}; |
138 | ||
139 | die "Security group '$param->{name}' is not empty\n" | |
140 | if scalar(@{$cluster_conf->{groups}->{$param->{name}}}); | |
141 | ||
142 | delete $cluster_conf->{groups}->{$param->{name}}; | |
143 | ||
144 | PVE::Firewall::save_clusterfw_conf($cluster_conf); | |
145 | ||
146 | return undef; | |
147 | }}); | |
387d0ffc | 148 | |
86791289 DM |
149 | __PACKAGE__->register_method ({ |
150 | subclass => "PVE::API2::Firewall::GroupRules", | |
d1c53b3e | 151 | path => '{group}', |
86791289 | 152 | }); |
9c7e0858 | 153 | |
8f119284 | 154 | 1; |