use PVE::CLIHandler;
-use base qw(PVE::CLIHandler);
+use PVE::APIClient::LWP;
+use PVE::PTY ();
-my $remote_name_regex = qr(\w+);
+use base qw(PVE::CLIHandler);
my $complete_remote_name = sub {
- my $conf = PVE::APIClient::Config::load_config();
-
- my $res = [];
-
- foreach my $k (keys %$conf) {
- if ($k =~ m/^remote_($remote_name_regex)$/) {
- push @$res, $1;
- }
- }
-
- return $res;
+ my $config = PVE::APIClient::Config->new();
+ return $config->remote_names;
};
register_standard_option('pveclient-remote-name', {
description => "The name of the remote.",
type => 'string',
- pattern => $remote_name_regex,
+ pattern => qr(\w+),
completion => $complete_remote_name,
});
+sub read_password {
+ return PVE::PTY::read_password("Remote password: ")
+}
+
__PACKAGE__->register_method ({
name => 'add',
path => 'add',
properties => {
name => get_standard_option('pveclient-remote-name', { completion => sub {} }),
host => {
- description => "The host, either host, host:port or https://host:port",
+ description => "The host.",
type => 'string',
+ format => 'address',
},
username => {
description => "The username.",
type => 'string',
- optional => 1,
},
+ password => {
+ description => "The users password",
+ type => 'string',
+ },
+ port => {
+ description => "The port",
+ type => 'integer',
+ optional => 1,
+ default => 8006,
+ }
},
},
returns => { type => 'null'},
code => sub {
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 ({
}});
our $cmddef = {
- add => [ __PACKAGE__, 'add', ['name', 'host']],
+ add => [ __PACKAGE__, 'add', ['name', 'host', 'username']],
remove => [ __PACKAGE__, 'remove', ['name']],
};