X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=blobdiff_plain;f=PVE%2FAPIClient%2FCommands%2Fremote.pm;h=b8b1465321064762474294d05d4e66c7fa258282;hp=7b407390034a6467cbf0d4d73279aa1f737cfd3b;hb=2d0ebe218c6945ed6c935afc6232abc841cda5b8;hpb=565bbc73ca66a8a0612df1c04e8f5b4a07f02ae1 diff --git a/PVE/APIClient/Commands/remote.pm b/PVE/APIClient/Commands/remote.pm index 7b40739..b8b1465 100644 --- a/PVE/APIClient/Commands/remote.pm +++ b/PVE/APIClient/Commands/remote.pm @@ -3,10 +3,33 @@ package PVE::APIClient::Commands::remote; use strict; use warnings; +use PVE::JSONSchema qw(register_standard_option get_standard_option); +use PVE::APIClient::Config; + use PVE::CLIHandler; +use PVE::APIClient::LWP; +use PVE::PTY (); + use base qw(PVE::CLIHandler); +my $complete_remote_name = sub { + + 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 => qr(\w+), + completion => $complete_remote_name, +}); + +sub read_password { + return PVE::PTY::read_password("Remote password: ") +} + __PACKAGE__->register_method ({ name => 'add', path => 'add', @@ -15,27 +38,58 @@ __PACKAGE__->register_method ({ parameters => { additionalProperties => 0, properties => { - name => { - description => "The name of the remote.", - type => 'string', - }, + 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.", + 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 ({ @@ -46,10 +100,7 @@ __PACKAGE__->register_method ({ parameters => { additionalProperties => 0, properties => { - name => { - description => "The name of the remote.", - type => 'string', - }, + name => get_standard_option('pveclient-remote-name'), }, }, returns => { type => 'null'}, @@ -61,7 +112,7 @@ __PACKAGE__->register_method ({ }}); our $cmddef = { - add => [ __PACKAGE__, 'add', ['name', 'host']], + add => [ __PACKAGE__, 'add', ['name', 'host', 'username']], remove => [ __PACKAGE__, 'remove', ['name']], };