use strict;
use warnings;
use JSON;
-use File::HomeDir;
-use PVE::Tools;
-use PVE::APIClient::LWP;
+use File::HomeDir ();
+use PVE::JSONSchema qw(register_standard_option get_standard_option);
+use PVE::SectionConfig;
+use PVE::Tools qw(file_get_contents file_set_contents);
-sub load_config {
+use base qw(PVE::SectionConfig);
- my $filename = home() . '/.pveclient';
- my $conf_str = PVE::Tools::file_get_contents($filename);
+my $complete_remote_name = sub {
- my $filemode = (stat($filename))[2] & 07777;
- if ($filemode != 0600) {
- die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
- }
+ my $config = PVE::APIClient::Config->load();
+ return [keys %{$config->{ids}}];
+};
+
+register_standard_option('pveclient-remote-name', {
+ description => "The name of the remote.",
+ type => 'string',
+ pattern => qr(\w+),
+ completion => $complete_remote_name,
+});
+
+
+my $defaultData = {
+ propertyList => {
+ type => {
+ description => "Section type.",
+ optional => 1,
+ },
+ name => get_standard_option('pveclient-remote-name'),
+ host => {
+ description => "The host.",
+ type => 'string', format => 'address',
+ optional => 1,
+ },
+ username => {
+ description => "The username.",
+ type => 'string',
+ optional => 1,
+ },
+ password => {
+ description => "The users password.",
+ type => 'string',
+ optional => 1,
+ },
+ port => {
+ description => "The port.",
+ type => 'integer',
+ optional => 1,
+ default => 8006,
+ },
+ fingerprint => {
+ description => "Fingerprint.",
+ type => 'string',
+ optional => 1,
+ },
+ comment => {
+ description => "Description.",
+ type => 'string',
+ optional => 1,
+ maxLength => 4096,
+ },
+ },
+};
+
+sub type {
+ return 'remote';
+}
- return decode_json($conf_str);
+sub options {
+ return {
+ name => { optional => 0 },
+ host => { optional => 0 },
+ comment => { optional => 1 },
+ username => { optional => 0 },
+ password => { optional => 0 },
+ port => { optional => 1 },
+ fingerprint => { optional => 1 },
+ };
}
-sub load_remote_config {
- my ($remote) = @_;
+sub private {
+ return $defaultData;
+}
+
+sub config_filename {
+ my ($class) = @_;
+
+ return File::HomeDir::home() . '/.pveclient';
+}
+
+sub load {
+ my ($class) = @_;
+
+ my $filename = $class->config_filename();
- my $conf = load_config();
+ my $raw = '';
- my $remote_conf = $conf->{"remote_$remote"} ||
- die "no such remote '$remote'\n";
+ if (-e $filename) {
+ my $filemode = (stat($filename))[2] & 07777;
+ if ($filemode != 0600) {
+ die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
+ }
- foreach my $opt (qw(hostname username password fingerprint)) {
- die "missing option '$opt' (remote '$remote')" if !defined($remote_conf->{$opt});
+ $raw = file_get_contents($filename);
}
- return $remote_conf;
+ return $class->parse_config($filename, $raw);
}
-sub get_remote_connection {
- my ($remote) = @_;
+sub save {
+ my ($class, $cfg) = @_;
- my $conf = load_remote_config($remote);
+ my $filename = $class->config_filename();
+ my $raw = $class->write_config($filename, $cfg);
- return PVE::APIClient::LWP->new(
- username => $conf->{username},
- password => $conf->{password},
- host => $conf->{hostname},
- cached_fingerprints => {
- $conf->{fingerprint} => 1
- });
+ file_set_contents($filename, $raw, 0600);
}
+sub lookup_remote {
+ my ($class, $cfg, $name, $noerr) = @_;
+
+ my $data = $cfg->{ids}->{$name};
+
+ return $data if $noerr || defined($data);
+
+ die "unknown remote \"$name\"\n";
+}
+
+sub remote_conn {
+ my ($class, $cfg, $remote) = @_;
+
+ my $section = $class->lookup_remote($cfg, $remote);
+ my $conn = PVE::APIClient::LWP->new(
+ username => $section->{username},
+ password => $section->{password},
+ host => $section->{host},
+ port => $section->{port},
+ cached_fingerprints => {
+ $section->{fingerprint} => 1,
+ }
+ );
+
+ $conn->login;
+
+ return $conn;
+}
+
+__PACKAGE__->register();
+__PACKAGE__->init();
+
1;