]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Commands/remote.pm
2e5337aa2c9be3a2837b5019ac7a18d93f09ca79
1 package PVE
::APIClient
::Commands
::remote
;
6 use PVE
::APIClient
::Helpers
;
7 use PVE
::APIClient
::JSONSchema
qw(get_standard_option);
8 use PVE
::APIClient
::Tools
qw(extract_param);
9 use PVE
::APIClient
::Config
;
11 use PVE
::APIClient
::CLIHandler
;
13 use PVE
::APIClient
::LWP
;
14 use PVE
::APIClient
::PTY
;
16 use base
qw(PVE::APIClient::CLIHandler);
19 return PVE
::APIClient
::PTY
::read_password
("Remote password: ")
22 # define as array to keep ordering
23 my $remote_list_returns_properties = [
24 name
=> get_standard_option
('pveclient-remote-name'),
25 host
=> { type
=> 'string', format
=> 'address' },
26 username
=> { type
=> 'string' },
27 port
=> { type
=> 'integer', optional
=> 1 },
28 fingerprint
=> { type
=> 'string', optional
=> 1 },
31 __PACKAGE__-
>register_method ({
32 name
=> 'remote_list',
33 path
=> 'remote_list',
35 description
=> "List remotes from your config file.",
37 additionalProperties
=> 0,
39 'format' => get_standard_option
('pve-output-format'),
46 properties
=> { @$remote_list_returns_properties },
52 my $format = PVE
::APIClient
::Tools
::extract_param
($param, 'format');
53 PVE
::APIClient
::Helpers
::set_output_format
($format);
55 my $config = PVE
::APIClient
::Config-
>load();
58 for my $name (keys %{$config->{ids
}}) {
59 my $data = $config->{ids
}->{$name};
60 next if $data->{type
} ne 'remote';
67 __PACKAGE__-
>register_method ({
71 description
=> "Add a remote to your config file.",
72 parameters
=> PVE
::APIClient
::RemoteConfig-
>createSchema(1),
73 returns
=> { type
=> 'null'},
77 my $remote = $param->{name
};
79 # Note: we try to keep lock time sort, and lock later when we have all info
80 my $config = PVE
::APIClient
::Config-
>load();
82 die "Remote '$remote' already exists\n"
83 if $config->{ids
}->{$remote};
87 my $password = $param->{password
};
88 if (!defined($password)) {
89 $password = PVE
::APIClient
::PTY
::read_password
("Remote password: ");
93 username
=> $param->{username
},
94 password
=> $password,
95 host
=> $param->{host
},
96 port
=> $param->{port
} // 8006,
99 if ($param->{fingerprint
}) {
100 $setup->{cached_fingerprints
} = {
101 $param->{fingerprint
} => 1,
104 $setup->{manual_verification
} = 1;
105 $setup->{register_fingerprint_cb
} = sub {
111 my $api = PVE
::APIClient
::LWP-
>new(%$setup);
114 $param->{fingerprint
} = $last_fp if !defined($param->{fingerprint
});
116 my $plugin = PVE
::APIClient
::Config-
>lookup('remote');
120 $config = PVE
::APIClient
::Config-
>load(); # reload
122 # check again (file is locked now)
123 die "Remote '$remote' already exists\n"
124 if $config->{ids
}->{$remote};
126 my $opts = $plugin->check_config($remote, $param, 1, 1);
128 $config->{ids
}->{$remote} = $opts;
130 PVE
::APIClient
::Config-
>save($config);
133 PVE
::APIClient
::Config-
>lock_config(undef, $code);
138 __PACKAGE__-
>register_method ({
139 name
=> 'remote_set',
140 path
=> 'remote_set',
142 description
=> "Update a remote configuration.",
143 parameters
=> PVE
::APIClient
::RemoteConfig-
>updateSchema(1),
144 returns
=> { type
=> 'null'},
148 my $name = extract_param
($param, 'name');
149 my $digest = extract_param
($param, 'digest');
150 my $delete = extract_param
($param, 'delete');
153 my $config = PVE
::APIClient
::Config-
>load();
154 my $remote = PVE
::APIClient
::Config-
>lookup_remote($config, $name);
156 my $plugin = PVE
::APIClient
::Config-
>lookup('remote');
157 my $opts = $plugin->check_config($name, $param, 0, 1);
159 foreach my $k (%$opts) {
160 $remote->{$k} = $opts->{$k};
164 my $options = $plugin->private()->{options
}->{'remote'};
165 foreach my $k (PVE
::APIClient
::Tools
::APIClient
::split_list
($delete)) {
166 my $d = $options->{$k} ||
167 die "no such option '$k'\n";
168 die "unable to delete required option '$k'\n"
170 die "unable to delete fixed option '$k'\n"
172 delete $remote->{$k};
176 PVE
::APIClient
::Config-
>save($config);
179 PVE
::APIClient
::Config-
>lock_config(undef, $code);
184 __PACKAGE__-
>register_method ({
185 name
=> 'remote_delete',
186 path
=> 'remote_delete',
188 description
=> "Removes a remote from your config file.",
190 additionalProperties
=> 0,
192 name
=> get_standard_option
('pveclient-remote-name'),
195 returns
=> { type
=> 'null'},
200 my $config = PVE
::APIClient
::Config-
>load();
201 delete $config->{ids
}->{$param->{name
}};
202 PVE
::APIClient
::Config-
>save($config);
205 PVE
::APIClient
::Config-
>lock_config(undef, $code);
211 add
=> [ __PACKAGE__
, 'remote_add', ['name', 'host', 'username']],
212 set
=> [ __PACKAGE__
, 'remote_set', ['name']],
213 delete => [ __PACKAGE__
, 'remote_delete', ['name']],
214 list
=> [__PACKAGE__
, 'remote_list', undef, {}, sub {
215 PVE
::APIClient
::Helpers
::print_ordered_result
($remote_list_returns_properties, @_);