]> git.proxmox.com Git - pve-firewall.git/blame - src/PVE/API2/Firewall/Cluster.pm
start API for aliases
[pve-firewall.git] / src / PVE / API2 / Firewall / Cluster.pm
CommitLineData
b4366f00
DM
1package PVE::API2::Firewall::Cluster;
2
3use strict;
4use warnings;
1df4ba7e 5use PVE::Exception qw(raise raise_param_exc raise_perm_exc);
b4366f00
DM
6use PVE::JSONSchema qw(get_standard_option);
7
8use PVE::Firewall;
81d574a7 9use PVE::API2::Firewall::Aliases;
86791289 10use PVE::API2::Firewall::Rules;
b4366f00 11use PVE::API2::Firewall::Groups;
009ee3ac 12use PVE::API2::Firewall::IPSet;
b4366f00 13
1df4ba7e
DM
14#fixme: locking?
15
b4366f00
DM
16use Data::Dumper; # fixme: remove
17
18use base qw(PVE::RESTHandler);
19
20__PACKAGE__->register_method ({
21 subclass => "PVE::API2::Firewall::Groups",
22 path => 'groups',
23});
24
86791289
DM
25__PACKAGE__->register_method ({
26 subclass => "PVE::API2::Firewall::ClusterRules",
27 path => 'rules',
28});
29
c85c87f9
DM
30__PACKAGE__->register_method ({
31 subclass => "PVE::API2::Firewall::ClusterIPSetList",
32 path => 'ipset',
33});
34
81d574a7
DM
35__PACKAGE__->register_method ({
36 subclass => "PVE::API2::Firewall::ClusterAliases",
37 path => 'aliases',
38});
39
40
b4366f00
DM
41__PACKAGE__->register_method({
42 name => 'index',
43 path => '',
44 method => 'GET',
45 permissions => { user => 'all' },
46 description => "Directory index.",
47 parameters => {
48 additionalProperties => 0,
49 },
50 returns => {
51 type => 'array',
52 items => {
53 type => "object",
54 properties => {},
55 },
56 links => [ { rel => 'child', href => "{name}" } ],
57 },
58 code => sub {
59 my ($param) = @_;
60
61 my $result = [
81d574a7 62 { name => 'aliases' },
b4366f00
DM
63 { name => 'rules' },
64 { name => 'options' },
65 { name => 'groups' },
9d6f90e6 66 { name => 'ipset' },
ebd54ae9 67 { name => 'macros' },
b4366f00
DM
68 ];
69
70 return $result;
71 }});
1df4ba7e 72
271f287b
DM
73my $option_properties = {
74 enable => {
75 type => 'boolean',
76 optional => 1,
77 },
78 policy_in => {
79 description => "Input policy.",
80 type => 'string',
81 optional => 1,
82 enum => ['ACCEPT', 'REJECT', 'DROP'],
83 },
84 policy_out => {
85 description => "Output policy.",
86 type => 'string',
87 optional => 1,
88 enum => ['ACCEPT', 'REJECT', 'DROP'],
89 },
90};
91
92my $add_option_properties = sub {
93 my ($properties) = @_;
94
95 foreach my $k (keys %$option_properties) {
96 $properties->{$k} = $option_properties->{$k};
97 }
98
99 return $properties;
100};
101
102
1df4ba7e
DM
103__PACKAGE__->register_method({
104 name => 'get_options',
105 path => 'options',
106 method => 'GET',
107 description => "Get Firewall options.",
108 parameters => {
109 additionalProperties => 0,
110 },
111 returns => {
112 type => "object",
113 #additionalProperties => 1,
271f287b 114 properties => $option_properties,
1df4ba7e
DM
115 },
116 code => sub {
117 my ($param) = @_;
118
119 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
120
5d38d64f 121 return PVE::Firewall::copy_opject_with_digest($cluster_conf->{options});
1df4ba7e
DM
122 }});
123
1df4ba7e
DM
124
125__PACKAGE__->register_method({
126 name => 'set_options',
127 path => 'options',
128 method => 'PUT',
129 description => "Set Firewall options.",
68c90e21 130 protected => 1,
1df4ba7e
DM
131 parameters => {
132 additionalProperties => 0,
133 properties => &$add_option_properties({
134 delete => {
135 type => 'string', format => 'pve-configid-list',
136 description => "A list of settings you want to delete.",
137 optional => 1,
138 },
5d38d64f 139 digest => get_standard_option('pve-config-digest'),
1df4ba7e
DM
140 }),
141 },
142 returns => { type => "null" },
143 code => sub {
144 my ($param) = @_;
145
146 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
147
5d38d64f
DM
148 my (undef, $digest) = PVE::Firewall::copy_opject_with_digest($cluster_conf->{options});
149 PVE::Tools::assert_if_modified($digest, $param->{digest});
150
1df4ba7e
DM
151 if ($param->{delete}) {
152 foreach my $opt (PVE::Tools::split_list($param->{delete})) {
153 raise_param_exc({ delete => "no such option '$opt'" })
154 if !$option_properties->{$opt};
155 delete $cluster_conf->{options}->{$opt};
156 }
157 }
158
159 if (defined($param->{enable})) {
271f287b
DM
160 $param->{enable} = $param->{enable} ? 1 : 0;
161 }
162
163 foreach my $k (keys %$option_properties) {
164 next if !defined($param->{$k});
165 $cluster_conf->{options}->{$k} = $param->{$k};
1df4ba7e
DM
166 }
167
1df4ba7e
DM
168 PVE::Firewall::save_clusterfw_conf($cluster_conf);
169
170 return undef;
171 }});
ebd54ae9
DM
172
173__PACKAGE__->register_method({
174 name => 'get_macros',
175 path => 'macros',
176 method => 'GET',
177 description => "List available macros",
178 parameters => {
179 additionalProperties => 0,
180 },
181 returns => {
182 type => 'array',
183 items => {
184 type => "object",
185 properties => {
186 macro => {
187 description => "Macro name.",
188 type => 'string',
189 },
190 descr => {
191 description => "More verbose description (if available).",
192 type => 'string',
193 }
194 },
195 },
196 },
197 code => sub {
198 my ($param) = @_;
199
200 my $res = [];
201
202 my ($macros, $descr) = PVE::Firewall::get_macros();
203
204 foreach my $macro (keys %$macros) {
205 push @$res, { macro => $macro, descr => $descr->{$macro} || $macro };
206 }
207
208 return $res;
209 }});
210
2111;