Implement remote add

Signed-off-by: René Jochum <r.jochum@proxmox.com>
This commit is contained in:
René Jochum 2018-06-04 16:49:03 +02:00 committed by Dietmar Maurer
parent 06039c7f9b
commit 2d0ebe218c

View File

@ -8,23 +8,28 @@ use PVE::APIClient::Config;
use PVE::CLIHandler; use PVE::CLIHandler;
use PVE::APIClient::LWP;
use PVE::PTY ();
use base qw(PVE::CLIHandler); use base qw(PVE::CLIHandler);
my $complete_remote_name = sub { my $complete_remote_name = sub {
my $config = PVE::APIClient::Config->new(); my $config = PVE::APIClient::Config->new();
my $known_remotes = $config->remotes; return $config->remote_names;
return [keys %{$known_remotes}];
}; };
register_standard_option('pveclient-remote-name', { register_standard_option('pveclient-remote-name', {
description => "The name of the remote.", description => "The name of the remote.",
type => 'string', type => 'string',
pattern => qr([\w\d\.\-\_]+), pattern => qr(\w+),
completion => $complete_remote_name, completion => $complete_remote_name,
}); });
sub read_password {
return PVE::PTY::read_password("Remote password: ")
}
__PACKAGE__->register_method ({ __PACKAGE__->register_method ({
name => 'add', name => 'add',
path => 'add', path => 'add',
@ -35,22 +40,56 @@ __PACKAGE__->register_method ({
properties => { properties => {
name => get_standard_option('pveclient-remote-name', { completion => sub {} }), name => get_standard_option('pveclient-remote-name', { completion => sub {} }),
host => { host => {
description => "The host, either host, host:port or https://host:port", description => "The host.",
type => 'string', type => 'string',
format => 'address',
}, },
username => { username => {
description => "The username.", description => "The username.",
type => 'string', type => 'string',
optional => 1,
}, },
password => {
description => "The users password",
type => 'string',
},
port => {
description => "The port",
type => 'integer',
optional => 1,
default => 8006,
}
}, },
}, },
returns => { type => 'null'}, returns => { type => 'null'},
code => sub { code => sub {
my ($param) = @_; my ($param) = @_;
die "implement me"; my $config = PVE::APIClient::Config->new();
my $known_remotes = $config->remotes;
if (exists($known_remotes->{$param->{name}})) {
die "Remote \"$param->{name}\" exists, remove it first\n";
}
my $last_fp = 0;
my $api = PVE::APIClient::LWP->new(
username => $param->{username},
password => $param->{password},
host => $param->{host},
port => $param->{port} // 8006,
manual_verification => 1,
register_fingerprint_cb => sub {
my $fp = shift @_;
$last_fp = $fp;
},
);
$api->login();
$config->add_remote($param->{name}, $param->{host}, $param->{port} // 8006,
$last_fp, $param->{username}, $param->{password});
$config->save;
return undef;
}}); }});
__PACKAGE__->register_method ({ __PACKAGE__->register_method ({
@ -73,7 +112,7 @@ __PACKAGE__->register_method ({
}}); }});
our $cmddef = { our $cmddef = {
add => [ __PACKAGE__, 'add', ['name', 'host']], add => [ __PACKAGE__, 'add', ['name', 'host', 'username']],
remove => [ __PACKAGE__, 'remove', ['name']], remove => [ __PACKAGE__, 'remove', ['name']],
}; };