X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=blobdiff_plain;f=PVE%2FAPIClient%2FCommands%2Fremote.pm;h=781fb63818ca4104d1d4167161dbef12364c14ff;hp=a6eb512bbadca232b6f4d274ac894df0b10c1fbb;hb=8842464b81a452996373e5d21205282eeef66888;hpb=3454a319f2bbd606609971ff438cd4fe05c815d0 diff --git a/PVE/APIClient/Commands/remote.pm b/PVE/APIClient/Commands/remote.pm index a6eb512..781fb63 100644 --- a/PVE/APIClient/Commands/remote.pm +++ b/PVE/APIClient/Commands/remote.pm @@ -3,63 +3,143 @@ package PVE::APIClient::Commands::remote; use strict; use warnings; -use PVE::JSONSchema qw(register_standard_option get_standard_option); +use PVE::JSONSchema qw(get_standard_option); +use PVE::Tools qw(extract_param); use PVE::APIClient::Config; use PVE::CLIHandler; -use base qw(PVE::CLIHandler); - -my $remote_name_regex = qr(\w+); +use PVE::APIClient::LWP; +use PVE::PTY (); -my $complete_remote_name = sub { +use base qw(PVE::CLIHandler); - my $conf = PVE::APIClient::Config::load_config(); +sub read_password { + return PVE::PTY::read_password("Remote password: ") +} - my $res = []; +__PACKAGE__->register_method ({ + name => 'list', + path => 'list', + method => 'GET', + description => "List remotes from your config file.", + parameters => { + additionalProperties => 0, + }, + returns => { type => 'null' }, + code => sub { + my $config = PVE::APIClient::Config->load(); - foreach my $k (keys %$conf) { - if ($k =~ m/^remote_($remote_name_regex)$/) { - push @$res, $1; + printf("%10s %10s %10s %10s %100s\n", "Name", "Host", "Port", "Username", "Fingerprint"); + for my $name (keys %{$config->{ids}}) { + my $remote = $config->{ids}->{$name}; + printf("%10s %10s %10s %10s %100s\n", $name, $remote->{'host'}, + $remote->{'port'} // '-', $remote->{'username'}, $remote->{'fingerprint'} // '-'); } - } - return $res; -}; - -register_standard_option('pveclient-remote-name', { - description => "The name of the remote.", - type => 'string', - pattern => $remote_name_regex, - completion => $complete_remote_name, -}); + return undef; + }}); __PACKAGE__->register_method ({ name => 'add', path => 'add', method => 'POST', description => "Add a remote to your config file.", - parameters => { - additionalProperties => 0, - properties => { - name => get_standard_option('pveclient-remote-name', { completion => sub {} }), - host => { - description => "The host, either host, host:port or https://host:port", - type => 'string', - }, - username => { - description => "The username.", - type => 'string', - optional => 1, - }, - }, - }, + parameters => PVE::APIClient::Config->createSchema(1), + returns => { type => 'null'}, + code => sub { + my ($param) = @_; + + # fixme: lock config file + + my $remote = $param->{name}; + + my $config = PVE::APIClient::Config->load(); + + die "Remote '$remote' already exists\n" + if $config->{ids}->{$remote}; + + my $last_fp = 0; + + my $password = $param->{password}; + if (!defined($password)) { + $password = PVE::PTY::read_password("Remote password: "); + } + + my $setup = { + username => $param->{username}, + password => $password, + host => $param->{host}, + port => $param->{port} // 8006, + }; + + if ($param->{fingerprint}) { + $setup->{cached_fingerprints} = { + $param->{fingerprint} => 1, + }; + } else { + $setup->{manual_verification} = 1; + $setup->{register_fingerprint_cb} = sub { + my $fp = shift @_; + $last_fp = $fp; + }; + } + + my $api = PVE::APIClient::LWP->new(%$setup); + $api->login(); + + $param->{fingerprint} = $last_fp if !defined($param->{fingerprint}); + my $plugin = PVE::APIClient::Config->lookup('remote'); + my $opts = $plugin->check_config($remote, $param, 1, 1); + $config->{ids}->{$remote} = $opts; + + PVE::APIClient::Config->save($config); + + return undef; + }}); + +__PACKAGE__->register_method ({ + name => 'update', + path => 'update', + method => 'PUT', + description => "Update a remote configuration.", + parameters => PVE::APIClient::Config->updateSchema(1), returns => { type => 'null'}, code => sub { my ($param) = @_; - die "implement me"; + # fixme: lock config file + + my $name = extract_param($param, 'name'); + my $digest = extract_param($param, 'digest'); + my $delete = extract_param($param, 'delete'); + my $config = PVE::APIClient::Config->load(); + my $remote = PVE::APIClient::Config->lookup_remote($config, $name); + + my $plugin = PVE::APIClient::Config->lookup('remote'); + my $opts = $plugin->check_config($name, $param, 0, 1); + + foreach my $k (%$opts) { + $remote->{$k} = $opts->{$k}; + } + + if ($delete) { + my $options = $plugin->private()->{options}->{'remote'}; + foreach my $k (PVE::Tools::split_list($delete)) { + my $d = $options->{$k} || + die "no such option '$k'\n"; + die "unable to delete required option '$k'\n" + if !$d->{optional}; + die "unable to delete fixed option '$k'\n" + if $d->{fixed}; + delete $remote->{$k}; + } + } + + PVE::APIClient::Config->save($config); + + return undef; }}); __PACKAGE__->register_method ({ @@ -77,13 +157,20 @@ __PACKAGE__->register_method ({ code => sub { my ($param) = @_; - die "implement me"; + # fixme: lock config + + my $config = PVE::APIClient::Config->load(); + delete $config->{ids}->{$param->{name}}; + PVE::APIClient::Config->save($config); + return undef; }}); our $cmddef = { - add => [ __PACKAGE__, 'add', ['name', 'host']], + add => [ __PACKAGE__, 'add', ['name', 'host', 'username']], + update => [ __PACKAGE__, 'update', ['name']], remove => [ __PACKAGE__, 'remove', ['name']], + list => [__PACKAGE__, 'list'], }; 1;