]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
8a7784896f7d881fc93fef3eb7d4bf3cc91658ed
[pve-client.git] / PVE / APIClient / Config.pm
1 package PVE::APIClient::Config;
2
3 use strict;
4 use warnings;
5 use JSON;
6
7 use File::HomeDir ();
8 use PVE::JSONSchema qw(register_standard_option get_standard_option);
9 use PVE::SectionConfig;
10 use PVE::Tools qw(file_get_contents file_set_contents);
11
12 use base qw(PVE::SectionConfig);
13
14 my $complete_remote_name = sub {
15
16 my $config = PVE::APIClient::Config->load();
17 return [keys %{$config->{ids}}];
18 };
19
20 register_standard_option('pveclient-remote-name', {
21 description => "The name of the remote.",
22 type => 'string',
23 pattern => qr(\w+),
24 completion => $complete_remote_name,
25 });
26
27
28 my $defaultData = {
29 propertyList => {
30 type => {
31 description => "Section type.",
32 optional => 1,
33 },
34 name => get_standard_option('pveclient-remote-name'),
35 host => {
36 description => "The host.",
37 type => 'string', format => 'address',
38 optional => 1,
39 },
40 username => {
41 description => "The username.",
42 type => 'string',
43 optional => 1,
44 },
45 password => {
46 description => "The users password.",
47 type => 'string',
48 optional => 1,
49 },
50 port => {
51 description => "The port.",
52 type => 'integer',
53 optional => 1,
54 default => 8006,
55 },
56 fingerprint => {
57 description => "Fingerprint.",
58 type => 'string',
59 optional => 1,
60 },
61 comment => {
62 description => "Description.",
63 type => 'string',
64 optional => 1,
65 maxLength => 4096,
66 },
67 },
68 };
69
70 sub type {
71 return 'remote';
72 }
73
74 sub options {
75 return {
76 name => { optional => 0 },
77 host => { optional => 0 },
78 comment => { optional => 1 },
79 username => { optional => 0 },
80 password => { optional => 0 },
81 port => { optional => 1 },
82 fingerprint => { optional => 1 },
83 };
84 }
85
86 sub private {
87 return $defaultData;
88 }
89
90 sub config_filename {
91 my ($class) = @_;
92
93 return File::HomeDir::home() . '/.pveclient';
94 }
95
96 sub load {
97 my ($class) = @_;
98
99 my $filename = $class->config_filename();
100
101 my $raw = '';
102
103 if (-e $filename) {
104 my $filemode = (stat($filename))[2] & 07777;
105 if ($filemode != 0600) {
106 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
107 }
108
109 $raw = file_get_contents($filename);
110 }
111
112 return $class->parse_config($filename, $raw);
113 }
114
115 sub save {
116 my ($class, $cfg) = @_;
117
118 my $filename = $class->config_filename();
119 my $raw = $class->write_config($filename, $cfg);
120
121 file_set_contents($filename, $raw, 0600);
122 }
123
124 sub lookup_remote {
125 my ($class, $cfg, $name, $noerr) = @_;
126
127 my $data = $cfg->{ids}->{$name};
128
129 return $data if $noerr || defined($data);
130
131 die "unknown remote \"$name\"\n";
132 }
133
134 sub remote_conn {
135 my ($class, $cfg, $remote) = @_;
136
137 my $section = $class->lookup_remote($cfg, $remote);
138 my $conn = PVE::APIClient::LWP->new(
139 username => $section->{username},
140 password => $section->{password},
141 host => $section->{host},
142 port => $section->{port},
143 cached_fingerprints => {
144 $section->{fingerprint} => 1,
145 }
146 );
147
148 $conn->login;
149
150 return $conn;
151 }
152
153 __PACKAGE__->register();
154 __PACKAGE__->init();
155
156 1;