]> git.proxmox.com Git - pmg-api.git/commitdiff
add PMG::NodeConfig::filter_domains_by_type helper
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 17 Mar 2021 10:02:15 +0000 (11:02 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 17 Mar 2021 10:26:29 +0000 (11:26 +0100)
for reuse

The private $filter_domains is still there to do the
in-place modification it did before.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PMG/API2/Certificates.pm
src/PMG/NodeConfig.pm

index 775d5758ad5d282fcc20731d68567135ebc80b1f..b50addd17674cf2f1b8525f7d27444c4688485c9 100644 (file)
@@ -468,17 +468,13 @@ my $order_certificate = sub {
 my $filter_domains = sub {
     my ($acme_config, $type) = @_;
 
-    my $domains = $acme_config->{domains};
-    foreach my $domain (sort keys %$domains) {
-       my $entry = $domains->{$domain};
-       if (!(grep { $_ eq $type } PVE::Tools::split_list($entry->{usage}))) {
-           delete $domains->{$domain};
-       }
-    }
+    my $domains = PMG::NodeConfig::filter_domains_by_type($acme_config->{domains}, $type);
 
-    if (!%$domains) {
+    if (!$domains) {
        raise("No domains configured for type '$type'\n", 400);
     }
+
+    $acme_config->{domains} = $domains;
 };
 
 __PACKAGE__->register_method ({
index 84c2141e626bce7c591d82db7ea64f1b37ea742c..19d23c344727e0296a9899db95cbd341e4589b2c 100644 (file)
@@ -222,4 +222,23 @@ sub get_acme_conf {
     return $res;
 }
 
+# Helper to filter the domains hash. Returns `undef` if the list is empty.
+sub filter_domains_by_type : prototype($$) {
+    my ($domains, $type) = @_;
+
+    return undef if !$domains || !%$domains;
+
+    my $out = {};
+
+    foreach my $domain (keys %$domains) {
+       my $entry = $domains->{$domain};
+       if (grep { $_ eq $type } PVE::Tools::split_list($entry->{usage})) {
+           $out->{$domain} = $entry;
+       }
+    }
+
+    return undef if !%$out;
+    return $out;
+}
+
 1;