Implement remote add
[pve-client.git] / PVE / APIClient / Commands / remote.pm
1 package PVE::APIClient::Commands::remote;
2
3 use strict;
4 use warnings;
5
6 use PVE::JSONSchema qw(register_standard_option get_standard_option);
7 use PVE::APIClient::Config;
8
9 use PVE::CLIHandler;
10
11 use PVE::APIClient::LWP;
12 use PVE::PTY ();
13
14 use base qw(PVE::CLIHandler);
15
16 my $complete_remote_name = sub {
17
18     my $config = PVE::APIClient::Config->new();
19     return $config->remote_names;
20 };
21
22 register_standard_option('pveclient-remote-name', {
23     description => "The name of the remote.",
24     type => 'string',
25     pattern => qr(\w+),
26     completion => $complete_remote_name,
27 });
28
29 sub read_password {
30    return PVE::PTY::read_password("Remote password: ")
31 }
32
33 __PACKAGE__->register_method ({
34     name => 'add',
35     path => 'add',
36     method => 'POST',
37     description => "Add a remote to your config file.",
38     parameters => {
39         additionalProperties => 0,
40         properties => {
41             name => get_standard_option('pveclient-remote-name', { completion => sub {} }),
42             host => {
43                 description => "The host.",
44                 type => 'string',
45                 format => 'address',
46             },
47             username => {
48                 description => "The username.",
49                 type => 'string',
50             },
51             password => {
52                 description => "The users password",
53                 type => 'string',
54             },
55             port => {
56                 description => "The port",
57                 type => 'integer',
58                 optional => 1,
59                 default => 8006,
60             }
61         },
62     },
63     returns => { type => 'null'},
64     code => sub {
65         my ($param) = @_;
66
67         my $config = PVE::APIClient::Config->new();
68         my $known_remotes = $config->remotes;
69
70         if (exists($known_remotes->{$param->{name}})) {
71             die "Remote \"$param->{name}\" exists, remove it first\n";
72         }
73
74         my $last_fp = 0;
75         my $api = PVE::APIClient::LWP->new(
76             username                => $param->{username},
77             password                => $param->{password},
78             host                    => $param->{host},
79             port                    => $param->{port} // 8006,
80             manual_verification     => 1,
81             register_fingerprint_cb => sub {
82                 my $fp = shift @_;
83                 $last_fp = $fp;
84             },
85         );
86         $api->login();
87
88         $config->add_remote($param->{name}, $param->{host}, $param->{port} // 8006, 
89                             $last_fp, $param->{username}, $param->{password});
90         $config->save;
91
92         return undef;
93     }});
94
95 __PACKAGE__->register_method ({
96     name => 'remove',
97     path => 'remove',
98     method => 'DELETE',
99     description => "Removes a remote from your config file.",
100     parameters => {
101         additionalProperties => 0,
102         properties => {
103             name => get_standard_option('pveclient-remote-name'),
104         },
105     },
106     returns => { type => 'null'},
107     code => sub {
108         my ($param) = @_;
109
110         die "implement me";
111
112     }});
113
114 our $cmddef = {
115     add => [ __PACKAGE__, 'add', ['name', 'host', 'username']],
116     remove => [ __PACKAGE__, 'remove', ['name']],
117 };
118
119 1;