]> git.proxmox.com Git - pve-storage.git/commitdiff
pbs: reuse pve apiclient for api connect helper
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 1 Dec 2020 08:53:38 +0000 (09:53 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 1 Dec 2020 18:22:47 +0000 (19:22 +0100)
it is flexible enough to easily do so, and should do well until we
actually have cheap native bindings (e.g., through wolfgangs rust
permlod magic).

Make it a private helper, we do *not* want to expose it directly for
now.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
PVE/Storage/PBSPlugin.pm
debian/control

index 380c9765676975b89bab9a3cf41279cdc9be1171..f648291077e91591e8ed0d67185abe45ebdd5cb6 100644 (file)
@@ -6,12 +6,11 @@ use strict;
 use warnings;
 
 use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
-use HTTP::Request;
 use IO::File;
 use JSON;
-use LWP::UserAgent;
 use POSIX qw(strftime ENOENT);
 
+use PVE::APIClient::LWP;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::Network;
 use PVE::Storage::Plugin;
@@ -49,7 +48,7 @@ sub properties {
            minimum => 1,
            maximum => 65535,
            default => 8007,
-       }
+       },
     };
 }
 
@@ -615,6 +614,37 @@ sub status {
     return ($total, $free, $used, $active);
 }
 
+# TODO: use a client with native rust/proxmox-backup bindings to profit from
+# API schema checks and types
+my sub pbs_api_connect {
+    my ($scfg, $password) = @_;
+
+    my $params = {};
+
+    my $user = $scfg->{username} // 'root@pam';
+
+    if (my $tokenid = PVE::AccessControl::pve_verify_tokenid($user, 1)) {
+       $params->{apitoken} = "PBSAPIToken=${tokenid}=${password}";
+    } else {
+       $params->{password} = $password;
+       $params->{username} = $user;
+    }
+
+    if (my $fp = $scfg->{fingerprint}) {
+       $params->{cached_fingerprints}->{uc($fp)} = 1;
+    }
+
+    my $conn = PVE::APIClient::LWP->new(
+       %$params,
+       host => $scfg->{server},
+       port => $scfg->{port} // 8007,
+       timeout => 7, # cope with a 401 (3s api delay) and high latency
+       cookie_name => 'PBSAuthCookie',
+    );
+
+    return $conn;
+}
+
 sub activate_storage {
     my ($class, $storeid, $scfg, $cache) = @_;
 
index 05465b3ade635a75453a9a1b70a41f368c606106..833bdc48797ed096322160ab88b707dc3b1f681d 100644 (file)
@@ -29,6 +29,7 @@ Depends: ceph-common (>= 12.2~),
          glusterfs-client (>= 3.4.0-2),
          libfile-chdir-perl,
          libnet-dbus-perl,
+         libpve-apiclient-perl (>= 3.1-1),
          libpve-cluster-perl,
          libpve-common-perl (>= 6.2-4),
          librados2-perl,