X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=blobdiff_plain;f=PVE%2FAPIClient%2FCommands%2Fremote.pm;h=b23390150f50588bb748fe9224af61fd91ba0bf0;hp=e8c876a7259921cff5f83452537f828e66985063;hb=e3468666311c5f2e5047b3773a27033b9574f33e;hpb=a304c3d793fec74aba1554cc0b7961024018f411 diff --git a/PVE/APIClient/Commands/remote.pm b/PVE/APIClient/Commands/remote.pm index e8c876a..b233901 100644 --- a/PVE/APIClient/Commands/remote.pm +++ b/PVE/APIClient/Commands/remote.pm @@ -3,24 +3,25 @@ package PVE::APIClient::Commands::remote; use strict; use warnings; -use PVE::JSONSchema qw(get_standard_option); -use PVE::Tools qw(extract_param); +use PVE::APIClient::Helpers; +use PVE::APIClient::JSONSchema qw(get_standard_option); +use PVE::APIClient::Tools qw(extract_param); use PVE::APIClient::Config; -use PVE::CLIHandler; +use PVE::APIClient::CLIHandler; use PVE::APIClient::LWP; -use PVE::PTY (); +use PVE::APIClient::PTY; -use base qw(PVE::CLIHandler); +use base qw(PVE::APIClient::CLIHandler); sub read_password { - return PVE::PTY::read_password("Remote password: ") + return PVE::APIClient::PTY::read_password("Remote password: ") } __PACKAGE__->register_method ({ - name => 'list', - path => 'list', + name => 'remote_list', + path => 'remote_list', method => 'GET', description => "List remotes from your config file.", parameters => { @@ -32,28 +33,28 @@ __PACKAGE__->register_method ({ 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'} // '-'); + my $data = $config->{ids}->{$name}; + next if $data->{type} ne 'remote'; + printf("%10s %10s %10s %10s %100s\n", $name, $data->{'host'}, + $data->{'port'} // '-', $data->{'username'}, $data->{'fingerprint'} // '-'); } return undef; }}); __PACKAGE__->register_method ({ - name => 'add', - path => 'add', + name => 'remote_add', + path => 'remote_add', method => 'POST', description => "Add a remote to your config file.", - parameters => PVE::APIClient::Config->createSchema(1), + parameters => PVE::APIClient::RemoteConfig->createSchema(1), returns => { type => 'null'}, code => sub { my ($param) = @_; - # fixme: lock config file - my $remote = $param->{name}; + # Note: we try to keep lock time sort, and lock later when we have all info my $config = PVE::APIClient::Config->load(); die "Remote '$remote' already exists\n" @@ -61,9 +62,14 @@ __PACKAGE__->register_method ({ my $last_fp = 0; + my $password = $param->{password}; + if (!defined($password)) { + $password = PVE::APIClient::PTY::read_password("Remote password: "); + } + my $setup = { username => $param->{username}, - password => $param->{password}, + password => $password, host => $param->{host}, port => $param->{port} // 8006, }; @@ -84,62 +90,78 @@ __PACKAGE__->register_method ({ $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); + my $code = sub { + + $config = PVE::APIClient::Config->load(); # reload + + # check again (file is locked now) + die "Remote '$remote' already exists\n" + if $config->{ids}->{$remote}; + + my $opts = $plugin->check_config($remote, $param, 1, 1); + + $config->{ids}->{$remote} = $opts; + + PVE::APIClient::Config->save($config); + }; + + PVE::APIClient::Config->lock_config(undef, $code); return undef; }}); __PACKAGE__->register_method ({ - name => 'update', - path => 'update', + name => 'remote_set', + path => 'remote_set', method => 'PUT', description => "Update a remote configuration.", - parameters => PVE::APIClient::Config->updateSchema(1), + parameters => PVE::APIClient::RemoteConfig->updateSchema(1), returns => { type => 'null'}, code => sub { my ($param) = @_; - # 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 $code = sub { + 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); + my $plugin = PVE::APIClient::Config->lookup('remote'); + my $opts = $plugin->check_config($name, $param, 0, 1); - foreach my $k (%$opts) { - $remote->{$k} = $opts->{$k}; - } + 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}; + if ($delete) { + my $options = $plugin->private()->{options}->{'remote'}; + foreach my $k (PVE::APIClient::Tools::APIClient::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); + PVE::APIClient::Config->save($config); + }; + + PVE::APIClient::Config->lock_config(undef, $code); return undef; }}); __PACKAGE__->register_method ({ - name => 'remove', - path => 'remove', + name => 'remote_delete', + path => 'remote_delete', method => 'DELETE', description => "Removes a remote from your config file.", parameters => { @@ -152,20 +174,22 @@ __PACKAGE__->register_method ({ code => sub { my ($param) = @_; - # fixme: lock config + my $code = sub { + my $config = PVE::APIClient::Config->load(); + delete $config->{ids}->{$param->{name}}; + PVE::APIClient::Config->save($config); + }; - my $config = PVE::APIClient::Config->load(); - delete $config->{ids}->{$param->{name}}; - PVE::APIClient::Config->save($config); + PVE::APIClient::Config->lock_config(undef, $code); return undef; }}); our $cmddef = { - add => [ __PACKAGE__, 'add', ['name', 'host', 'username']], - update => [ __PACKAGE__, 'update', ['name']], - remove => [ __PACKAGE__, 'remove', ['name']], - list => [__PACKAGE__, 'list'], + add => [ __PACKAGE__, 'remote_add', ['name', 'host', 'username']], + set => [ __PACKAGE__, 'remote_set', ['name']], + delete => [ __PACKAGE__, 'remote_delete', ['name']], + list => [__PACKAGE__, 'remote_list'], }; 1;