]> git.proxmox.com Git - pve-client.git/blame - PVE/APIClient/Commands/remote.pm
Implement remote add
[pve-client.git] / PVE / APIClient / Commands / remote.pm
CommitLineData
565bbc73
DM
1package PVE::APIClient::Commands::remote;
2
3use strict;
4use warnings;
5
3454a319
DM
6use PVE::JSONSchema qw(register_standard_option get_standard_option);
7use PVE::APIClient::Config;
8
565bbc73
DM
9use PVE::CLIHandler;
10
2d0ebe21
RJ
11use PVE::APIClient::LWP;
12use PVE::PTY ();
13
565bbc73
DM
14use base qw(PVE::CLIHandler);
15
3454a319
DM
16my $complete_remote_name = sub {
17
06039c7f 18 my $config = PVE::APIClient::Config->new();
2d0ebe21 19 return $config->remote_names;
3454a319
DM
20};
21
22register_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
29sub 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
114our $cmddef = {
2d0ebe21 115 add => [ __PACKAGE__, 'add', ['name', 'host', 'username']],
565bbc73
DM
116 remove => [ __PACKAGE__, 'remove', ['name']],
117};
118
1191;