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