]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Config.pm
e197dabbc25b0ea9c03214bfff1e8e8505223c96
[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);
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 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
138 __PACKAGE__->register_method ({
139 name => 'create',
140 protected => 1,
141 path => '',
142 method => 'POST',
143 description => "Create a new storage.",
144 permissions => {
145 check => ['perm', '/storage', ['Datastore.Allocate']],
146 },
147 parameters => PVE::Storage::Plugin->createSchema(),
148 returns => { type => 'null' },
149 code => sub {
150 my ($param) = @_;
151
152 my $type = extract_param($param, 'type');
153 my $storeid = extract_param($param, 'storage');
154
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
159 my $sensitive = extract_sensitive_params($param, []);
160
161 my $plugin = PVE::Storage::Plugin->lookup($type);
162 my $opts = $plugin->check_config($storeid, $param, 1, 1);
163
164 PVE::Storage::lock_storage_config(sub {
165 my $cfg = PVE::Storage::config();
166
167 if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
168 die "storage ID '$storeid' already defined\n";
169 }
170
171 $cfg->{ids}->{$storeid} = $opts;
172
173 $plugin->on_add_hook($storeid, $opts, %$sensitive);
174
175 eval {
176 # try to activate if enabled on local node,
177 # we only do this to detect errors/problems sooner
178 if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
179 PVE::Storage::activate_storage($cfg, $storeid);
180 }
181 };
182 if (my $err = $@) {
183 eval { $plugin->on_delete_hook($storeid, $opts) };
184 warn "$@\n" if $@;
185 die $err;
186 }
187
188 PVE::Storage::write_config($cfg);
189
190 }, "create storage failed");
191
192 return undef;
193 }});
194
195 __PACKAGE__->register_method ({
196 name => 'update',
197 protected => 1,
198 path => '{storage}',
199 method => 'PUT',
200 description => "Update storage configuration.",
201 permissions => {
202 check => ['perm', '/storage', ['Datastore.Allocate']],
203 },
204 parameters => PVE::Storage::Plugin->updateSchema(),
205 returns => { type => 'null' },
206 code => sub {
207 my ($param) = @_;
208
209 my $storeid = extract_param($param, 'storage');
210 my $digest = extract_param($param, 'digest');
211 my $delete = extract_param($param, 'delete');
212
213 if ($delete) {
214 $delete = [ PVE::Tools::split_list($delete) ];
215 }
216
217 PVE::Storage::lock_storage_config(sub {
218 my $cfg = PVE::Storage::config();
219
220 PVE::SectionConfig::assert_if_modified($cfg, $digest);
221
222 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
223 my $type = $scfg->{type};
224
225 my $sensitive = extract_sensitive_params($param, $delete);
226
227 my $plugin = PVE::Storage::Plugin->lookup($type);
228 my $opts = $plugin->check_config($storeid, $param, 0, 1);
229
230 if ($delete) {
231 my $options = $plugin->private()->{options}->{$type};
232 foreach my $k (@$delete) {
233 my $d = $options->{$k} || die "no such option '$k'\n";
234 die "unable to delete required option '$k'\n" if !$d->{optional};
235 die "unable to delete fixed option '$k'\n" if $d->{fixed};
236 die "cannot set and delete property '$k' at the same time!\n"
237 if defined($opts->{$k});
238
239 delete $scfg->{$k};
240 }
241 }
242
243 $plugin->on_update_hook($storeid, $opts, %$sensitive);
244
245 for my $k (keys %$opts) {
246 $scfg->{$k} = $opts->{$k};
247 }
248
249 PVE::Storage::write_config($cfg);
250
251 }, "update storage failed");
252
253 return undef;
254 }});
255
256 __PACKAGE__->register_method ({
257 name => 'delete',
258 protected => 1,
259 path => '{storage}', # /storage/config/{storage}
260 method => 'DELETE',
261 description => "Delete storage configuration.",
262 permissions => {
263 check => ['perm', '/storage', ['Datastore.Allocate']],
264 },
265 parameters => {
266 additionalProperties => 0,
267 properties => {
268 storage => get_standard_option('pve-storage-id', {
269 completion => \&PVE::Storage::complete_storage,
270 }),
271 },
272 },
273 returns => { type => 'null' },
274 code => sub {
275 my ($param) = @_;
276
277 my $storeid = extract_param($param, 'storage');
278
279 PVE::Storage::lock_storage_config(sub {
280 my $cfg = PVE::Storage::config();
281
282 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
283
284 die "can't remove storage - storage is used as base of another storage\n"
285 if PVE::Storage::storage_is_used($cfg, $storeid);
286
287 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
288
289 $plugin->on_delete_hook($storeid, $scfg);
290
291 delete $cfg->{ids}->{$storeid};
292
293 PVE::Storage::write_config($cfg);
294
295 }, "delete storage failed");
296
297 PVE::AccessControl::remove_storage_access($storeid);
298
299 return undef;
300 }});
301
302 1;