]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/PBS/Remote.pm
2eba9bb891f9febc921fbb18ae030c46c145b03d
[pmg-api.git] / src / PMG / API2 / PBS / Remote.pm
1 package PMG::API2::PBS::Remote;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param);
8 use PVE::JSONSchema qw(get_standard_option);
9 use PVE::RESTHandler;
10 use PVE::PBSClient;
11
12 use PMG::PBSConfig;
13
14 use base qw(PVE::RESTHandler);
15
16 __PACKAGE__->register_method ({
17 name => 'list',
18 path => '',
19 method => 'GET',
20 description => "List all configured Proxmox Backup Server instances.",
21 permissions => { check => [ 'admin', 'audit' ] },
22 proxyto => 'master',
23 protected => 1,
24 parameters => {
25 additionalProperties => 0,
26 properties => {}
27 },
28 returns => {
29 type => "array",
30 items => PMG::PBSConfig->createSchema(1),
31 links => [ { rel => 'child', href => "{remote}" } ],
32 },
33 code => sub {
34 my ($param) = @_;
35
36 my $res = [];
37
38 my $conf = PMG::PBSConfig->new();
39 return $res if !defined($conf);
40
41 for my $remote (sort keys %{$conf->{ids}}) {
42 my $d = $conf->{ids}->{$remote};
43 push @$res, {
44 remote => $remote,
45 server => $d->{server},
46 datastore => $d->{datastore},
47 username => $d->{username},
48 disable => $d->{disable},
49 };
50 }
51
52 return $res;
53 }});
54
55 __PACKAGE__->register_method ({
56 name => 'create',
57 path => '',
58 method => 'POST',
59 description => "Add Proxmox Backup Server remote instance.",
60 permissions => { check => [ 'admin' ] },
61 proxyto => 'master',
62 protected => 1,
63 parameters => PMG::PBSConfig->createSchema(1),
64 returns => { type => 'null' } ,
65 code => sub {
66 my ($param) = @_;
67
68 my $code = sub {
69 my $conf = PMG::PBSConfig->new();
70 $conf->{ids} //= {};
71 my $ids = $conf->{ids};
72
73 my $remote = extract_param($param, 'remote');
74 die "PBS remote '$remote' already exists\n" if $ids->{$remote};
75
76 my $remotecfg = PMG::PBSConfig->check_config($remote, $param, 1);
77
78 my $password = extract_param($remotecfg, 'password');
79
80 my $pbs = PVE::PBSClient->new($remotecfg, $remote, $conf->{secret_dir});
81 $pbs->set_password($password) if defined($password);
82
83 $ids->{$remote} = $remotecfg;
84 $conf->write();
85 };
86
87 PMG::PBSConfig::lock_config($code, "add PBS remote failed");
88
89 return undef;
90 }});
91
92 __PACKAGE__->register_method ({
93 name => 'read_config',
94 path => '{remote}',
95 method => 'GET',
96 description => "Get Proxmox Backup Server remote configuration.",
97 proxyto => 'master',
98 permissions => { check => [ 'admin', 'audit' ] },
99 parameters => {
100 additionalProperties => 1,
101 properties => {
102 remote => {
103 description => "Proxmox Backup Server ID.",
104 type => 'string', format => 'pve-configid',
105 },
106 },
107 },
108 returns => {},
109 code => sub {
110 my ($param) = @_;
111
112 my $conf = PMG::PBSConfig->new();
113
114 my $remote = $param->{remote};
115
116 my $data = $conf->{ids}->{$remote};
117 die "PBS remote '$remote' does not exist\n" if !$data;
118
119 delete $data->{type};
120
121 $data->{digest} = $conf->{digest};
122 $data->{remote} = $remote;
123
124 return $data;
125 }});
126
127 __PACKAGE__->register_method ({
128 name => 'update_config',
129 path => '{remote}',
130 method => 'PUT',
131 description => "Update PBS remote settings.",
132 permissions => { check => [ 'admin' ] },
133 protected => 1,
134 proxyto => 'master',
135 parameters => PMG::PBSConfig->updateSchema(),
136 returns => { type => 'null' },
137 code => sub {
138 my ($param) = @_;
139
140 my $code = sub {
141
142 my $conf = PMG::PBSConfig->new();
143 my $ids = $conf->{ids};
144
145 my $digest = extract_param($param, 'digest');
146 PVE::SectionConfig::assert_if_modified($conf, $digest);
147
148 my $remote = extract_param($param, 'remote');
149
150 die "PBS remote '$remote' does not exist\n" if !$ids->{$remote};
151
152 my $delete_str = extract_param($param, 'delete');
153 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
154
155 my $pbs = PVE::PBSClient->new($ids->{$remote}, $remote, $conf->{secret_dir});
156 foreach my $opt (PVE::Tools::split_list($delete_str)) {
157 if ($opt eq 'password') {
158 $pbs->delete_password();
159 }
160 delete $ids->{$remote}->{$opt};
161 }
162
163 if (defined(my $password = extract_param($param, 'password'))) {
164 $pbs->set_password($password);
165 }
166
167 my $remoteconfig = PMG::PBSConfig->check_config($remote, $param, 0, 1);
168
169 foreach my $p (keys %$remoteconfig) {
170 $ids->{$remote}->{$p} = $remoteconfig->{$p};
171 }
172
173 $conf->write();
174 };
175
176 PMG::PBSConfig::lock_config($code, "update PBS remote failed");
177
178 return undef;
179 }});
180
181 __PACKAGE__->register_method ({
182 name => 'delete',
183 path => '{remote}',
184 method => 'DELETE',
185 description => "Delete an PBS remote",
186 permissions => { check => [ 'admin' ] },
187 protected => 1,
188 proxyto => 'master',
189 parameters => {
190 additionalProperties => 0,
191 properties => {
192 remote => {
193 description => "Profile ID.",
194 type => 'string', format => 'pve-configid',
195 },
196 }
197 },
198 returns => { type => 'null' },
199 code => sub {
200 my ($param) = @_;
201
202 my $code = sub {
203
204 my $conf = PMG::PBSConfig->new();
205 my $ids = $conf->{ids};
206
207 my $remote = $param->{remote};
208 die "PBS remote '$remote' does not exist\n" if !$ids->{$remote};
209
210 my $pbs = PVE::PBSClient->new($ids->{$remote}, $remote, $conf->{secret_dir});
211 $pbs->delete_password($remote);
212 delete $ids->{$remote};
213
214 $conf->write();
215 };
216
217 PMG::PBSConfig::lock_config($code, "delete PBS remote failed");
218
219 return undef;
220 }});
221
222 1;