}});
}
-sub register_add_ip {
+sub register_create_ip {
my ($class) = @_;
my $properties = $class->additional_parameters();
$properties->{comment} = $api_properties->{comment};
$class->register_method({
- name => 'add_ip',
+ name => 'create_ip',
path => '',
method => 'POST',
description => "Add IP or Network to IPSet.",
}});
}
-sub register_remove_ip {
+sub register_read_ip {
+ my ($class) = @_;
+
+ my $properties = $class->additional_parameters();
+
+ $properties->{name} = $api_properties->{name};
+ $properties->{cidr} = $api_properties->{cidr};
+
+ $class->register_method({
+ name => 'read_ip',
+ path => '{cidr}',
+ method => 'GET',
+ description => "Read IP or Network settings from IPSet.",
+ protected => 1,
+ parameters => {
+ additionalProperties => 0,
+ properties => $properties,
+ },
+ returns => { type => "object" },
+ code => sub {
+ my ($param) = @_;
+
+ my ($fw_conf, $ipset) = $class->load_config($param);
+
+ foreach my $entry (@$ipset) {
+ return $entry if $entry->{cidr} eq $param->{cidr};
+ }
+
+ raise_param_exc({ cidr => "no such IP/Network" });
+ }});
+}
+
+sub register_update_ip {
+ my ($class) = @_;
+
+ my $properties = $class->additional_parameters();
+
+ $properties->{name} = $api_properties->{name};
+ $properties->{cidr} = $api_properties->{cidr};
+ $properties->{nomatch} = $api_properties->{nomatch};
+ $properties->{comment} = $api_properties->{comment};
+
+ $class->register_method({
+ name => 'update_ip',
+ path => '{cidr}',
+ method => 'PUT',
+ description => "Update IP or Network settings",
+ protected => 1,
+ parameters => {
+ additionalProperties => 0,
+ properties => $properties,
+ },
+ returns => { type => "null" },
+ code => sub {
+ my ($param) = @_;
+
+ my ($fw_conf, $ipset) = $class->load_config($param);
+
+ foreach my $entry (@$ipset) {
+ if($entry->{cidr} eq $param->{cidr}) {
+ $entry->{nomatch} = $param->{nomatch};
+ $entry->{comment} = $param->{comment};
+ $class->save_ipset($param, $fw_conf, $ipset);
+ return;
+ }
+ }
+
+ raise_param_exc({ cidr => "no such IP/Network" });
+ }});
+}
+
+sub register_delete_ip {
my ($class) = @_;
my $properties = $class->additional_parameters();
my ($class) = @_;
$class->register_get_ipset();
- $class->register_add_ip();
- $class->register_remove_ip();
+ $class->register_create_ip();
+ $class->register_read_ip();
+ $class->register_update_ip();
+ $class->register_delete_ip();
}
package PVE::API2::Firewall::ClusterIPset;
__PACKAGE__->register_handlers();
+package PVE::API2::Firewall::BaseIPSetList;
+
+use strict;
+use warnings;
+use PVE::Firewall;
+use PVE::Exception qw(raise_param_exc);
+
+use base qw(PVE::RESTHandler);
+
+sub register_index {
+ my ($class) = @_;
+
+ $class->register_method({
+ name => 'ipset_index',
+ path => '',
+ method => 'GET',
+ description => "List IPSets",
+ parameters => {
+ additionalProperties => 0,
+ },
+ returns => {
+ type => 'array',
+ items => {
+ type => "object",
+ properties => {
+ name => {
+ description => "IPSet name.",
+ type => 'string',
+ },
+ },
+ },
+ links => [ { rel => 'child', href => "{name}" } ],
+ },
+ code => sub {
+ my ($param) = @_;
+
+ my $fw_conf = $class->load_config();
+
+ my $res = [];
+ foreach my $name (keys %{$fw_conf->{ipset}}) {
+ push @$res, { name => $name, count => scalar(@{$fw_conf->{ipset}->{$name}}) };
+ }
+
+ return $res;
+ }});
+}
+
+sub register_create {
+ my ($class) = @_;
+
+ $class->register_method({
+ name => 'create_ipset',
+ path => '',
+ method => 'POST',
+ description => "Create new IPSet",
+ protected => 1,
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ name => {
+ # fixme: verify format
+ description => "IP set name.",
+ type => 'string',
+ },
+ rename => {
+ description => "Rename an existing IPSet.",
+ type => 'string',
+ optional => 1,
+ },
+ }
+ },
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+
+ my $fw_conf = $class->load_config();
+
+ foreach my $name (keys %{$fw_conf->{ipset}}) {
+ raise_param_exc({ name => "IPSet '$name' already exists" })
+ if $name eq $param->{name};
+ }
+
+ if ($param->{rename}) {
+ raise_param_exc({ name => "IPSet '$param->{rename}' does not exists" })
+ if !$fw_conf->{ipset}->{$param->{rename}};
+ my $data = delete $fw_conf->{ipset}->{$param->{rename}};
+ $fw_conf->{ipset}->{$param->{name}} = $data;
+ } else {
+ $fw_conf->{ipset}->{$param->{name}} = [];
+ }
+
+ $class->save_config($fw_conf);
+
+ return undef;
+ }});
+}
+
+sub register_delete {
+ my ($class) = @_;
+
+ $class->register_method({
+ name => 'delete_ipset',
+ path => '{name}',
+ method => 'DELETE',
+ description => "Delete IPSet",
+ protected => 1,
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ name => {
+ # fixme: verify format
+ description => "IP set name.",
+ type => 'string',
+ },
+ }
+ },
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+
+ my $fw_conf = $class->load_config();
+
+ return undef if !$fw_conf->{ipset}->{$param->{name}};
+
+ die "IPSet '$param->{name}' is not empty\n"
+ if scalar(@{$fw_conf->{ipset}->{$param->{name}}});
+
+ delete $fw_conf->{ipset}->{$param->{name}};
+
+ $class->save_config($fw_conf);
+
+ return undef;
+ }});
+}
+
+sub register_handlers {
+ my ($class) = @_;
+
+ $class->register_index();
+ $class->register_create();
+ $class->register_delete();
+}
+
+package PVE::API2::Firewall::ClusterIPSetList;
+
+use strict;
+use warnings;
+use PVE::Firewall;
+
+use base qw(PVE::API2::Firewall::BaseIPSetList);
+
+sub load_config {
+ my ($class) = @_;
+
+ return PVE::Firewall::load_clusterfw_conf();
+}
+
+sub save_config {
+ my ($class, $fw_conf) = @_;
+
+ PVE::Firewall::save_clusterfw_conf($fw_conf);
+}
+
+__PACKAGE__->register_handlers();
+
+__PACKAGE__->register_method ({
+ subclass => "PVE::API2::Firewall::ClusterIPset",
+ path => '{name}',
+ # set fragment delimiter (no subdirs) - we need that, because CIDR address contain a slash '/'
+ fragmentDelimiter => '',
+});
+
1;