]>
Commit | Line | Data |
---|---|---|
565bbc73 DM |
1 | package PVE::APIClient::Commands::remote; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
3454a319 DM |
6 | use PVE::JSONSchema qw(register_standard_option get_standard_option); |
7 | use PVE::APIClient::Config; | |
8 | ||
565bbc73 DM |
9 | use PVE::CLIHandler; |
10 | ||
2d0ebe21 RJ |
11 | use PVE::APIClient::LWP; |
12 | use PVE::PTY (); | |
13 | ||
565bbc73 DM |
14 | use base qw(PVE::CLIHandler); |
15 | ||
3454a319 DM |
16 | my $complete_remote_name = sub { |
17 | ||
06039c7f | 18 | my $config = PVE::APIClient::Config->new(); |
2d0ebe21 | 19 | return $config->remote_names; |
3454a319 DM |
20 | }; |
21 | ||
22 | register_standard_option('pveclient-remote-name', { | |
23 | description => "The name of the remote.", | |
24 | type => 'string', | |
2d0ebe21 | 25 | pattern => qr(\w+), |
3454a319 DM |
26 | completion => $complete_remote_name, |
27 | }); | |
28 | ||
2d0ebe21 RJ |
29 | sub read_password { |
30 | return PVE::PTY::read_password("Remote password: ") | |
31 | } | |
32 | ||
565bbc73 DM |
33 | __PACKAGE__->register_method ({ |
34 | name => 'add', | |
35 | path => 'add', | |
36 | method => 'POST', | |
37 | description => "Add a remote to your config file.", | |
38 | parameters => { | |
39 | additionalProperties => 0, | |
40 | properties => { | |
3454a319 | 41 | name => get_standard_option('pveclient-remote-name', { completion => sub {} }), |
565bbc73 | 42 | host => { |
2d0ebe21 | 43 | description => "The host.", |
565bbc73 | 44 | type => 'string', |
2d0ebe21 | 45 | format => 'address', |
565bbc73 DM |
46 | }, |
47 | username => { | |
3454a319 | 48 | description => "The username.", |
565bbc73 | 49 | type => 'string', |
565bbc73 | 50 | }, |
2d0ebe21 RJ |
51 | password => { |
52 | description => "The users password", | |
53 | type => 'string', | |
54 | }, | |
55 | port => { | |
56 | description => "The port", | |
57 | type => 'integer', | |
58 | optional => 1, | |
59 | default => 8006, | |
60 | } | |
565bbc73 DM |
61 | }, |
62 | }, | |
63 | returns => { type => 'null'}, | |
64 | code => sub { | |
65 | my ($param) = @_; | |
66 | ||
2d0ebe21 RJ |
67 | my $config = PVE::APIClient::Config->new(); |
68 | my $known_remotes = $config->remotes; | |
69 | ||
70 | if (exists($known_remotes->{$param->{name}})) { | |
71 | die "Remote \"$param->{name}\" exists, remove it first\n"; | |
72 | } | |
73 | ||
74 | my $last_fp = 0; | |
75 | my $api = PVE::APIClient::LWP->new( | |
76 | username => $param->{username}, | |
77 | password => $param->{password}, | |
78 | host => $param->{host}, | |
79 | port => $param->{port} // 8006, | |
80 | manual_verification => 1, | |
81 | register_fingerprint_cb => sub { | |
82 | my $fp = shift @_; | |
83 | $last_fp = $fp; | |
84 | }, | |
85 | ); | |
86 | $api->login(); | |
87 | ||
88 | $config->add_remote($param->{name}, $param->{host}, $param->{port} // 8006, | |
89 | $last_fp, $param->{username}, $param->{password}); | |
90 | $config->save; | |
565bbc73 | 91 | |
2d0ebe21 | 92 | return undef; |
565bbc73 DM |
93 | }}); |
94 | ||
95 | __PACKAGE__->register_method ({ | |
96 | name => 'remove', | |
97 | path => 'remove', | |
98 | method => 'DELETE', | |
99 | description => "Removes a remote from your config file.", | |
100 | parameters => { | |
101 | additionalProperties => 0, | |
102 | properties => { | |
3454a319 | 103 | name => get_standard_option('pveclient-remote-name'), |
565bbc73 DM |
104 | }, |
105 | }, | |
106 | returns => { type => 'null'}, | |
107 | code => sub { | |
108 | my ($param) = @_; | |
109 | ||
110 | die "implement me"; | |
111 | ||
112 | }}); | |
113 | ||
114 | our $cmddef = { | |
2d0ebe21 | 115 | add => [ __PACKAGE__, 'add', ['name', 'host', 'username']], |
565bbc73 DM |
116 | remove => [ __PACKAGE__, 'remove', ['name']], |
117 | }; | |
118 | ||
119 | 1; |