]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Commands/remote.pm
e8c876a7259921cff5f83452537f828e66985063
[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::Tools qw(extract_param);
8 use PVE::APIClient::Config;
9
10 use PVE::CLIHandler;
11
12 use PVE::APIClient::LWP;
13 use PVE::PTY ();
14
15 use base qw(PVE::CLIHandler);
16
17 sub read_password {
18 return PVE::PTY::read_password("Remote password: ")
19 }
20
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 {
31 my $config = PVE::APIClient::Config->load();
32
33 printf("%10s %10s %10s %10s %100s\n", "Name", "Host", "Port", "Username", "Fingerprint");
34 for my $name (keys %{$config->{ids}}) {
35 my $remote = $config->{ids}->{$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 => PVE::APIClient::Config->createSchema(1),
49 returns => { type => 'null'},
50 code => sub {
51 my ($param) = @_;
52
53 # fixme: lock config file
54
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};
61
62 my $last_fp = 0;
63
64 my $setup = {
65 username => $param->{username},
66 password => $param->{password},
67 host => $param->{host},
68 port => $param->{port} // 8006,
69 };
70
71 if ($param->{fingerprint}) {
72 $setup->{cached_fingerprints} = {
73 $param->{fingerprint} => 1,
74 };
75 } else {
76 $setup->{manual_verification} = 1;
77 $setup->{register_fingerprint_cb} = sub {
78 my $fp = shift @_;
79 $last_fp = $fp;
80 };
81 }
82
83 my $api = PVE::APIClient::LWP->new(%$setup);
84 $api->login();
85
86 $param->{fingerprint} = $last_fp if !defined($param->{fingerprint});
87 my $plugin = PVE::APIClient::Config->lookup('remote');
88 my $opts = $plugin->check_config($remote, $param, 1, 1);
89 $config->{ids}->{$remote} = $opts;
90
91 PVE::APIClient::Config->save($config);
92
93 return undef;
94 }});
95
96 __PACKAGE__->register_method ({
97 name => 'update',
98 path => 'update',
99 method => 'PUT',
100 description => "Update a remote configuration.",
101 parameters => PVE::APIClient::Config->updateSchema(1),
102 returns => { type => 'null'},
103 code => sub {
104 my ($param) = @_;
105
106 # fixme: lock config file
107
108 my $name = extract_param($param, 'name');
109 my $digest = extract_param($param, 'digest');
110 my $delete = extract_param($param, 'delete');
111
112 my $config = PVE::APIClient::Config->load();
113 my $remote = PVE::APIClient::Config->lookup_remote($config, $name);
114
115 my $plugin = PVE::APIClient::Config->lookup('remote');
116 my $opts = $plugin->check_config($name, $param, 0, 1);
117
118 foreach my $k (%$opts) {
119 $remote->{$k} = $opts->{$k};
120 }
121
122 if ($delete) {
123 my $options = $plugin->private()->{options}->{'remote'};
124 foreach my $k (PVE::Tools::split_list($delete)) {
125 my $d = $options->{$k} ||
126 die "no such option '$k'\n";
127 die "unable to delete required option '$k'\n"
128 if !$d->{optional};
129 die "unable to delete fixed option '$k'\n"
130 if $d->{fixed};
131 delete $remote->{$k};
132 }
133 }
134
135 PVE::APIClient::Config->save($config);
136
137 return undef;
138 }});
139
140 __PACKAGE__->register_method ({
141 name => 'remove',
142 path => 'remove',
143 method => 'DELETE',
144 description => "Removes a remote from your config file.",
145 parameters => {
146 additionalProperties => 0,
147 properties => {
148 name => get_standard_option('pveclient-remote-name'),
149 },
150 },
151 returns => { type => 'null'},
152 code => sub {
153 my ($param) = @_;
154
155 # fixme: lock config
156
157 my $config = PVE::APIClient::Config->load();
158 delete $config->{ids}->{$param->{name}};
159 PVE::APIClient::Config->save($config);
160
161 return undef;
162 }});
163
164 our $cmddef = {
165 add => [ __PACKAGE__, 'add', ['name', 'host', 'username']],
166 update => [ __PACKAGE__, 'update', ['name']],
167 remove => [ __PACKAGE__, 'remove', ['name']],
168 list => [__PACKAGE__, 'list'],
169 };
170
171 1;