]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Commands/remote.pm
5d04e3a87cc21e73793dfe907c9e359e0f305030
[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(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 sub read_password {
17 return PVE::PTY::read_password("Remote password: ")
18 }
19
20 __PACKAGE__->register_method ({
21 name => 'list',
22 path => 'list',
23 method => 'GET',
24 description => "List remotes from your config file.",
25 parameters => {
26 additionalProperties => 0,
27 },
28 returns => { type => 'null' },
29 code => sub {
30 my $config = PVE::APIClient::Config->new();
31 my $known_remotes = $config->remote_names;
32
33 printf("%10s %10s %10s %10s %100s\n", "Name", "Host", "Port", "Username", "Fingerprint");
34 for my $name (@$known_remotes) {
35 my $remote = $config->lookup_remote($name);
36 printf("%10s %10s %10s %10s %100s\n", $name, $remote->{'host'},
37 $remote->{'port'}, $remote->{'username'}, $remote->{'fingerprint'});
38 }
39
40 return undef;
41 }});
42
43 __PACKAGE__->register_method ({
44 name => 'add',
45 path => 'add',
46 method => 'POST',
47 description => "Add a remote to your config file.",
48 parameters => {
49 additionalProperties => 0,
50 properties => {
51 name => get_standard_option('pveclient-remote-name', { completion => sub {} }),
52 host => {
53 description => "The host.",
54 type => 'string',
55 format => 'address',
56 },
57 username => {
58 description => "The username.",
59 type => 'string',
60 },
61 password => {
62 description => "The users password",
63 type => 'string',
64 },
65 port => {
66 description => "The port",
67 type => 'integer',
68 optional => 1,
69 default => 8006,
70 }
71 },
72 },
73 returns => { type => 'null'},
74 code => sub {
75 my ($param) = @_;
76
77 my $config = PVE::APIClient::Config->new();
78 my $known_remotes = $config->remotes;
79
80 if (exists($known_remotes->{$param->{name}})) {
81 die "Remote \"$param->{name}\" exists, remove it first\n";
82 }
83
84 my $last_fp = 0;
85 my $api = PVE::APIClient::LWP->new(
86 username => $param->{username},
87 password => $param->{password},
88 host => $param->{host},
89 port => $param->{port} // 8006,
90 manual_verification => 1,
91 register_fingerprint_cb => sub {
92 my $fp = shift @_;
93 $last_fp = $fp;
94 },
95 );
96 $api->login();
97
98 $config->add_remote($param->{name}, $param->{host}, $param->{port} // 8006,
99 $last_fp, $param->{username}, $param->{password});
100 $config->save;
101
102 return undef;
103 }});
104
105 __PACKAGE__->register_method ({
106 name => 'remove',
107 path => 'remove',
108 method => 'DELETE',
109 description => "Removes a remote from your config file.",
110 parameters => {
111 additionalProperties => 0,
112 properties => {
113 name => get_standard_option('pveclient-remote-name'),
114 },
115 },
116 returns => { type => 'null'},
117 code => sub {
118 my ($param) = @_;
119
120 my $config = PVE::APIClient::Config->new();
121 $config->remove_remote($param->{name});
122 $config->save;
123
124 return undef;
125 }});
126
127 our $cmddef = {
128 add => [ __PACKAGE__, 'add', ['name', 'host', 'username']],
129 remove => [ __PACKAGE__, 'remove', ['name']],
130 list => [__PACKAGE__, 'list'],
131 };
132
133 1;