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