]> git.proxmox.com Git - pmg-api.git/commitdiff
refactor API2::Domains for reuse in DKIMSign
authorStoiko Ivanov <s.ivanov@proxmox.com>
Mon, 21 Oct 2019 17:23:30 +0000 (19:23 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 23 Oct 2019 09:27:43 +0000 (11:27 +0200)
both DKIM Signed Domains and Relay Domains are lists of domains (DKIMSign falls
back to Relay Domains). By refactoring the method creation we can reuse most
of the code for the handling of DKIMSign domains

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
src/PMG/API2/Domains.pm

index 1393475565d9c037dab0bbe3303f49cf295c7b7a..2f6558afe5fc8b705c98eb53dd92fb4c00d29253 100644 (file)
@@ -14,205 +14,228 @@ use PMG::Config;
 
 use base qw(PVE::RESTHandler);
 
-__PACKAGE__->register_method ({
-    name => 'index',
-    path => '',
-    method => 'GET',
-    description => "List relay domains.",
-    permissions => { check => [ 'admin', 'audit' ] },
-    proxyto => 'master',
-    parameters => {
-       additionalProperties => 0,
-       properties => {},
-    },
-    returns => {
-       type => 'array',
-       items => {
-           type => "object",
-           properties => {
-               domain => { type => 'string'},
-               comment => { type => 'string'},
-           },
+my @domain_args = ('domains', 'relay', 1);
+
+sub index_method {
+    my ($filename, $type, $run_postmap) = @_;
+    return {
+       name => 'index',
+       path => '',
+       method => 'GET',
+       description => "List $type domains.",
+       permissions => { check => [ 'admin', 'audit' ] },
+       proxyto => 'master',
+       parameters => {
+           additionalProperties => 0,
+           properties => {},
        },
-       links => [ { rel => 'child', href => "{domain}" } ],
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $domains = PVE::INotify::read_file('domains');
-
-       my $res = [];
-
-       foreach my $domain (sort keys %$domains) {
-           push @$res, $domains->{$domain};
-       }
-
-       return $res;
-    }});
-
-__PACKAGE__->register_method ({
-    name => 'create',
-    path => '',
-    method => 'POST',
-    proxyto => 'master',
-    protected => 1,
-    permissions => { check => [ 'admin' ] },
-    description => "Add relay domain.",
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           domain => {
-               description => "Domain name.",
-               type => 'string', format => 'transport-domain',
+       returns => {
+           type => 'array',
+           items => {
+               type => "object",
+               properties => {
+                   domain => { type => 'string'},
+                   comment => { type => 'string'},
+               },
            },
-           comment => {
-               description => "Comment.",
-               type => 'string',
-               optional => 1,
+           links => [ { rel => 'child', href => "{domain}" } ],
+       },
+       code => sub {
+           my ($param) = @_;
+
+           my $domains = PVE::INotify::read_file($filename);
+
+           my $res = [];
+
+           foreach my $domain (sort keys %$domains) {
+               push @$res, $domains->{$domain};
+           }
+
+           return $res;
+       }};
+}
+
+sub create_method {
+    my ($filename, $type, $run_postmap) = @_;
+    return {
+       name => 'create',
+       path => '',
+       method => 'POST',
+       proxyto => 'master',
+       protected => 1,
+       permissions => { check => [ 'admin' ] },
+       description => "Add $type domain.",
+       parameters => {
+           additionalProperties => 0,
+           properties => {
+               domain => {
+                   description => "Domain name.",
+                   type => 'string', format => 'transport-domain',
+               },
+               comment => {
+                   description => "Comment.",
+                   type => 'string',
+                   optional => 1,
+               },
            },
        },
-    },
-    returns => { type => 'null' },
-    code => sub {
-       my ($param) = @_;
+       returns => { type => 'null' },
+       code => sub {
+           my ($param) = @_;
+
+           my $code = sub {
 
-       my $code = sub {
+               my $domains = PVE::INotify::read_file($filename);
 
-           my $domains = PVE::INotify::read_file('domains');
+               die "Domain '$param->{domain}' already exists\n"
+                   if $domains->{$param->{domain}};
 
-           die "Domain '$param->{domain}' already exists\n"
-               if $domains->{$param->{domain}};
+               $domains->{$param->{domain}} = {
+                   comment => $param->{comment} // '',
+               };
 
-           $domains->{$param->{domain}} = {
-               comment => $param->{comment} // '',
+               PVE::INotify::write_file($filename, $domains);
+
+               PMG::Config::postmap_pmg_domains() if $run_postmap;
            };
 
-           PVE::INotify::write_file('domains', $domains);
-
-           PMG::Config::postmap_pmg_domains();
-       };
-
-       PMG::Config::lock_config($code, "add relay domain failed");
-
-       return undef;
-    }});
-
-__PACKAGE__->register_method ({
-    name => 'read',
-    path => '{domain}',
-    method => 'GET',
-    description => "Read Domain data (comment).",
-    proxyto => 'master',
-    permissions => { check => [ 'admin', 'audit' ] },
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           domain => {
-               description => "Domain name.",
-               type => 'string', format => 'transport-domain',
+           PMG::Config::lock_config($code, "add $type domain failed");
+
+           return undef;
+       }};
+}
+
+sub read_method {
+    my ($filename, $type, $run_postmap) = @_;
+    return {
+       name => 'read',
+       path => '{domain}',
+       method => 'GET',
+       description => "Read Domain data (comment).",
+       proxyto => 'master',
+       permissions => { check => [ 'admin', 'audit' ] },
+       parameters => {
+           additionalProperties => 0,
+           properties => {
+               domain => {
+                   description => "Domain name.",
+                   type => 'string', format => 'transport-domain',
+               },
            },
        },
-    },
-    returns => {
-       type => "object",
-       properties => {
-           domain => { type => 'string'},
-           comment => { type => 'string'},
-       },
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $domains = PVE::INotify::read_file('domains');
-
-       die "Domain '$param->{domain}' does not exist\n"
-           if !$domains->{$param->{domain}};
-
-       return $domains->{$param->{domain}};
-    }});
-
-__PACKAGE__->register_method ({
-    name => 'write',
-    path => '{domain}',
-    method => 'PUT',
-    description => "Update relay domain data (comment).",
-    protected => 1,
-    permissions => { check => [ 'admin' ] },
-    proxyto => 'master',
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           domain => {
-               description => "Domain name.",
-               type => 'string', format => 'transport-domain',
-           },
-           comment => {
-               description => "Comment.",
-               type => 'string',
+       returns => {
+           type => "object",
+           properties => {
+               domain => { type => 'string'},
+               comment => { type => 'string'},
            },
        },
-    },
-    returns => { type => 'null' },
-    code => sub {
-       my ($param) = @_;
-
-       my $code = sub {
+       code => sub {
+           my ($param) = @_;
 
-           my $domains = PVE::INotify::read_file('domains');
+           my $domains = PVE::INotify::read_file($filename);
 
            die "Domain '$param->{domain}' does not exist\n"
                if !$domains->{$param->{domain}};
 
-           $domains->{$param->{domain}}->{comment} = $param->{comment};
+           return $domains->{$param->{domain}};
+       }};
+}
+
+sub write_method {
+    my ($filename, $type, $run_postmap) = @_;
+    return {
+       name => 'write',
+       path => '{domain}',
+       method => 'PUT',
+       description => "Update $type domain data (comment).",
+       protected => 1,
+       permissions => { check => [ 'admin' ] },
+       proxyto => 'master',
+       parameters => {
+           additionalProperties => 0,
+           properties => {
+               domain => {
+                   description => "Domain name.",
+                   type => 'string', format => 'transport-domain',
+               },
+               comment => {
+                   description => "Comment.",
+                   type => 'string',
+               },
+           },
+       },
+       returns => { type => 'null' },
+       code => sub {
+           my ($param) = @_;
 
-           PVE::INotify::write_file('domains', $domains);
+           my $code = sub {
 
-           PMG::Config::postmap_pmg_domains();
-       };
+               my $domains = PVE::INotify::read_file($filename);
 
-       PMG::Config::lock_config($code, "update relay domain failed");
+               die "Domain '$param->{domain}' does not exist\n"
+                   if !$domains->{$param->{domain}};
 
-       return undef;
-    }});
+               $domains->{$param->{domain}}->{comment} = $param->{comment};
 
-__PACKAGE__->register_method ({
-    name => 'delete',
-    path => '{domain}',
-    method => 'DELETE',
-    description => "Delete a relay domain",
-    protected => 1,
-    permissions => { check => [ 'admin' ] },
-    proxyto => 'master',
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           domain => {
-               description => "Domain name.",
-               type => 'string', format => 'transport-domain',
-           },
-       }
-    },
-    returns => { type => 'null' },
-    code => sub {
-       my ($param) = @_;
+               PVE::INotify::write_file($filename, $domains);
 
-       my $code = sub {
+               PMG::Config::postmap_pmg_domains() if $run_postmap;
+           };
 
-           my $domains = PVE::INotify::read_file('domains');
+           PMG::Config::lock_config($code, "update $type domain failed");
+
+           return undef;
+       }};
+}
+
+sub delete_method {
+    my ($filename, $type, $run_postmap) = @_;
+    return {
+       name => 'delete',
+       path => '{domain}',
+       method => 'DELETE',
+       description => "Delete a $type domain",
+       protected => 1,
+       permissions => { check => [ 'admin' ] },
+       proxyto => 'master',
+       parameters => {
+           additionalProperties => 0,
+           properties => {
+               domain => {
+                   description => "Domain name.",
+                   type => 'string', format => 'transport-domain',
+               },
+           }
+       },
+       returns => { type => 'null' },
+       code => sub {
+           my ($param) = @_;
 
-           die "Domain '$param->{domain}' does not exist\n"
-               if !$domains->{$param->{domain}};
+           my $code = sub {
+
+               my $domains = PVE::INotify::read_file($filename);
 
-           delete $domains->{$param->{domain}};
+               die "Domain '$param->{domain}' does not exist\n"
+                   if !$domains->{$param->{domain}};
 
-           PVE::INotify::write_file('domains', $domains);
+               delete $domains->{$param->{domain}};
+
+               PVE::INotify::write_file($filename, $domains);
+
+               PMG::Config::postmap_pmg_domains() if $run_postmap;
+           };
 
-           PMG::Config::postmap_pmg_domains();
-       };
+           PMG::Config::lock_config($code, "delete $type domain failed");
 
-       PMG::Config::lock_config($code, "delete relay domain failed");
+           return undef;
+       }};
+}
 
-       return undef;
-    }});
+__PACKAGE__->register_method(index_method(@domain_args));
+__PACKAGE__->register_method(create_method(@domain_args));
+__PACKAGE__->register_method(read_method(@domain_args));
+__PACKAGE__->register_method(write_method(@domain_args));
+__PACKAGE__->register_method(delete_method(@domain_args));
 
 1;