]> git.proxmox.com Git - pve-client.git/blame - PVE/APIClient/Commands/remote.pm
lxc enter: simplify code and cleanups
[pve-client.git] / PVE / APIClient / Commands / remote.pm
CommitLineData
565bbc73
DM
1package PVE::APIClient::Commands::remote;
2
3use strict;
4use warnings;
5
184877d4 6use PVE::JSONSchema qw(get_standard_option);
69aa81b3 7use PVE::Tools qw(extract_param);
3454a319
DM
8use PVE::APIClient::Config;
9
565bbc73
DM
10use PVE::CLIHandler;
11
2d0ebe21
RJ
12use PVE::APIClient::LWP;
13use PVE::PTY ();
14
565bbc73
DM
15use base qw(PVE::CLIHandler);
16
2d0ebe21
RJ
17sub read_password {
18 return PVE::PTY::read_password("Remote password: ")
19}
20
e2ca543c
RJ
21__PACKAGE__->register_method ({
22 name => 'list',
23 path => 'list',
24 method => 'GET',
25 description => "List remotes from your config file.",
26 parameters => {
27 additionalProperties => 0,
28 },
29 returns => { type => 'null' },
30 code => sub {
69aa81b3 31 my $config = PVE::APIClient::Config->load();
e2ca543c
RJ
32
33 printf("%10s %10s %10s %10s %100s\n", "Name", "Host", "Port", "Username", "Fingerprint");
69aa81b3
DM
34 for my $name (keys %{$config->{ids}}) {
35 my $remote = $config->{ids}->{$name};
e2ca543c 36 printf("%10s %10s %10s %10s %100s\n", $name, $remote->{'host'},
69aa81b3 37 $remote->{'port'} // '-', $remote->{'username'}, $remote->{'fingerprint'} // '-');
e2ca543c
RJ
38 }
39
40 return undef;
41 }});
42
565bbc73
DM
43__PACKAGE__->register_method ({
44 name => 'add',
45 path => 'add',
46 method => 'POST',
47 description => "Add a remote to your config file.",
69aa81b3 48 parameters => PVE::APIClient::Config->createSchema(1),
565bbc73
DM
49 returns => { type => 'null'},
50 code => sub {
51 my ($param) = @_;
52
69aa81b3 53 # fixme: lock config file
2d0ebe21 54
69aa81b3
DM
55 my $remote = $param->{name};
56
57 my $config = PVE::APIClient::Config->load();
58
59 die "Remote '$remote' already exists\n"
60 if $config->{ids}->{$remote};
2d0ebe21
RJ
61
62 my $last_fp = 0;
69aa81b3 63
8842464b
DM
64 my $password = $param->{password};
65 if (!defined($password)) {
66 $password = PVE::PTY::read_password("Remote password: ");
67 }
68
69aa81b3 69 my $setup = {
2d0ebe21 70 username => $param->{username},
8842464b 71 password => $password,
2d0ebe21
RJ
72 host => $param->{host},
73 port => $param->{port} // 8006,
69aa81b3
DM
74 };
75
76 if ($param->{fingerprint}) {
77 $setup->{cached_fingerprints} = {
78 $param->{fingerprint} => 1,
79 };
80 } else {
81 $setup->{manual_verification} = 1;
82 $setup->{register_fingerprint_cb} = sub {
83 my $fp = shift @_;
84 $last_fp = $fp;
85 };
86 }
87
a304c3d7 88 my $api = PVE::APIClient::LWP->new(%$setup);
2d0ebe21
RJ
89 $api->login();
90
69aa81b3
DM
91 $param->{fingerprint} = $last_fp if !defined($param->{fingerprint});
92 my $plugin = PVE::APIClient::Config->lookup('remote');
93 my $opts = $plugin->check_config($remote, $param, 1, 1);
94 $config->{ids}->{$remote} = $opts;
95
96 PVE::APIClient::Config->save($config);
97
98 return undef;
99 }});
100
101__PACKAGE__->register_method ({
102 name => 'update',
103 path => 'update',
104 method => 'PUT',
105 description => "Update a remote configuration.",
106 parameters => PVE::APIClient::Config->updateSchema(1),
107 returns => { type => 'null'},
108 code => sub {
109 my ($param) = @_;
110
111 # fixme: lock config file
112
113 my $name = extract_param($param, 'name');
114 my $digest = extract_param($param, 'digest');
115 my $delete = extract_param($param, 'delete');
116
117 my $config = PVE::APIClient::Config->load();
118 my $remote = PVE::APIClient::Config->lookup_remote($config, $name);
119
120 my $plugin = PVE::APIClient::Config->lookup('remote');
121 my $opts = $plugin->check_config($name, $param, 0, 1);
122
123 foreach my $k (%$opts) {
124 $remote->{$k} = $opts->{$k};
125 }
126
127 if ($delete) {
128 my $options = $plugin->private()->{options}->{'remote'};
129 foreach my $k (PVE::Tools::split_list($delete)) {
130 my $d = $options->{$k} ||
131 die "no such option '$k'\n";
132 die "unable to delete required option '$k'\n"
133 if !$d->{optional};
134 die "unable to delete fixed option '$k'\n"
135 if $d->{fixed};
136 delete $remote->{$k};
137 }
138 }
139
140 PVE::APIClient::Config->save($config);
565bbc73 141
2d0ebe21 142 return undef;
565bbc73
DM
143 }});
144
145__PACKAGE__->register_method ({
146 name => 'remove',
147 path => 'remove',
148 method => 'DELETE',
149 description => "Removes a remote from your config file.",
150 parameters => {
151 additionalProperties => 0,
152 properties => {
3454a319 153 name => get_standard_option('pveclient-remote-name'),
565bbc73
DM
154 },
155 },
156 returns => { type => 'null'},
157 code => sub {
158 my ($param) = @_;
159
69aa81b3
DM
160 # fixme: lock config
161
162 my $config = PVE::APIClient::Config->load();
163 delete $config->{ids}->{$param->{name}};
164 PVE::APIClient::Config->save($config);
565bbc73 165
f79acf69 166 return undef;
565bbc73
DM
167 }});
168
169our $cmddef = {
2d0ebe21 170 add => [ __PACKAGE__, 'add', ['name', 'host', 'username']],
69aa81b3 171 update => [ __PACKAGE__, 'update', ['name']],
565bbc73 172 remove => [ __PACKAGE__, 'remove', ['name']],
e2ca543c 173 list => [__PACKAGE__, 'list'],
565bbc73
DM
174};
175
1761;