]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Config.pm
ea655c5805256264b6c8d6a41b792947bf460bc8
[pve-storage.git] / PVE / API2 / Storage / Config.pm
1 package PVE::API2::Storage::Config;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param extract_sensitive_params);
8 use PVE::Cluster qw(cfs_read_file cfs_write_file);
9 use PVE::Storage;
10 use PVE::Storage::Plugin;
11 use PVE::Storage::LVMPlugin;
12 use PVE::Storage::CIFSPlugin;
13 use HTTP::Status qw(:constants);
14 use Storable qw(dclone);
15 use PVE::JSONSchema qw(get_standard_option);
16 use PVE::RPCEnvironment;
17
18 use PVE::RESTHandler;
19
20 use base qw(PVE::RESTHandler);
21
22 my @ctypes = qw(images vztmpl iso backup);
23
24 my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
25
26 my $api_storage_config = sub {
27 my ($cfg, $storeid) = @_;
28
29 my $scfg = dclone(PVE::Storage::storage_config($cfg, $storeid));
30 $scfg->{storage} = $storeid;
31 $scfg->{digest} = $cfg->{digest};
32 $scfg->{content} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'content', $scfg->{content});
33
34 if ($scfg->{nodes}) {
35 $scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
36 }
37
38 return $scfg;
39 };
40
41 __PACKAGE__->register_method ({
42 name => 'index',
43 path => '',
44 method => 'GET',
45 description => "Storage index.",
46 permissions => {
47 description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
48 user => 'all',
49 },
50 parameters => {
51 additionalProperties => 0,
52 properties => {
53 type => {
54 description => "Only list storage of specific type",
55 type => 'string',
56 enum => $storage_type_enum,
57 optional => 1,
58 },
59 },
60 },
61 returns => {
62 type => 'array',
63 items => {
64 type => "object",
65 properties => { storage => { type => 'string'} },
66 },
67 links => [ { rel => 'child', href => "{storage}" } ],
68 },
69 code => sub {
70 my ($param) = @_;
71
72 my $rpcenv = PVE::RPCEnvironment::get();
73 my $authuser = $rpcenv->get_user();
74
75 my $cfg = PVE::Storage::config();
76
77 my @sids = PVE::Storage::storage_ids($cfg);
78
79 my $res = [];
80 foreach my $storeid (@sids) {
81 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
82 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
83
84 my $scfg = &$api_storage_config($cfg, $storeid);
85 next if $param->{type} && $param->{type} ne $scfg->{type};
86 push @$res, $scfg;
87 }
88
89 return $res;
90 }});
91
92 __PACKAGE__->register_method ({
93 name => 'read',
94 path => '{storage}',
95 method => 'GET',
96 description => "Read storage configuration.",
97 permissions => {
98 check => ['perm', '/storage/{storage}', ['Datastore.Allocate']],
99 },
100 parameters => {
101 additionalProperties => 0,
102 properties => {
103 storage => get_standard_option('pve-storage-id'),
104 },
105 },
106 returns => { type => 'object' },
107 code => sub {
108 my ($param) = @_;
109
110 my $cfg = PVE::Storage::config();
111
112 return &$api_storage_config($cfg, $param->{storage});
113 }});
114
115 my $sensitive_params = [qw(password encryption-key master-pubkey)];
116
117 __PACKAGE__->register_method ({
118 name => 'create',
119 protected => 1,
120 path => '',
121 method => 'POST',
122 description => "Create a new storage.",
123 permissions => {
124 check => ['perm', '/storage', ['Datastore.Allocate']],
125 },
126 parameters => PVE::Storage::Plugin->createSchema(),
127 returns => {
128 type => 'object',
129 properties => {
130 storage => {
131 description => "The ID of the created storage.",
132 type => 'string',
133 },
134 type => {
135 description => "The type of the created storage.",
136 type => 'string',
137 enum => $storage_type_enum,
138 },
139 config => {
140 description => "Partial, possible server generated, configuration properties.",
141 type => 'object',
142 optional => 1,
143 additionalProperties => 1,
144 properties => {
145 'encryption-key' => {
146 description => "The, possible auto-generated, encryption-key.",
147 optional => 1,
148 type => 'string',
149 },
150 },
151 },
152 },
153 },
154 code => sub {
155 my ($param) = @_;
156
157 my $type = extract_param($param, 'type');
158 my $storeid = extract_param($param, 'storage');
159
160 # revent an empty nodelist.
161 # fix me in section config create never need an empty entity.
162 delete $param->{nodes} if !$param->{nodes};
163
164 my $sensitive = extract_sensitive_params($param, $sensitive_params, []);
165
166 my $plugin = PVE::Storage::Plugin->lookup($type);
167 my $opts = $plugin->check_config($storeid, $param, 1, 1);
168
169 my $returned_config;
170 PVE::Storage::lock_storage_config(sub {
171 my $cfg = PVE::Storage::config();
172
173 if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
174 die "storage ID '$storeid' already defined\n";
175 }
176
177 $cfg->{ids}->{$storeid} = $opts;
178
179 $returned_config = $plugin->on_add_hook($storeid, $opts, %$sensitive);
180
181 eval {
182 # try to activate if enabled on local node,
183 # we only do this to detect errors/problems sooner
184 if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
185 PVE::Storage::activate_storage($cfg, $storeid);
186 }
187 };
188 if (my $err = $@) {
189 eval { $plugin->on_delete_hook($storeid, $opts) };
190 warn "$@\n" if $@;
191 die $err;
192 }
193
194 PVE::Storage::write_config($cfg);
195
196 }, "create storage failed");
197
198 my $res = {
199 storage => $storeid,
200 type => $type,
201 };
202 $res->{config} = $returned_config if $returned_config;
203 return $res;
204 }});
205
206 __PACKAGE__->register_method ({
207 name => 'update',
208 protected => 1,
209 path => '{storage}',
210 method => 'PUT',
211 description => "Update storage configuration.",
212 permissions => {
213 check => ['perm', '/storage', ['Datastore.Allocate']],
214 },
215 parameters => PVE::Storage::Plugin->updateSchema(),
216 returns => {
217 type => 'object',
218 properties => {
219 storage => {
220 description => "The ID of the created storage.",
221 type => 'string',
222 },
223 type => {
224 description => "The type of the created storage.",
225 type => 'string',
226 enum => $storage_type_enum,
227 },
228 config => {
229 description => "Partial, possible server generated, configuration properties.",
230 type => 'object',
231 optional => 1,
232 additionalProperties => 1,
233 properties => {
234 'encryption-key' => {
235 description => "The, possible auto-generated, encryption-key.",
236 optional => 1,
237 type => 'string',
238 },
239 },
240 },
241 },
242 },
243 code => sub {
244 my ($param) = @_;
245
246 my $storeid = extract_param($param, 'storage');
247 my $digest = extract_param($param, 'digest');
248 my $delete = extract_param($param, 'delete');
249 my $type;
250
251 if ($delete) {
252 $delete = [ PVE::Tools::split_list($delete) ];
253 }
254
255 my $returned_config;
256 PVE::Storage::lock_storage_config(sub {
257 my $cfg = PVE::Storage::config();
258
259 PVE::SectionConfig::assert_if_modified($cfg, $digest);
260
261 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
262 $type = $scfg->{type};
263
264 my $sensitive = extract_sensitive_params($param, $sensitive_params, $delete);
265
266 my $plugin = PVE::Storage::Plugin->lookup($type);
267 my $opts = $plugin->check_config($storeid, $param, 0, 1);
268
269 if ($delete) {
270 my $options = $plugin->private()->{options}->{$type};
271 foreach my $k (@$delete) {
272 my $d = $options->{$k} || die "no such option '$k'\n";
273 die "unable to delete required option '$k'\n" if !$d->{optional};
274 die "unable to delete fixed option '$k'\n" if $d->{fixed};
275 die "cannot set and delete property '$k' at the same time!\n"
276 if defined($opts->{$k});
277
278 delete $scfg->{$k};
279 }
280 }
281
282 $returned_config = $plugin->on_update_hook($storeid, $opts, %$sensitive);
283
284 for my $k (keys %$opts) {
285 $scfg->{$k} = $opts->{$k};
286 }
287
288 PVE::Storage::write_config($cfg);
289
290 }, "update storage failed");
291
292 my $res = {
293 storage => $storeid,
294 type => $type,
295 };
296 $res->{config} = $returned_config if $returned_config;
297 return $res;
298 }});
299
300 __PACKAGE__->register_method ({
301 name => 'delete',
302 protected => 1,
303 path => '{storage}', # /storage/config/{storage}
304 method => 'DELETE',
305 description => "Delete storage configuration.",
306 permissions => {
307 check => ['perm', '/storage', ['Datastore.Allocate']],
308 },
309 parameters => {
310 additionalProperties => 0,
311 properties => {
312 storage => get_standard_option('pve-storage-id', {
313 completion => \&PVE::Storage::complete_storage,
314 }),
315 },
316 },
317 returns => { type => 'null' },
318 code => sub {
319 my ($param) = @_;
320
321 my $storeid = extract_param($param, 'storage');
322
323 PVE::Storage::lock_storage_config(sub {
324 my $cfg = PVE::Storage::config();
325
326 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
327
328 die "can't remove storage - storage is used as base of another storage\n"
329 if PVE::Storage::storage_is_used($cfg, $storeid);
330
331 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
332
333 $plugin->on_delete_hook($storeid, $scfg);
334
335 delete $cfg->{ids}->{$storeid};
336
337 PVE::Storage::write_config($cfg);
338
339 }, "delete storage failed");
340
341 PVE::AccessControl::remove_storage_access($storeid);
342
343 return undef;
344 }});
345
346 1;