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