]> git.proxmox.com Git - pve-firewall.git/blob - src/PVE/API2/Firewall/Groups.pm
support comments on group sections
[pve-firewall.git] / src / PVE / API2 / Firewall / Groups.pm
1 package PVE::API2::Firewall::Groups;
2
3 use strict;
4 use warnings;
5 use PVE::JSONSchema qw(get_standard_option);
6 use PVE::Exception qw(raise raise_param_exc);
7
8 use PVE::Firewall;
9 use PVE::API2::Firewall::Rules;
10
11 use Data::Dumper; # fixme: remove
12
13 use base qw(PVE::RESTHandler);
14
15 __PACKAGE__->register_method({
16 name => 'list_security_groups',
17 path => '',
18 method => 'GET',
19 description => "List security groups.",
20 parameters => {
21 additionalProperties => 0,
22 },
23 returns => {
24 type => 'array',
25 items => {
26 type => "object",
27 properties => {
28 name => get_standard_option('pve-security-group-name'),
29 comment => {
30 type => 'string',
31 optional => 1,
32 }
33 },
34 },
35 links => [ { rel => 'child', href => "{name}" } ],
36 },
37 code => sub {
38 my ($param) = @_;
39
40 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
41
42 my $res = [];
43 foreach my $group (keys %{$cluster_conf->{groups}}) {
44 my $data = {
45 name => $group,
46 count => scalar(@{$cluster_conf->{groups}->{$group}})
47 };
48 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
49 $data->{comment} = $comment;
50 }
51 push @$res, $data;
52 }
53
54 return $res;
55 }});
56
57 __PACKAGE__->register_method({
58 name => 'create_security_group',
59 path => '',
60 method => 'POST',
61 description => "Create new security group.",
62 protected => 1,
63 parameters => {
64 additionalProperties => 0,
65 properties => {
66 name => get_standard_option('pve-security-group-name'),
67 comment => {
68 type => 'string',
69 optional => 1,
70 },
71 rename => get_standard_option('pve-security-group-name', {
72 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.",
73 optional => 1,
74 }),
75 },
76 },
77 returns => { type => 'null' },
78 code => sub {
79 my ($param) = @_;
80
81 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
82
83 foreach my $name (keys %{$cluster_conf->{groups}}) {
84 raise_param_exc({ name => "Security group '$name' already exists" })
85 if !$param->{rename} && $name eq $param->{name};
86 }
87
88 if ($param->{rename}) {
89 raise_param_exc({ name => "Security group '$param->{rename}' does not exists" })
90 if !$cluster_conf->{groups}->{$param->{rename}};
91 my $data = delete $cluster_conf->{groups}->{$param->{rename}};
92 $cluster_conf->{groups}->{$param->{name}} = $data;
93 if (my $comment = delete $cluster_conf->{group_comments}->{$param->{rename}}) {
94 $cluster_conf->{group_comments}->{$param->{name}} = $comment;
95 }
96 $cluster_conf->{group_comments}->{$param->{name}} = $param->{comment} if defined($param->{comment});
97 } else {
98 $cluster_conf->{groups}->{$param->{name}} = [];
99 $cluster_conf->{group_comments}->{$param->{name}} = $param->{comment} if defined($param->{comment});
100 }
101
102 PVE::Firewall::save_clusterfw_conf($cluster_conf);
103
104 return undef;
105 }});
106
107 __PACKAGE__->register_method({
108 name => 'delete_security_group',
109 path => '{name}',
110 method => 'DELETE',
111 description => "Delete security group.",
112 protected => 1,
113 parameters => {
114 additionalProperties => 0,
115 properties => {
116 name => get_standard_option('pve-security-group-name'),
117 }
118 },
119 returns => { type => 'null' },
120 code => sub {
121 my ($param) = @_;
122
123 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
124
125 return undef if !$cluster_conf->{groups}->{$param->{name}};
126
127 die "Security group '$param->{name}' is not empty\n"
128 if scalar(@{$cluster_conf->{groups}->{$param->{name}}});
129
130 delete $cluster_conf->{groups}->{$param->{name}};
131
132 PVE::Firewall::save_clusterfw_conf($cluster_conf);
133
134 return undef;
135 }});
136
137 __PACKAGE__->register_method ({
138 subclass => "PVE::API2::Firewall::GroupRules",
139 path => '{group}',
140 });
141
142 1;