]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
6c5b537d84c3aca9cb17b3ad7517f16bde638b14
[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::Tools qw(file_get_contents file_set_contents);
9
10 sub new {
11 my ($class) = @_;
12
13 my $self = {
14 file => File::HomeDir::home() . '/.pveclient',
15 };
16 bless $self => $class;
17
18 $self->load();
19
20 return $self;
21 }
22
23 sub load {
24 my ($self) = @_;
25
26 if (-e $self->{file}) {
27 my $filemode = (stat($self->{file}))[2] & 07777;
28 if ($filemode != 0600) {
29 die sprintf "wrong permissions on '$self->{file}' %04o (expected 0600)\n", $filemode;
30 }
31
32 my $contents = file_get_contents($self->{file});
33 $self->{data} = from_json($contents);
34 } else {
35 $self->{data} = {};
36 }
37
38 if (!exists($self->{data}->{remotes})) {
39 $self->{data}->{remotes} = {};
40 }
41
42 # Verify config
43 for my $name (@{$self->remote_names}) {
44 my $cfg = $self->{data}->{remotes}->{$name};
45
46 foreach my $opt (qw(host port username fingerprint)) {
47 die "missing option '$opt' (remote '$name')" if !defined($cfg->{$opt});
48 }
49 }
50 }
51
52 sub save {
53 my ($self) = @_;
54
55 my $contents = to_json($self->{data}, {pretty => 1, canonical => 1});
56 file_set_contents($self->{file}, $contents, 0600);
57 }
58
59 sub add_remote {
60 my ($self, $name, $host, $port, $fingerprint, $username, $password) = @_;
61
62 $self->{data}->{remotes}->{$name} = {
63 host => $host,
64 port => $port,
65 fingerprint => $fingerprint,
66 username => $username,
67 };
68
69 if (defined($password)) {
70 $self->{data}->{remotes}->{$name}->{password} = $password;
71 }
72 }
73
74 sub remote_names {
75 my ($self) = @_;
76
77 return [keys %{$self->{data}->{remotes}}];
78 }
79
80 sub lookup_remote {
81 my ($self, $name) = @_;
82
83 die "Unknown remote \"$name\" given"
84 if (!exists($self->{data}->{remotes}->{$name}));
85
86 return $self->{data}->{remotes}->{$name};
87 }
88
89 sub remotes {
90 my ($self) = @_;
91
92 my $res = {};
93
94 # Remove the password from each remote.
95 for my $name ($self->remote_names) {
96 my $cfg = $self->{data}->{remotes}->{$name};
97 $res->{$name} = {
98 host => $cfg->{host},
99 port => $cfg->{port},
100 username => $cfg->{username},
101 fingerprint => $cfg->{fingerprint},
102 };
103 }
104
105 return $res;
106 }
107
108 sub remove_remote {
109 my ($self, $remote) = @_;
110
111 $self->lookup_remote($remote);
112
113 delete($self->{data}->{remotes}->{$remote});
114
115 $self->save();
116 }
117
118 sub remote_conn {
119 my ($self, $remote) = @_;
120
121 my $section = $self->lookup_remote($remote);
122 my $conn = PVE::APIClient::LWP->new(
123 username => $section->{username},
124 password => $section->{password},
125 host => $section->{host},
126 port => $section->{port},
127 cached_fingerprints => {
128 $section->{fingerprint} => 1,
129 }
130 );
131
132 $conn->login;
133
134 return $conn;
135 }
136
137 1;