]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Storage/Config.pm
api/config: fix indentation
[pve-storage.git] / PVE / API2 / Storage / Config.pm
CommitLineData
b6cf0a66
DM
1package PVE::API2::Storage::Config;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
1dc01b9f 7use PVE::Tools qw(extract_param);
b6cf0a66
DM
8use PVE::Cluster qw(cfs_read_file cfs_write_file);
9use PVE::Storage;
1dc01b9f 10use PVE::Storage::Plugin;
304344ce 11use PVE::Storage::LVMPlugin;
a9db2ca8 12use PVE::Storage::CIFSPlugin;
b6cf0a66
DM
13use HTTP::Status qw(:constants);
14use Storable qw(dclone);
15use PVE::JSONSchema qw(get_standard_option);
5f642f73 16use PVE::RPCEnvironment;
b6cf0a66
DM
17
18use PVE::RESTHandler;
19
20use base qw(PVE::RESTHandler);
21
22my @ctypes = qw(images vztmpl iso backup);
23
1dc01b9f 24my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
b6cf0a66
DM
25
26my $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
115my 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(),
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 163
8ff8e277
TL
164 PVE::Storage::lock_storage_config(sub {
165 my $cfg = PVE::Storage::config();
b6cf0a66 166
8ff8e277
TL
167 if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
168 die "storage ID '$storeid' already defined\n";
169 }
b6cf0a66 170
8ff8e277 171 $cfg->{ids}->{$storeid} = $opts;
b6cf0a66 172
8ff8e277
TL
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);
b6cf0a66 180 }
8ff8e277
TL
181 };
182 if (my $err = $@) {
183 eval { $plugin->on_delete_hook($storeid, $opts) };
184 warn "$@\n" if $@;
185 die $err;
186 }
b6cf0a66 187
8ff8e277 188 PVE::Storage::write_config($cfg);
37ab64f3 189
8ff8e277 190 }, "create storage failed");
b6cf0a66 191
1dc01b9f 192 return undef;
b6cf0a66
DM
193 }});
194
195__PACKAGE__->register_method ({
196 name => 'update',
197 protected => 1,
198 path => '{storage}',
199 method => 'PUT',
200 description => "Update storage configuration.",
37ab64f3 201 permissions => {
5f642f73
DM
202 check => ['perm', '/storage', ['Datastore.Allocate']],
203 },
1dc01b9f 204 parameters => PVE::Storage::Plugin->updateSchema(),
b6cf0a66
DM
205 returns => { type => 'null' },
206 code => sub {
207 my ($param) = @_;
208
1dc01b9f
DM
209 my $storeid = extract_param($param, 'storage');
210 my $digest = extract_param($param, 'digest');
4273e3ac 211 my $delete = extract_param($param, 'delete');
b6cf0a66 212
72385de9
WB
213 if ($delete) {
214 $delete = [ PVE::Tools::split_list($delete) ];
215 }
216
37ab64f3 217 PVE::Storage::lock_storage_config(sub {
83d7192f 218 my $cfg = PVE::Storage::config();
b6cf0a66 219
1dc01b9f 220 PVE::SectionConfig::assert_if_modified($cfg, $digest);
b6cf0a66 221
1dc01b9f 222 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
0ff4cfea
DM
223 my $type = $scfg->{type};
224
72385de9 225 my $sensitive = extract_sensitive_params($param, $delete);
b6cf0a66 226
0ff4cfea 227 my $plugin = PVE::Storage::Plugin->lookup($type);
1dc01b9f 228 my $opts = $plugin->check_config($storeid, $param, 0, 1);
b6cf0a66 229
4273e3ac 230 if ($delete) {
0ff4cfea 231 my $options = $plugin->private()->{options}->{$type};
72385de9 232 foreach my $k (@$delete) {
4273e3ac
TL
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
72385de9 243 $plugin->on_update_hook($storeid, $opts, %$sensitive);
0ff4cfea 244
91f42b33 245 for my $k (keys %$opts) {
b6cf0a66
DM
246 $scfg->{$k} = $opts->{$k};
247 }
248
83d7192f 249 PVE::Storage::write_config($cfg);
b6cf0a66 250
37ab64f3 251 }, "update storage failed");
b6cf0a66
DM
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.",
37ab64f3 262 permissions => {
5f642f73
DM
263 check => ['perm', '/storage', ['Datastore.Allocate']],
264 },
b6cf0a66 265 parameters => {
37ab64f3
TL
266 additionalProperties => 0,
267 properties => {
f3bd890d
DM
268 storage => get_standard_option('pve-storage-id', {
269 completion => \&PVE::Storage::complete_storage,
270 }),
b6cf0a66
DM
271 },
272 },
273 returns => { type => 'null' },
274 code => sub {
275 my ($param) = @_;
276
1dc01b9f
DM
277 my $storeid = extract_param($param, 'storage');
278
8ff8e277
TL
279 PVE::Storage::lock_storage_config(sub {
280 my $cfg = PVE::Storage::config();
b6cf0a66 281
8ff8e277 282 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
402df80b 283
8ff8e277
TL
284 die "can't remove storage - storage is used as base of another storage\n"
285 if PVE::Storage::storage_is_used($cfg, $storeid);
b6cf0a66 286
8ff8e277 287 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
3932ca0d 288
8ff8e277 289 $plugin->on_delete_hook($storeid, $scfg);
3932ca0d 290
8ff8e277 291 delete $cfg->{ids}->{$storeid};
b6cf0a66 292
8ff8e277 293 PVE::Storage::write_config($cfg);
b6cf0a66 294
8ff8e277 295 }, "delete storage failed");
2a2cf20a
AG
296
297 PVE::AccessControl::remove_storage_access($storeid);
298
b6cf0a66
DM
299 return undef;
300 }});
301
3021;