]>
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 | |
123 | for my $opt (qw(password encryption_key)) { | |
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(), |
b6cf0a66 DM |
148 | returns => { type => 'null' }, |
149 | code => sub { | |
150 | my ($param) = @_; | |
151 | ||
1dc01b9f DM |
152 | my $type = extract_param($param, 'type'); |
153 | my $storeid = extract_param($param, 'storage'); | |
b6cf0a66 | 154 | |
a4a9405d WL |
155 | # revent an empty nodelist. |
156 | # fix me in section config create never need an empty entity. | |
157 | delete $param->{nodes} if !$param->{nodes}; | |
158 | ||
72385de9 | 159 | my $sensitive = extract_sensitive_params($param, []); |
a4a9405d | 160 | |
1dc01b9f DM |
161 | my $plugin = PVE::Storage::Plugin->lookup($type); |
162 | my $opts = $plugin->check_config($storeid, $param, 1, 1); | |
b6cf0a66 DM |
163 | |
164 | PVE::Storage::lock_storage_config( | |
165 | sub { | |
166 | ||
83d7192f | 167 | my $cfg = PVE::Storage::config(); |
b6cf0a66 | 168 | |
1dc01b9f | 169 | if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) { |
b6cf0a66 DM |
170 | die "storage ID '$storeid' already defined\n"; |
171 | } | |
172 | ||
173 | $cfg->{ids}->{$storeid} = $opts; | |
174 | ||
72385de9 | 175 | $plugin->on_add_hook($storeid, $opts, %$sensitive); |
3932ca0d | 176 | |
a4a9405d WL |
177 | eval { |
178 | # try to activate if enabled on local node, | |
179 | # we only do this to detect errors/problems sooner | |
180 | if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) { | |
181 | PVE::Storage::activate_storage($cfg, $storeid); | |
182 | } | |
183 | }; | |
187e32ce | 184 | if (my $err = $@) { |
3932ca0d TL |
185 | eval { $plugin->on_delete_hook($storeid, $opts) }; |
186 | warn "$@\n" if $@; | |
a4a9405d | 187 | die $err; |
b6cf0a66 DM |
188 | } |
189 | ||
83d7192f | 190 | PVE::Storage::write_config($cfg); |
37ab64f3 | 191 | |
b6cf0a66 DM |
192 | }, "create storage failed"); |
193 | ||
1dc01b9f | 194 | return undef; |
b6cf0a66 DM |
195 | }}); |
196 | ||
197 | __PACKAGE__->register_method ({ | |
198 | name => 'update', | |
199 | protected => 1, | |
200 | path => '{storage}', | |
201 | method => 'PUT', | |
202 | description => "Update storage configuration.", | |
37ab64f3 | 203 | permissions => { |
5f642f73 DM |
204 | check => ['perm', '/storage', ['Datastore.Allocate']], |
205 | }, | |
1dc01b9f | 206 | parameters => PVE::Storage::Plugin->updateSchema(), |
b6cf0a66 DM |
207 | returns => { type => 'null' }, |
208 | code => sub { | |
209 | my ($param) = @_; | |
210 | ||
1dc01b9f DM |
211 | my $storeid = extract_param($param, 'storage'); |
212 | my $digest = extract_param($param, 'digest'); | |
4273e3ac | 213 | my $delete = extract_param($param, 'delete'); |
b6cf0a66 | 214 | |
72385de9 WB |
215 | if ($delete) { |
216 | $delete = [ PVE::Tools::split_list($delete) ]; | |
217 | } | |
218 | ||
37ab64f3 | 219 | PVE::Storage::lock_storage_config(sub { |
b6cf0a66 | 220 | |
83d7192f | 221 | my $cfg = PVE::Storage::config(); |
b6cf0a66 | 222 | |
1dc01b9f | 223 | PVE::SectionConfig::assert_if_modified($cfg, $digest); |
b6cf0a66 | 224 | |
1dc01b9f | 225 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); |
0ff4cfea DM |
226 | my $type = $scfg->{type}; |
227 | ||
72385de9 | 228 | my $sensitive = extract_sensitive_params($param, $delete); |
b6cf0a66 | 229 | |
0ff4cfea | 230 | my $plugin = PVE::Storage::Plugin->lookup($type); |
1dc01b9f | 231 | my $opts = $plugin->check_config($storeid, $param, 0, 1); |
b6cf0a66 | 232 | |
4273e3ac | 233 | if ($delete) { |
0ff4cfea | 234 | my $options = $plugin->private()->{options}->{$type}; |
72385de9 | 235 | foreach my $k (@$delete) { |
4273e3ac TL |
236 | my $d = $options->{$k} || die "no such option '$k'\n"; |
237 | die "unable to delete required option '$k'\n" if !$d->{optional}; | |
238 | die "unable to delete fixed option '$k'\n" if $d->{fixed}; | |
239 | die "cannot set and delete property '$k' at the same time!\n" | |
240 | if defined($opts->{$k}); | |
241 | ||
242 | delete $scfg->{$k}; | |
243 | } | |
244 | } | |
245 | ||
72385de9 | 246 | $plugin->on_update_hook($storeid, $opts, %$sensitive); |
0ff4cfea | 247 | |
91f42b33 | 248 | for my $k (keys %$opts) { |
b6cf0a66 DM |
249 | $scfg->{$k} = $opts->{$k}; |
250 | } | |
251 | ||
83d7192f | 252 | PVE::Storage::write_config($cfg); |
b6cf0a66 | 253 | |
37ab64f3 | 254 | }, "update storage failed"); |
b6cf0a66 DM |
255 | |
256 | return undef; | |
257 | }}); | |
258 | ||
259 | __PACKAGE__->register_method ({ | |
260 | name => 'delete', | |
261 | protected => 1, | |
262 | path => '{storage}', # /storage/config/{storage} | |
263 | method => 'DELETE', | |
264 | description => "Delete storage configuration.", | |
37ab64f3 | 265 | permissions => { |
5f642f73 DM |
266 | check => ['perm', '/storage', ['Datastore.Allocate']], |
267 | }, | |
b6cf0a66 | 268 | parameters => { |
37ab64f3 TL |
269 | additionalProperties => 0, |
270 | properties => { | |
f3bd890d DM |
271 | storage => get_standard_option('pve-storage-id', { |
272 | completion => \&PVE::Storage::complete_storage, | |
273 | }), | |
b6cf0a66 DM |
274 | }, |
275 | }, | |
276 | returns => { type => 'null' }, | |
277 | code => sub { | |
278 | my ($param) = @_; | |
279 | ||
1dc01b9f DM |
280 | my $storeid = extract_param($param, 'storage'); |
281 | ||
b6cf0a66 DM |
282 | PVE::Storage::lock_storage_config( |
283 | sub { | |
284 | ||
83d7192f | 285 | my $cfg = PVE::Storage::config(); |
b6cf0a66 | 286 | |
5a39d0a1 | 287 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); |
402df80b | 288 | |
b6cf0a66 | 289 | die "can't remove storage - storage is used as base of another storage\n" |
1dc01b9f | 290 | if PVE::Storage::storage_is_used($cfg, $storeid); |
b6cf0a66 | 291 | |
3932ca0d TL |
292 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
293 | ||
294 | $plugin->on_delete_hook($storeid, $scfg); | |
295 | ||
1dc01b9f | 296 | delete $cfg->{ids}->{$storeid}; |
b6cf0a66 | 297 | |
83d7192f | 298 | PVE::Storage::write_config($cfg); |
b6cf0a66 DM |
299 | |
300 | }, "delete storage failed"); | |
2a2cf20a AG |
301 | |
302 | PVE::AccessControl::remove_storage_access($storeid); | |
303 | ||
b6cf0a66 DM |
304 | return undef; |
305 | }}); | |
306 | ||
307 | 1; |