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