]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Commands/remote.pm
2e5337aa2c9be3a2837b5019ac7a18d93f09ca79
[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::APIClient::Helpers;
7 use PVE::APIClient::JSONSchema qw(get_standard_option);
8 use PVE::APIClient::Tools qw(extract_param);
9 use PVE::APIClient::Config;
10
11 use PVE::APIClient::CLIHandler;
12
13 use PVE::APIClient::LWP;
14 use PVE::APIClient::PTY;
15
16 use base qw(PVE::APIClient::CLIHandler);
17
18 sub read_password {
19 return PVE::APIClient::PTY::read_password("Remote password: ")
20 }
21
22 # define as array to keep ordering
23 my $remote_list_returns_properties = [
24 name => get_standard_option('pveclient-remote-name'),
25 host => { type => 'string', format => 'address' },
26 username => { type => 'string' },
27 port => { type => 'integer', optional => 1 },
28 fingerprint => { type => 'string', optional => 1 },
29 ];
30
31 __PACKAGE__->register_method ({
32 name => 'remote_list',
33 path => 'remote_list',
34 method => 'GET',
35 description => "List remotes from your config file.",
36 parameters => {
37 additionalProperties => 0,
38 properties => {
39 'format' => get_standard_option('pve-output-format'),
40 },
41 },
42 returns => {
43 type => 'array',
44 items => {
45 type => 'object',
46 properties => { @$remote_list_returns_properties },
47 },
48 },
49 code => sub {
50 my ($param) = @_;
51
52 my $format = PVE::APIClient::Tools::extract_param($param, 'format');
53 PVE::APIClient::Helpers::set_output_format($format);
54
55 my $config = PVE::APIClient::Config->load();
56
57 my $res = [];
58 for my $name (keys %{$config->{ids}}) {
59 my $data = $config->{ids}->{$name};
60 next if $data->{type} ne 'remote';
61 push @$res, $data;
62 }
63
64 return $res;
65 }});
66
67 __PACKAGE__->register_method ({
68 name => 'remote_add',
69 path => 'remote_add',
70 method => 'POST',
71 description => "Add a remote to your config file.",
72 parameters => PVE::APIClient::RemoteConfig->createSchema(1),
73 returns => { type => 'null'},
74 code => sub {
75 my ($param) = @_;
76
77 my $remote = $param->{name};
78
79 # Note: we try to keep lock time sort, and lock later when we have all info
80 my $config = PVE::APIClient::Config->load();
81
82 die "Remote '$remote' already exists\n"
83 if $config->{ids}->{$remote};
84
85 my $last_fp = 0;
86
87 my $password = $param->{password};
88 if (!defined($password)) {
89 $password = PVE::APIClient::PTY::read_password("Remote password: ");
90 }
91
92 my $setup = {
93 username => $param->{username},
94 password => $password,
95 host => $param->{host},
96 port => $param->{port} // 8006,
97 };
98
99 if ($param->{fingerprint}) {
100 $setup->{cached_fingerprints} = {
101 $param->{fingerprint} => 1,
102 };
103 } else {
104 $setup->{manual_verification} = 1;
105 $setup->{register_fingerprint_cb} = sub {
106 my $fp = shift @_;
107 $last_fp = $fp;
108 };
109 }
110
111 my $api = PVE::APIClient::LWP->new(%$setup);
112 $api->login();
113
114 $param->{fingerprint} = $last_fp if !defined($param->{fingerprint});
115
116 my $plugin = PVE::APIClient::Config->lookup('remote');
117
118 my $code = sub {
119
120 $config = PVE::APIClient::Config->load(); # reload
121
122 # check again (file is locked now)
123 die "Remote '$remote' already exists\n"
124 if $config->{ids}->{$remote};
125
126 my $opts = $plugin->check_config($remote, $param, 1, 1);
127
128 $config->{ids}->{$remote} = $opts;
129
130 PVE::APIClient::Config->save($config);
131 };
132
133 PVE::APIClient::Config->lock_config(undef, $code);
134
135 return undef;
136 }});
137
138 __PACKAGE__->register_method ({
139 name => 'remote_set',
140 path => 'remote_set',
141 method => 'PUT',
142 description => "Update a remote configuration.",
143 parameters => PVE::APIClient::RemoteConfig->updateSchema(1),
144 returns => { type => 'null'},
145 code => sub {
146 my ($param) = @_;
147
148 my $name = extract_param($param, 'name');
149 my $digest = extract_param($param, 'digest');
150 my $delete = extract_param($param, 'delete');
151
152 my $code = sub {
153 my $config = PVE::APIClient::Config->load();
154 my $remote = PVE::APIClient::Config->lookup_remote($config, $name);
155
156 my $plugin = PVE::APIClient::Config->lookup('remote');
157 my $opts = $plugin->check_config($name, $param, 0, 1);
158
159 foreach my $k (%$opts) {
160 $remote->{$k} = $opts->{$k};
161 }
162
163 if ($delete) {
164 my $options = $plugin->private()->{options}->{'remote'};
165 foreach my $k (PVE::APIClient::Tools::APIClient::split_list($delete)) {
166 my $d = $options->{$k} ||
167 die "no such option '$k'\n";
168 die "unable to delete required option '$k'\n"
169 if !$d->{optional};
170 die "unable to delete fixed option '$k'\n"
171 if $d->{fixed};
172 delete $remote->{$k};
173 }
174 }
175
176 PVE::APIClient::Config->save($config);
177 };
178
179 PVE::APIClient::Config->lock_config(undef, $code);
180
181 return undef;
182 }});
183
184 __PACKAGE__->register_method ({
185 name => 'remote_delete',
186 path => 'remote_delete',
187 method => 'DELETE',
188 description => "Removes a remote from your config file.",
189 parameters => {
190 additionalProperties => 0,
191 properties => {
192 name => get_standard_option('pveclient-remote-name'),
193 },
194 },
195 returns => { type => 'null'},
196 code => sub {
197 my ($param) = @_;
198
199 my $code = sub {
200 my $config = PVE::APIClient::Config->load();
201 delete $config->{ids}->{$param->{name}};
202 PVE::APIClient::Config->save($config);
203 };
204
205 PVE::APIClient::Config->lock_config(undef, $code);
206
207 return undef;
208 }});
209
210 our $cmddef = {
211 add => [ __PACKAGE__, 'remote_add', ['name', 'host', 'username']],
212 set => [ __PACKAGE__, 'remote_set', ['name']],
213 delete => [ __PACKAGE__, 'remote_delete', ['name']],
214 list => [__PACKAGE__, 'remote_list', undef, {}, sub {
215 PVE::APIClient::Helpers::print_ordered_result($remote_list_returns_properties, @_);
216 }],
217 };
218
219 1;