]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Commands/remote.pm
register standard option inside PVE::APIClient::Config
[pve-client.git] / PVE / APIClient / Commands / remote.pm
index a6eb512bbadca232b6f4d274ac894df0b10c1fbb..5d04e3a87cc21e73793dfe907c9e359e0f305030 100644 (file)
@@ -3,36 +3,42 @@ 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::APIClient::Config;
 
 use PVE::CLIHandler;
 
-use base qw(PVE::CLIHandler);
-
-my $remote_name_regex = qr(\w+);
-
-my $complete_remote_name = sub {
+use PVE::APIClient::LWP;
+use PVE::PTY ();
 
-    my $conf = PVE::APIClient::Config::load_config();
+use base qw(PVE::CLIHandler);
 
-    my $res = [];
+sub read_password {
+   return PVE::PTY::read_password("Remote password: ")
+}
 
-    foreach my $k (keys %$conf) {
-       if ($k =~ m/^remote_($remote_name_regex)$/) {
-           push @$res, $1;
+__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->new();
+       my $known_remotes = $config->remote_names;
+
+       printf("%10s %10s %10s %10s %100s\n", "Name", "Host", "Port", "Username", "Fingerprint");
+       for my $name (@$known_remotes) {
+           my $remote = $config->lookup_remote($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',
@@ -44,22 +50,56 @@ __PACKAGE__->register_method ({
        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 ({
@@ -77,13 +117,17 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       die "implement me";
+       my $config = PVE::APIClient::Config->new();
+       $config->remove_remote($param->{name});
+       $config->save;
 
+       return undef;
     }});
 
 our $cmddef = {
-    add => [ __PACKAGE__, 'add', ['name', 'host']],
+    add => [ __PACKAGE__, 'add', ['name', 'host', 'username']],
     remove => [ __PACKAGE__, 'remove', ['name']],
+    list => [__PACKAGE__, 'list'],
 };
 
 1;